UWP 多个视图未关闭
Posted
技术标签:
【中文标题】UWP 多个视图未关闭【英文标题】:UWP multiple views not closing 【发布时间】:2017-06-09 08:13:54 【问题描述】:问题
我正在使用 次要视图 来运行我的媒体文件,但是当我使用 关闭按钮 关闭我的次要视图时(当媒体仍在播放时)次要视图/窗口关闭,但媒体以某种方式继续播放,因为我可以听到声音并且声音来源似乎是主要视图(主应用程序窗口)。如何在关闭辅助窗口时完全终止?
尝试过
我遵循 windows 示例多个视图并能够完成所有步骤,我从示例中复制了 ViewLifetimeControl.cs 文件并在我的项目中使用它。代码运行良好,直到在辅助视图的 released 事件中到达 Windows.Current.Close()。
然后,当它在已发布的事件中尝试“Window.Current.Close()” 时,它会给出一个异常。根据文档,由于任何正在进行的更改(可能是因为正在播放媒体文件)而发生异常,但即使在播放媒体文件时我也需要强制关闭窗口我该怎么做?顺便说一句,这里是个例外:
Message = "无法使用已与其底层 RCW 分离的 COM 对象。"
创建和显示辅助视图的代码
internal static async Task CompactOpen(string Title, string caption)
ViewLifetimeControl viewControl = null;
await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
viewControl = ViewLifetimeControl.CreateForCurrentView();
viewControl.Title = Title;
viewControl.StartViewInUse();
var frame = new Frame();
frame.MinHeight = 200;
frame.MinWidth = 200;
frame.Navigate(typeof(CompactNowPlayingPage), new object[] viewControl,caption);
Window.Current.Content = frame;
Window.Current.Activate();
ApplicationView.GetForCurrentView().Title = viewControl.Title;
);
((App)App.Current).SecondaryViews.Add(viewControl);
var selectedView = viewControl;
var sizePreference = new SizePreferenceString() Title = "SizePreference", Preference = ViewSizePreference.Default ;
var anchorSizePreference = new SizePreferenceString() Title = "AnchorSizePreference", Preference = ViewSizePreference.Default ;
if (selectedView != null && sizePreference != null && anchorSizePreference != null)
try
selectedView.StartViewInUse();
var viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(
selectedView.Id,
sizePreference.Preference,
ApplicationView.GetForCurrentView().Id,
anchorSizePreference.Preference);
if (!viewShown)
// The window wasn't actually shown, so release the reference to it
// This may trigger the window to be destroyed
// Signal that switching has completed and let the view close
selectedView.StopViewInUse();
catch (InvalidOperationException)
// The view could be in the process of closing, and
// this thread just hasn't updated. As part of being closed,
// this thread will be informed to clean up its list of
// views (see SecondaryViewPage.xaml.cs)
发布事件
private async void ViewLifetimeControl_Released(Object sender, EventArgs e)
((ViewLifetimeControl)sender).Released -= ViewLifetimeControl_Released;
// The ViewLifetimeControl object is bound to UI elements on the main thread
// So, the object must be removed from that thread
await mainDispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
((App)App.Current).SecondaryViews.Remove(thisViewControl);
);
// The released event is fired on the thread of the window
// it pertains to.
//
// It's important to make sure no work is scheduled on this thread
// after it starts to close (no data binding changes, no changes to
// XAML, creating new objects in destructors, etc.) since
// that will throw exceptions
Window.Current.Close(); //this is where that exception occurs
注意:以上两种方法,甚至所有相关变量,我都遵循 uwp 示例中的指南进行多个视图。
提前致谢,任何帮助将不胜感激,我只想强制关闭辅助视图(如果可能的话)
【问题讨论】:
你不能在你打电话给Window.Current.Close()
之前关闭媒体文件吗?
【参考方案1】:
这是在编辑器还是应用程序中?如果它在您的应用程序的调试或构建中,则辅助视图很可能仍处于打开状态但处于隐藏状态。您可能正在使用自定义关闭按钮,但它的工作性能不够好。而不是放下SecondaryViews.Remove
,你应该做你最初写的并尝试StopViewInUse
。可能不行,我不习惯这种事。
【讨论】:
是的,我也在使用 sTopViewInUse,因为我正在关注多个视图的官方示例以上是关于UWP 多个视图未关闭的主要内容,如果未能解决你的问题,请参考以下文章