I'm currently rewriting a Joystick Usercontrol from the old typical codebehind usercontrol into a MVPVM WinUI 3 and having a problem referencing a UIElement from the presenter class. Yes I know you aren't supposed to reference the UI from the presenter. Just need help on how to rewrite it. The ControlArea in the Presenter Class is where I'm having trouble with. Can someone tell me how to rewrite these two lines?
The View UI code is:
<Ellipse
x:Name="ControlArea"
Height="{x:Bind p.ViewModel.OuterDiameter}"
Width="{x:Bind p.ViewModel.OuterDiameter}"
Fill="{x:Bind p.ViewModel.OuterFill}"
Stroke="{x:Bind p.ViewModel.OuterStroke}"/>
The Presenter Class Method is:
void OnPointerMoved(object sender, PointerRoutedEventArgs eventArgs)
{
if (!ViewModel.ControllerPressed) return;
Double x = eventArgs.GetCurrentPoint(ControlArea).Position.X - ViewModel.ControlRadius;
Double y = eventArgs.GetCurrentPoint(ControlArea).Position.Y - ViewModel.ControlRadius;
double disp = Math.Sqrt(x * x + y * y);
if (disp < ViewModel.ControlRadius)
{
ViewModel.JoystickTransform.X = ViewModel.XValue = x;
ViewModel.JoystickTransform.Y = ViewModel.YValue = y;
}
else
{
ViewModel.JoystickTransform.X = ViewModel.XValue = ViewModel.ControlRadius * (x / disp); //A*cos(x)
ViewModel.JoystickTransform.Y = ViewModel.YValue = ViewModel.ControlRadius * (y / disp); //A*sin(x)
}
ViewModel.OnJoyStickMoved?.Invoke(sender, new EventArgs());
}
This is an example of how to make an
Ellipsefollow the mouse.Make sure to set the
Canvas'sBackgroundto get pointer events.UPDATE
You can also use bindings.