1

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. the code runs fine until it reaches Windows.Current.Close() in released event of the secondary view.

Then it gives an exception when it tries "Window.Current.Close()" with in the released event. 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."

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.

Thanks in advance, any help would be really appreciated, I only want to force close the secondary view ( If that's possible )

Muhammad Touseef
  • 4,357
  • 4
  • 31
  • 75

1 Answers1

0

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. It may not work, I'm not used to this kind of thing.

Rohan Shetty
  • 162
  • 1
  • 13