UWP problems with multiple views

前端 未结 4 713
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 17:06

I am writing an app which should be able to run multiple views to edit different documents each in their own window. I\'ve wrote some code which works, but I\'m having some

4条回答
  •  清酒与你
    2021-01-02 17:33

    don't View(your)Lifetime away ... cheers,

    int idCreate = 0; List idSaved = new List();
    protected override async void OnLaunched(LaunchActivatedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
        {
            rootFrame = new Frame();
            rootFrame.NavigationFailed += OnNavigationFailed;
            Window.Current.Content = rootFrame;
        }
    
        if (rootFrame.Content == null)
        {
            rootFrame.Navigate(typeof(MainPage), e.Arguments);
            idSaved.Add(ApplicationView.GetForCurrentView().Id);
        }
        else
        {
            var create = CoreApplication.CreateNewView(); 
            await create.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                var frame = new Frame();
                frame.Navigate(typeof(MainPage), e.Arguments);
                Window.Current.Content = frame;
                Window.Current.Activate();
    
                idCreate = ApplicationView.GetForCurrentView().Id;
            });
    
            for(int i = idSaved.Count - 1; i >= 0; i--)
                if (await ApplicationViewSwitcher.TryShowAsStandaloneAsync(
                        idCreate, ViewSizePreference.UseMinimum, 
                        idSaved[i], ViewSizePreference.UseMinimum)
                   ) break;
    
            idSaved.Add(idCreate);
        }
        Window.Current.Activate();
    }
    

提交回复
热议问题