gpt4 book ai didi

c# - 如何覆盖 App.cs 中的 OnFileActivated 事件以使 OpenWith 正常工作

转载 作者:太空狗 更新时间:2023-10-29 22:59:27 25 4
gpt4 key购买 nike

我正在开发这个 Windows 应用商店应用程序,我希望它在最终用户想要使用这个应用程序打开文件时支持打开方式,我将支持的格式添加到应用程序 list 文件中的声明部分,如下图所示显示:

enter image description here

然后我覆盖了 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 在一个文件上,然后应用程序不能完全显示启动画面时,我现在有这个消息框错误

enter image description here

更新 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 正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21143474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com