创建一个metro风格的应用程序并将其添加到打开上下文菜单

时间:2012-11-05 17:48:51

标签: c# windows-runtime windows-store-apps winrt-xaml .net-4.5

我愿意创建类似于Windows Photo Viewer的metro风格应用。

为此,我需要在Openwith上下文菜单中添加我的应用程序。此外,在Windows资源管理器中选择的图像文件应该在我的照片查看器中打开。如何使用所选图像文件作为参数打开应用程序?

提前致谢


非常感谢@Filip

我在OnFileActivated(FileActivatedEventArgs args)

中的App.xaml.cs中写了以下代码
protected override void OnFileActivated(FileActivatedEventArgs args)
        {
            // TODO: Handle file activation

            // The number of files received is args.Files.Size
            // The first file is args.Files[0].Name
            base.OnFileActivated(args);
            StorageFile file = args.Files[0] as StorageFile;

            Frame rootFrame = Window.Current.Content as Frame;

            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();
                //rootFrame.SourcePageType = typeof(ImageViewer);

                if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    //TODO: Load state from previously suspended application
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

            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
                if (!rootFrame.Navigate(typeof(ImageViewer), file))
                {
                    throw new Exception("Failed to create initial page");
                }
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }

但是,我的应用程序在打开时没有显示任何内容。那么根据你可能是什么缺点?

1 个答案:

答案 0 :(得分:0)

如果您编辑Package.appxmanifest以在清单编辑器的声明选项卡中添加文件类型关联 - 您的应用程序将成为可以打开您指定的文件类型的应用程序之一。然后,您只需要向App类添加覆盖以处理文件激活,如this article中所述:

protected override void OnFileActivated(FileActivatedEventArgs args)
{
   // TODO: Handle file activation

   // The number of files received is args.Files.Size
   // The first file is args.Files[0].Name
}