UWP多个视图未关闭

时间:2017-06-09 08:13:55

标签: c# multithreading xaml uwp multiple-views

问题

我正在使用辅助视图来运行我的媒体文件,但当我使用关闭按钮关闭我的辅助视图时(媒体仍在播放时)视图/窗口关闭,但媒体以某种方式继续播放,因为我可以听到声音和声源似乎是主视图(主应用程序窗口)。当我关闭辅助窗口时,如何完全终止

受审

我跟踪了Windows样本的多个视图,并且能够完成所有步骤,我从示例中复制了 ViewLifetimeControl.cs 文件并在我的项目中使用它。代码运行正常,直到它在次要视图的已发布事件中到达 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示例中的指导原则来查看多个视图。

在此先感谢,任何帮助都会非常感激,我只想强制关闭次要视图(如果可能的话)

1 个答案:

答案 0 :(得分:0)

这是在编辑器还是应用程序中?如果它在您的应用程序的调试或构建中,则辅助视图很可能仍然是打开但隐藏的。您可能正在使用自定义关闭按钮,该按钮不能很好地执行其工作。不要放下SecondaryViews.Remove,而应该按照原来的标准进行操作,然后尝试StopViewInUse。它可能不起作用,我不习惯这种事情。