1

I'm interested in creating a UWP application (running on HoloLens, though that shouldn't matter in terms of code) that launches to a default 2D XAML view and then can switch to 3D view upon command. The 3D view is being created with UrhoSharp.

While researching how to do this online I came across the following code which looked like just the ticket for me (although it launches in 3D then switches to 2D, the opposite sequence to what I want):

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

I'm finding the code pretty tough to understand with all the async/await lambdas, but the final function "SwitchViewsAsync" has got me stumped. I'll reproduce the code below 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 I'm most confused about is the "SwitchViewAsync()" method at the bottom. It's calling "ApplicationView.GetForCurrentView()" and then switches to it via "ApplicationViewSwitcher.SwitchAsync(...)". This seems to be getting the current view...and then switching to it? If the view is current why switch to it? Can anyone please help me understand what's happening here?

4

0 回答 0