c# – 如何在App.cs中覆盖OnFileActivated事件以使OpenWith正常工作
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何在App.cs中覆盖OnFileActivated事件以使OpenWith正常工作,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3651字,纯文字阅读大概需要6分钟。
内容图文
![c# – 如何在App.cs中覆盖OnFileActivated事件以使OpenWith正常工作](/upload/InfoBanner/zyjiaocheng/827/a885f37e71d9458ba91f7db6b41e6527.jpg)
我正在使用这个Windows应用商店应用程序,我希望它支持Open With当最终用户想要使用此应用程序打开文件时,我将支持的格式添加到应用程序清单文件中的声明部分,如下图所示:
我在App.xaml.cs文件中覆盖了OnFileActivated事件,如下所示:
protected override void OnFileActivated(FileActivatedEventArgs args)
{
if (args.Files.Count > 1)
{
MessageDialog messageDialog1 = new MessageDialog("DirectTouchPlayer can't open many files, the first file will be opened.");
messageDialog1.ShowAsync();
}
OpenedMediaFile = (StorageFile)args.Files.First();
MessageDialog messageDialog2 = new MessageDialog(OpenedMediaFile.Path + " " + " : Opened !");
messageDialog2.ShowAsync();
}
当我在.MP4文件上执行OpenWith时,此事件似乎没有触发,应用程序午餐却无法退出启动画面,我试图像这样调试事件thread
解释但调试器不会停止在OnFileActivated方法中标记的任何断点处,我感谢您的帮助.
更新1:
当OpenWith在一个文件上,然后该应用程序不能完全启动画面,我现在有这个消息框错误
更新2:
我将我的OnFileActivated事件处理程序更改为:
protected override async void OnFileActivated(FileActivatedEventArgs args)
{
var loadMediaFileTask = new Task<IStorageItem>(() =>
{
return args.Files.First();
});
OpenedFile = await loadMediaFileTask;
}
其中OpenedFile是:
public static IStorageItem OpenedFile { get; set; }
我通过OnLaunched事件中的导航作为参数传递:
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(PlayerPageView), OpenedFile);
}
而PlayerPageView是将在MediaElement中使用视频的页面
所有这些更新也不起作用,我不明白我应该做什么,如果你向我澄清事情,我会很高兴.
解决方法:
OnLaunched可能不会被调用,因为这是在用户正常打开应用程序时调用的方法.您需要在OnFileActivated方法内部的OnLaunched中执行大部分相同的逻辑.
他们提供的样本有关于OnLaunched的说明:
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used when the application is launched to open a specific file, to display
/// search results, and so forth.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
这是sample version that Microsoft provides in their Windows 8.1 App Sample for Launching by Association:
/// <summary>
// Handle file activations.
/// </summary>
protected override async void OnFileActivated(FileActivatedEventArgs args)
{
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
// Associate the frame with a SuspensionManager key
SuspensionManager.RegisterFrame(rootFrame, "AppFrame");
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
// Restore the saved session state only when appropriate
try
{
await SuspensionManager.RestoreAsync();
}
catch (SuspensionManagerException)
{
//Something went wrong restoring state.
//Assume there is no state and continue
}
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
if (!rootFrame.Navigate(typeof(MainPage)))
{
throw new Exception("Failed to create initial page");
}
}
var p = rootFrame.Content as MainPage;
p.FileEvent = args;
p.ProtocolEvent = null;
p.NavigateToFilePage();
// Ensure the current window is active
Window.Current.Activate();
}
我建议尽可能多地模仿这个.
另外需要注意的是,我建议不要在Activated事件中弹出MessageDialogs.当您确定UI线程是免费的时,保存到您到达的页面(甚至是ExtendedSplash屏幕).
希望这有助于编码!
内容总结
以上是互联网集市为您收集整理的c# – 如何在App.cs中覆盖OnFileActivated事件以使OpenWith正常工作全部内容,希望文章能够帮你解决c# – 如何在App.cs中覆盖OnFileActivated事件以使OpenWith正常工作所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。