简体   繁体   English

UWP多个视图未关闭

[英]UWP multiple views not closing

PROBLEM 问题

I am using a secondary view to run my media files, but When I close my secondary view with close button on it ( while media is still playing ) the secondary view/window closes but the media somehow keeps playing because I can hear the sound and source of sound seems to be the primary view ( main app window ). 我正在使用辅助视图来运行我的媒体文件,但是当我关闭我的辅助视图并关闭它时(媒体仍在播放时),辅助视图/窗口关闭,但媒体以某种方式继续播放,因为我可以听到声音和声源似乎是主要视图(主应用程序窗口)。 how can I completely terminate the secondary window when I close it? 当我关闭它时,如何完全终止辅助窗口?

TRIED 受审

I followed windows samples multiple views and was able to complete all steps, I copied the ViewLifetimeControl.cs file from the sample and used it in my project. 我跟着windows样本多个视图并且能够完成所有步骤,我从示例中复制了ViewLifetimeControl.cs文件并在我的项目中使用它。 the code runs fine until it reaches Windows.Current.Close() in released event of the secondary view. 代码运行正常,直到它在次要视图的已发布事件中到达Windows.Current.Close()

Then it gives an exception when it tries " Window.Current.Close()" with in the released event. 然后它在发布的事件中尝试“ Window.Current.Close()”时给出异常 according to documentation exception occurs due to any on going changes ( which might be because of media file playing ), but I need to force close the window even when media file is playing how can I do that? 根据文档异常发生由于任何正在进行的更改(可能是因为媒体文件播放),但我需要强制关闭窗口,即使媒体文件正在播放我该怎么办? btw here is the exception : 顺便说一下这是例外:

Message = "COM object that has been separated from its underlying RCW cannot be used." Message =“无法使用已与其基础RCW分离的COM对象。”

Code to Create and Show secondary view 用于创建和显示辅助视图的代码

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)
        }
    }           

}

Released Event 发布的活动

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
}

Note : both of above methods and even all the related variables, all of them I have followed the guidelines within the uwp sample for multiple views. 注意:上述两种方法甚至所有相关变量,所有这些我都遵循uwp示例中的多个视图的指导原则。

Thanks in advance, any help would be really appreciated, I only want to force close the secondary view ( If that's possible ) 在此先感谢,任何帮助将非常感激,我只想强制关闭次要视图(如果可能的话)

Is this in the editor or the app? 这是在编辑器或应用程序中? If it's in your debug or build of the app, the secondary view is most likely still open but hidden. 如果它在您的调试或构建应用程序中,则辅助视图很可能仍然是打开但隐藏的。 You may be using a custom close button which doesn't perform its job well enough. 您可能正在使用自定义关闭按钮,该按钮不能很好地执行其工作。 Instead of putting down SecondaryViews.Remove you should do what you had originally written and try StopViewInUse . 而不是放下SecondaryViews.Remove你应该做你最初写的,并尝试StopViewInUse It may not work, I'm not used to this kind of thing. 它可能不起作用,我不习惯这种事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM