2017-06-07 97 views
2

我正在使用輔助視圖來運行我的媒體文件,但是當我用關閉按鈕關閉輔助視圖時(媒體仍在播放時)輔助視圖/窗口關閉,但媒體以某種方式保持關閉因爲我可以聽到聲音和聲音來源似乎是主視圖(主應用程序窗口)。如何關閉時如何完全終止輔助窗口?uwp完全關閉輔助視圖

這裏是我的代碼來創建輔助視圖。

await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
     { 
      var frame = new Frame(); 
      frame.MinHeight = 200; 
      frame.MinWidth = 200; 
      compactViewId = ApplicationView.GetForCurrentView().Id; 
      frame.Navigate(typeof(CompactNowPlayingPage), caption); 
      Window.Current.Content = frame; 
      Window.Current.Activate(); 
      ApplicationView.GetForCurrentView().Title = Title; 

     }); 
     bool viewShown = await ApplicationViewSwitcher.TryShowAsViewModeAsync(compactViewId, ApplicationViewMode.Default); 

UPDATE

經過一番調試我特地爲你們知道,壓在二級視圖關閉按鈕只隱藏的看法,但它保持在它的線程上運行,我只是想要一個關閉按鈕完全關閉次要視圖,關閉它的線程並整個破壞窗口。

更新2

我跟着窗戶樣本的多個視圖,並能夠完成所有步驟,代碼運行正常,直到它到達Windows.Current.Close()在公佈事件。

然後,當它在發佈的事件中嘗試「Window.Current.Close()」時會發出異常。根據文檔異常發生由於任何正在進行的更改(這可能是因爲媒體文件播放),但即使在播放媒體文件時,我也需要強制關閉窗口,我該怎麼做?順便說一句,這裏是例外:

消息=「已經從其基礎RCW分離的COM對象不能使用。」

更新3

這是最新的更新,我不是現在以下官方樣片,只是下面簡單的方法了。

代碼來打開次要視圖

await Helpers.DeviceTypeHelper.CompactOpen(e.ClickedItem as Video, identifier); //where identified is just a string for some custom logic in the secondary view. 

//following method is located in a helper class within the project 
internal static async Task CompactOpen(Video PlayingVideo, string caption) 
{ 
    ApplicationView newView = null; 
    await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() => 
    { 
     var frame = new Frame(); 
     frame.Navigate(typeof(CompactNowPlayingPage),new object[] { PlayingVideo,caption}); 
     Window.Current.Content = frame; 
     Window.Current.Activate(); 
     newView = ApplicationView.GetForCurrentView(); 
     newView.Title = PlayingVideo.MyVideoFile.DisplayName; 
    }); 

    await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newView.Id); 

} 

次視圖

public sealed partial class CompactNowPlayingPage : Page 
{ 
    public CompactNowPlayingViewModel ViewModel { get; } = new CompactNowPlayingViewModel(); 
    private CustomMediaTransportControls controls; 
    public CompactNowPlayingPage() 
    { 
     InitializeComponent(); 
     this.Loaded += MediaPage_Loaded; 
     this.Unloaded += MediaPage_Unloaded; 
     Microsoft.Toolkit.Uwp.UI.Extensions.ApplicationView.SetExtendViewIntoTitleBar(this, true); 
     Microsoft.Toolkit.Uwp.UI.Extensions.TitleBar.SetButtonBackgroundColor(this, Colors.Transparent); 
    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     string chk = ""; 
     var paramm = e.Parameter as object[]; 
     NowPlayingVideo = paramm[0] as Video; 
     var vis = Visibility.Collapsed; 
     chk = paramm[1].ToString(); 
     switch (chk) 
     { 
      case "library": 
       vis = Visibility.Visible; 
       break; 
      case "playlist": 
       vis = Visibility.Visible; 
       break; 
      case "history": 
       vis = Visibility.Collapsed; 
       break; 
      case "directplay": 
       vis = Visibility.Collapsed; 
       break; 
      default: 
       break; 
     } 
     controls = new CustomMediaTransportControls(NowPlayingVideo,vis); 
     Media.TransportControls = controls; 
     PlayVideo(); 
    } 
    private Video NowPlayingVideo { get; set; } 
    private void PlayVideo() 
    { 
     if (NowPlayingVideo != null) 
     { 
      string token = ""; 
      if (StorageApplicationPermissions.FutureAccessList.Entries.Count == 800) 
      { 
       var en = StorageApplicationPermissions.FutureAccessList.Entries; 
       StorageApplicationPermissions.FutureAccessList.Remove(en.Last().Token); 
      } 
      token = StorageApplicationPermissions.FutureAccessList.Add(NowPlayingVideo.MyVideoFile); 
      Media.Source = null; 
      Media.Source = $"winrt://{token}"; 
      SetViews(); 
     } 
    } 

    private void SetViews() 
    { 
     NowPlayingVideo.Views++; 
     Database.DbHelper.UpdateViews(NowPlayingVideo.MyVideoFile.Path); 
    } 
    private void MediaPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().Consolidated += MediaPage_Consolidated; 
    } 

    private void MediaPage_Unloaded(object sender, RoutedEventArgs e) 
    { 
     Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().Consolidated -= MediaPage_Consolidated; 
    } 

    private void MediaPage_Consolidated(Windows.UI.ViewManagement.ApplicationView sender, Windows.UI.ViewManagement.ApplicationViewConsolidatedEventArgs args) 
    { 
     Window.Current.Close(); 
    } 

} 

次視圖XAML

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <vlc:MediaElement AreTransportControlsEnabled="True" 
         Name="Media"      
         HardwareAcceleration="True" 
         AutoPlay="True"> 
    </vlc:MediaElement> 
</Grid> 

案例1:一切都運行完美的,如果我把在Assets文件夾中的視頻文件,並把它作爲一個源到媒體元素和評論二級頁面的整個OnanvigatdTo方法。我也能夠成功關閉窗口。 ...

案例2:但是當我試圖通過NowPlayingVideo對象設置媒體如在上面的代碼,我也用默認的傳輸控制,所以我不會評論用於指定自定義運輸線路在上面的代碼運行正常,但控制當我再嘗試關閉我得到以下異常的窗口App.igcs文件,但堆棧跟蹤不存在:

消息=「已嘗試使用一個沒有支持類工廠的COM對象。「不能使用已從與其基礎RCW分開消息=「COM對象

**情況3:**酷似殼體2,但在這裏我去掉定製傳輸控制線,從而現在我分配自定義傳輸進行控制以我的媒體元素,此時的例外是某些堆棧跟蹤有點不同以及

堆棧跟蹤=「在System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(對象objSrc,IntPtr的pCPCMD,IntPtr的& ppTarget個)\ r \ n在Windows.UI.Xaml.DependencyObject.get_Dispatcher()\ r \ n在VLC.MediaElement.d__160.MoveNext()\ r \ n ---堆棧跟蹤結束...

Message =「嘗試使用沒有備份類工廠的COM對象。」

回答

2

簡短的回答是:您需要確保nothings保留在您的視圖實例上,並在視圖的Consolidated事件中調用Window.Close。官方樣本中的代碼較長的答案在這裏。看看在ViewLifetimeControl.cs源文件:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/MultipleViews/cs

+0

視圖對象犯規甚至有一個綜合的事件:/ – touseef

+0

ApplicationView.GetForCurrentView有,但CoreApplicationView可是沒有它 – touseef

+0

是的,你聽的ApplicationView實例的合併事件:https://docs.microsoft.com/en-us/uwp/api/Windows.UI.ViewManagement.ApplicationView#events_ –