我正在使用辅助视图来运行我的媒体文件,但是当我使用关闭按钮关闭辅助视图时(当媒体仍在播放时),辅助视图/窗口会关闭,但媒体会以某种方式继续播放,因为我可以听到声音和声源似乎是主要视图(主应用程序窗口)。关闭辅助窗口时如何完全终止它?
这是我创建辅助视图的代码。
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);
更新
经过一些调试后,我知道在辅助视图上按下关闭按钮只会隐藏视图,但它会继续在其线程上运行,我只希望该关闭按钮完全关闭辅助视图,关闭其线程并销毁窗口作为一个整体。
更新 2
我遵循 windows 示例多个视图并能够完成所有步骤,代码运行良好,直到它到达Windows.Current.Close()
发布事件。
然后,当它在已发布事件中尝试“Window.Current.Close()”时会给出异常。根据文档,由于任何正在进行的更改(可能是因为正在播放媒体文件)而发生异常,但是即使正在播放媒体文件,我也需要强制关闭窗口我该怎么做?这是一个例外:
Message = "无法使用已与其基础 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文件中出现以下异常,但堆栈跟踪不存在:
Message = "已尝试使用没有支持类工厂的 COM 对象。" Message = "无法使用已与其基础 RCW 分离的 COM 对象。
案例 3:与案例 2 完全一样,但在这里我取消了自定义传输控件行的注释,所以现在我将自定义传输控件分配给我的媒体元素,这次异常与一些堆栈跟踪也有点不同
StackTrace = " 在 System.StubHelpers.StubHelpers.GetCOMIPFromRCW_WinRT(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget)\r\n 在 Windows.UI.Xaml.DependencyObject.get_Dispatcher()\r\n 在 VLC.MediaElement.d__160.MoveNext( )\r\n--- 堆栈跟踪结束 ...
Message = "已尝试使用没有支持类工厂的 COM 对象。"