none
Need a little help understanding this view switching example RRS feed

  • Question

  • Hi everyone, I'm in need of some help with the code example shown here:

    https://mtaulty.com/2016/10/25/windows-10-uwp-hololens-and-switching-2d3d-views/

    It's a UWP application that has two views: a "normal" 2D XAML view and then a 3D view. The author is running it on the HoloLens emulator. When he clicks it switches between views. I'll reproduce the code here for convenience:

    namespace ViewLibrary
    {
      using System;
      using System.Threading.Tasks;
      using Windows.ApplicationModel.Core;
      using Windows.UI;
      using Windows.UI.Core;
      using Windows.UI.ViewManagement;
      using Windows.UI.Xaml;
      using Windows.UI.Xaml.Controls;
      using Windows.UI.Xaml.Media;
     
      public static class ViewManagement
      {
        static CoreApplicationView coreView3d;
        static CoreApplicationView coreView2d;
    
        public static async Task SwitchTo2DViewAsync()
        {
          if (coreView3d == null)
          {
            coreView3d = CoreApplication.MainView;
          }
    
          if (coreView2d == null)
          {
            coreView2d = CoreApplication.CreateNewView();
     
            await RunOnDispatcherAsync(
              coreView2d, 
              async () =>
              {
                Window.Current.Content = Create2dUI();
              }
            );
          }
    
          await RunOnDispatcherAsync(coreView2d, SwitchViewsAsync);
        }
    
        static UIElement Create2dUI()
        {
          var button = new Button()
          {
            HorizontalAlignment = HorizontalAlignment.Stretch,
            VerticalAlignment = VerticalAlignment.Stretch,
            Content = "Back to 3D",
            Background = new SolidColorBrush(Colors.Red)
          };
    
          button.Click += async (s, e) =>
          {
            await SwitchTo3DViewAsync();
          };
    
          return (button);
        }
    
        static async Task RunOnDispatcherAsync(CoreApplicationView view, 
          Func<Task> action)
        {
          await view.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () => action());
        }
    
        public static async Task SwitchTo3DViewAsync()
        {
          await RunOnDispatcherAsync(coreView3d, SwitchViewsAsync);
        }
    
        static async Task SwitchViewsAsync()
        {
          var view = ApplicationView.GetForCurrentView();
          await ApplicationViewSwitcher.SwitchAsync(view.Id);
          Window.Current.Activate();
        }
    
      }
    }

    What confuses me is the "SwitchViewsAsync()" method near the bottom of the above code. It calls "ApplicationView.GetForCurrentView()"...and then seems to switch to that same view (even though it's already the current view)? Can anyone walk me through this?

    Thanks so much in advance,

    L

    Tuesday, January 29, 2019 9:19 PM

All replies

  • Hi,

    Since the problem is related to HoloLens, it is out of the scope of the UWP forum. I suggest that you could ask about this in Mixed Reality Forum. You could get more support there.

    Thank you for your patience.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 30, 2019 7:35 AM