UWP多视图问题我正在编写一个应用程序,它应该能够运行多个视图,以便在各自的窗口中编辑不同的文档。我写了一些有效的代码,但遇到了一些问题。我写的代码是基于微软提供的MultipleViews示例(https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/MultipleViews)。我主要有两个问题。第一个是如果我关闭主视图,这是应用程序启动时打开的第一个窗口,那么我无法通过单击应用程序磁贴或打开关联的文件类型来打开任何新视图/窗口,直到我关闭所有视图/窗口并重新启动应用程序。第二个是当我尝试从MainPage.xaml.cs打开一个新的视图/窗口时,应用程序崩溃了。我用来管理App.xaml.cs中的图像的代码如下:sealedpartialclassApp:Application{//我使用这个布尔值来确定应用程序是否已经启动一次publicObservableCollectionSecondaryViews=newObservableCollection();私有CoreDispatchermainDispatcher;公共CoreDispatcherMainDispatcher{得到{返回mainDispatcher;}}privateintmainViewId;publicintMainViewId{get{返回mainViewId;}}publicApp(){this.InitializeComponent();this.Suspending+=OnSuspending;}protectedoverrideasyncvoidOnLaunched(LaunchActivatedEventArgse){FramerootFrame=Window.Current.ContentasFrame;if(rootFrame==null){rootFrame=newFrame();rootFrame.NavigationFailed+=OnNavigationFailed;if(e.PreviousExecutionState==ApplicationExecutionState.Terminated){//TODO:从先前挂起的应用程序加载状态}//将框架放在当前窗口中Window.Current.Content=rootFrame;}if(rootFrame.Content==null){alreadyLaunched=true;rootFrame.Navigate(typeof(MainPage),e.Arguments);}elseif(alreadyLaunched){varselectedView=awaitcreateMainPageAsync();if(null!=selectedView){selectedView.StartViewInUse();varviewShown=awaitApplicationViewSwitcher.TryShowAsStandaloneAsync(selectedView.Id,ViewSizePreference.Default,ApplicationView.GetForCurrentView().Id,ViewSizePreference.Default);awaitselectedView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>{varcurrentPage=(MainPage)((Frame)Window.Current.Content).Content;Window.Current.Activate();});selectedView.StopViewInUse();}}//确保当前窗口处于活动状态Window.Current.Activate();}protectedoverrideasyncvoidOnFileActivated(FileActivatedEventArgsargs){base.OnFileActivated(args);if(alreadyLaunched){//FramerootFrame=Window.Current.ContentasFrame;//((MainPage)rootFrame.Content).OpenFileActivated(args);变量electedView=awaitcreateMainPageAsync();if(null!=selectedView){selectedView.StartViewInUse();varviewShown=awaitApplicationViewSwitcher.TryShowAsStandaloneAsync(selectedView.Id,ViewSizePreference.Default,ApplicationView.GetForCurrentView().Id,ViewSizePreference.Default);等待selectedView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>{varcurrentPage=(MainPage)((Frame)Window.Current.Content).Content;Window.Current.Activate();currentPage.OpenFileActivated(args);});selectedView.StopViewInUse();}}else{框架rootFrame=newFrame();rootFrame.Navigate(typeof(MainPage),args);Window.Current.Content=rootFrame;窗口.Current.Activate();已经启动=真;}}部分voidConstruct();部分无效OverrideOnLaunched(LaunchActivatedEventArgsargs,refboolhandled);部分无效InitializeRootFrame(框架框架);partialvoidOverrideOnLaunched(LaunchActivatedEventArgsargs,refboolhandled){//检查如果应该显示辅助视图ViewLifetimeControlViewLifetimeControl;handled=TryFindViewLifetimeControlForViewId(args.CurrentlyShownApplicationViewId,outViewLifetimeControl);如果(已处理){vartask=ViewLifetimeControl.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>{Window.Current.Activate();});}}partialvoidInitializeRootFrame(Frameframe){mainDispatcher=Window.Current.Dispatcher;mainViewId=ApplicationView.GetForCurrentView().Id;}boolTryFindViewLifetimeControlForViewId(intviewId,outViewLifetimeControlfoundData){foreach(varViewLifetimeControlinSecondaryViews){if(ViewLifetimeControl.Id==viewId){foundData=ViewLifetimeControl;返回真;}}foundData=null;返回假;}privateasyncTaskcreateMainPageAsync(){ViewLifetimeControlviewControl=null;awaitCoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>{//使用这个对象跨线程跟踪视图和重要//有关这些视图内容的详细信息//在您的应用程序中,您可能希望跟踪信息//例如该窗口内打开的文档或页面viewControl=ViewLifetimeControl.CreateForCurrentView();viewControl.Title=DateTime.Now.ToString();//增加引用计数,因为我们刚刚创建了视图并且我们有一个对它的引用viewControl.StartViewInUse();varframe=newFrame();frame.Navigate(typeof(MainPage),viewControl);Window.Current.Content=框架;//这是对8.1的更改:为了稍后显示视图,需要激活它。窗口.Current.Activate();//ApplicationView.GetForCurrentView().Title=viewControl.Title;});((App)App.Current).SecondaryViews.Add(viewControl);返回视图控件;}voidOnNavigationFailed(objectsender,NavigationFailedEventArgse){thrownewException("FailedtoloadPage"+e.SourcePageType.FullName);}privatevoidOnSuspending(objectsender,SuspendingEventArgse){vardeferral=e.SuspendingOperation.GetDeferral();//TODO:保存应用程序状态并停止任何后台活动deferral.Complete();}//我从MainPage调用这个函数。xaml.cs尝试打开一个新窗口publicasyncvoidLoadNewView(){varselectedView=awaitcreateMainPageAsync();if(null!=selectedView){selectedView.StartViewInUse();varviewShown=awaitApplicationViewSwitcher.TryShowAsStandaloneAsync(s,ViewSizePreference.Default,ApplicationView.GetForCurrentView().Id,ViewSizePreference.Default);等待selectedView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>{varcurrentPage=(MainPage)((Frame)Window.Current.Content).Content;Window.Current.Activate();currentPage.LoadNewFile();});selectedView.StopViewInUse();我用来尝试从MainPage.xaml.cs启动新视图/窗口的代码:((App)App.Current).LoadNewView();我一直在阅读Microsoft文档以尝试了解问题所在,但我仍然不明白多视图是如何工作的它是如何工作的,就像每次打开新视图/窗口时实例化App类一样,我非常感谢您的帮助。实际上,能够在主窗口关闭后打开一个新窗口的正确方法是使用TryShowAsStandaloneAsync提供的重载的TryShowAsStandaloneAsync。protectedoverrideasyncvoidOnLaunched(LaunchActivatedEventArgse){//创建newWindowId和东西...awaitApplicationViewSwitcher.TryShowAsStandaloneAsync(newWindowId,ViewSizePreference.Default,e.CurrentlyShownApplicationViewId,ViewSizePreference.Default基本上你需要指定第三个参数);anchorViewId调用(锚点)窗口的ID。在这种情况下,您只需传入e.CurrentlyShownApplicationViewId。我找到了解决问题的方法,实际上我决定不使用示例附带的ViewLifeTime控件。问题是,当主视图关闭时,您必须使用仍处于打开状态的其他视图中的Dispatcher.RunAsync()方法来运行线程这是我在App.xaml.cs中为任何感兴趣的人更改的代码:publicboolisMainViewClosed=false;publicObservableCollectionsecondaryViews=newObservableCollection();//...protectedoverrideasyncvoidOnLaunched(LaunchActivatedEventArgse){FramerootFrame=Window.Current.ContentasFrame;if(rootFrame==null){rootFrame=newFrame();rootFrame.NavigationFailed+=OnNavigationFailed;if(e.PreviousExecutionState==ApplicationExecutionState.Terminated){//TODO:从先前挂起的应用程序加载状态}Window.Current.Content=rootFrame;}if(rootFrame.Content==null){alreadyLaunched=true;rootFrame.Navigate(typeof(MainPage),e.Arguments);}elseif(alreadyLaunched){//如果主视图关闭,使用仍然打开的视图之一的线程if(isMainViewClosed){intnewViewId=0;等待secondaryViews[0].Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>{varcurrentPage=(MainPage)((Frame)Window.Current.Content).Content;窗口.Current.Activate();当前页面.NewWindow();newViewId=ApplicationView.GetForCurrentView().Id;});boolviewShown=awaitApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);}else{CoreApplicationViewnewView=CoreApplication.CreateNewView();intnewViewId=0;等待newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>{Frameframe=newFrame();frame.Navigate(typeof(MainPage),null);Window.Current.Content=frame;varcurrentPage=(MainPage)((Frame)Window.Current.Content).Content;Window.Current.Activate();secondaryViews.Add(CoreApplication.GetCurrentView());newViewId=ApplicationView.GetForCurrentView().Id;});boolviewShown=awaitApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);}}Window.Current.Activate();}不要查看(你的)lifetime...干杯,以上就是C#学习教程:多视图UWP问题分享的全部内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注——intidCreate=0;列表idSaved=n新列表();protectedoverrideasyncvoidOnLaunched(LaunchActivatedEventArgse){FramerootFrame=Window.Current.ContentasFrame;if(rootFrame==null){rootFrame=newFrame();rootFrame.NavigationFailed+=OnNavigationFailed;Window.Current.Content=rootFrame;}if(rootFrame.Content==null){rootFrame.Navigate(typeof(MainPage),e.Arguments);idSaved.Add(ApplicationView.GetForCurrentView().Id);}else{varcreate=CoreApplication.CreateNewView();等待create.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=>{varframe=newFrame();frame.Navigate(typeof(MainPage),e.Arguments);Window.Current.Content=frame;Window.Current.Activate();idCreate=ApplicationView.GetForCurrentView().Id;});for(inti=idSaved.Count-1;i>=0;i--)if(awaitApplicationViewSwitcher.TryShowAsStandaloneAsync(idCreate,ViewSizePreference.UseMinimum,idSaved[i],ViewSizePreference.UseMinimum))中断;idSaved.Add(idCreate);}Window.Current.Ac激发();}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
