【问题标题】:Handle file opened from outside application to load WPF app?处理从外部应用程序打开的文件以加载 WPF 应用程序?
【发布时间】:2020-05-15 12:00:47
【问题描述】:

我的 WPF 应用程序具有可以打开它的 .xyz 文件(使用 WIX 安装程序),但是,在我的 WPF 应用程序中,我想以某种方式捕获它并调用双击文件的一些加载功能在应用程序启动之前从文件资源管理器中获取。

现在,如果您在文件资源管理器中双击相应的 xyz 文件,它会打开应用程序,但显然没有其他任何反应。我的 WPF 代码中是否有办法检测到这一点并调用传入文件路径/名称的必要函数?

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    Application 中有StartUp 事件,您可以在其中设置参数。 关注这个example

    【讨论】:

      【解决方案2】:

      您应该可以在App.xaml.cs 类中找到文件路径:

      public partial class App : Application
      {
          public static string FilePath { get; private set; }
          protected override void OnStartup(StartupEventArgs e)
          {
              base.OnStartup(e);
              if (e.Args != null && e.Args.Length > 0)
                  FilePath = e.Args[0];
          }
      }
      

      如果您存储在这样的静态属性中,您可以从应用中的任何类访问它:

      string filePath = App.FilePath;
      

      【讨论】:

      • 感谢 mm8。不明白为什么我一直被否决:(...
      猜你喜欢
      • 2015-11-26
      • 2017-02-15
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多