0

I have a UWP app that opens a projection window (similar to the sample app). Now I want react to the RotationChanged-Event in both windows regardless of which window is active.

Can I somehow share the instance for the RadialController? Or do I have to manage it in one view and manually pass the event to the second one?

Updated Question: How do I use the same ViewModel instance in both windows?

P.s: I think adding RadialController and/or Surface-Dial as tags might make sense.

JamesLiu
  • 178
  • 6
Thomas
  • 4,030
  • 4
  • 40
  • 79

2 Answers2

0

Using UWP, I think you are supposed to use the MVVM pattern.

If you are using the MVVM pattern, having multiple GUI Elements (Windows included) react to a single ViewModel change would be trivial. So the real question is: Are you use the MVVM pattern, another pattern or no pattern at all?

If you need to learn it first, I wrote something about it regarding to WPF (UWP is a followup of WPF, with App-related backend stuff thrown in): https://social.msdn.microsoft.com/Forums/vstudio/en-US/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2/lets-talk-about-mvvm?forum=wpf

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • I'm not sure it is that easy. With `CoreWindow`s running on dedicated threads each and being non-agile types, you will have to implement some sort of marshaling. Unless the C# language projection already implements that marshaling, the onus is on you. I could be wrong, though. – IInspectable Feb 21 '17 at 11:10
  • Luckily WPF/UWP were designed from the ground up with the Dispatcher and Multithreading in mind: http://stackoverflow.com/questions/32671719/uwp-updating-ui-through-data-binding-from-a-background-thread You may have to write the Binding code manually rather then using the Binding class, but that should be about it. It is one of the rare cases you need Viewside code (as this is a Viewside issue). It is a bit odd that a GUI Element is in an alternate Thread (main/original thread is the default), but for draw heavy stuff it definetly makes sense. – Christopher Feb 21 '17 at 11:22
  • First off, I am using MVVM. After your post I was trying to bind both Windows to the same ViewModel, but ended up with two separate instances :-/ So do I have to send the events manually to the second window using a Dispatcher? – Thomas Feb 21 '17 at 12:12
0

Unfortunately it's not as simple as using the same ViewModel as @IInspectable pointed out. That's why I sent a command to the second view on every Dial_RotationChanged. Not as pretty as I would have hoped, but works quite well so far.

The code looks somewhat like this:

private async void Dial_RotationChanged(RadialController sender,
                                        RadialControllerRotationChangedEventArgs args)
{
    await ProjectionViewPageControl.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
    () =>
    {
        var thePage = (DetailPage)((Frame)Window.Current.Content).Content;
        thePage.ProjectionTest(args.RotationDeltaInDegrees);
    });
}
Thomas
  • 4,030
  • 4
  • 40
  • 79