1

问题

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

尝试过

我遵循 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 示例中的指南进行多个视图。

在此先感谢,任何帮助将不胜感激,我只想强制关闭辅助视图(如果可能的话)

4

1 回答 1

0

这是在编辑器还是应用程序中?如果它在您的应用程序的调试或构建中,则辅助视图很可能仍处于打开状态但处于隐藏状态。您可能正在使用自定义关闭按钮,但它的工作效果不够好。而不是放下SecondaryViews.Remove你应该做你最初写的并尝试StopViewInUse。它可能不起作用,我不习惯这种事情。

于 2017-06-10T02:28:38.673 回答