【问题标题】:Wpf app not start up with windows when cinstall like uwp package当 cinstall 像 uwp 包时,Wpf 应用程序无法与 Windows 一起启动
【发布时间】:2019-01-26 17:17:50
【问题描述】:

我想用 Windows 启动我的 wpf 应用程序。但是当我像 uwp 应用程序一样安装它时它不起作用。

这是在运行时设置启动的代码:

static public void SetStartup()
        {
            DeleteStartup();
            RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            rk.SetValue(Global.GlobalConstant.APPLICATIONNAME, Directory.GetCurrentDirectory().ToString() + @"\" + Global.GlobalConstant.APPLICATIONNAME);
        }

        static public void DeleteStartup()
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            key.DeleteValue(Global.GlobalConstant.APPLICATIONNAME, false);
        }

        static public void SetStartLocation()
        {
            RegistryKey RegKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            if (RegKey.GetValue(GlobalConstant.APPLICATIONNAME) != null)
            {
                string Path = RegKey.GetValue(GlobalConstant.APPLICATIONNAME).ToString();
                Path = Path.Remove(Path.Length - GlobalConstant.APPLICATIONNAME.Length, GlobalConstant.APPLICATIONNAME.Length); // Now it's a valid directory.
                Directory.SetCurrentDirectory(Path);
            }
    }

但它不起作用,因为 uwp 应用程序会自行安装在特定文件夹中。 (C:\Program Files\WindowsApps)

有什么解决办法吗? 谢谢

【问题讨论】:

  • 如果您想将 WPF 应用程序安装为“UWP 应用程序”,您应该使用desktop bridge。我不确定您要对注册表做什么。
  • 这个 uwp 只在 pc 上可用,所以我想用 windows 启动我的应用程序。

标签: c# wpf windows uwp startup


【解决方案1】:

这个 uwp 只在 pc 上可用,所以我想用 windows 启动我的应用程序。

@mm8 的建议是正确的。对于常规 UWP 应用,Windows 10 Fall Creators Update(版本 1709)支持启动。

如果您的应用程序是经典的WPF应用程序,您需要使用Desktop Bridge来打包您的WPF应用程序,然后您可以在Package.appxmanifest中声明启动任务扩展。之后,当您为桌面应用程序生成包时,您可以将其安装为 UWP 应用程序。系统启动时会启动。

详情请见StartupTask Class

请注意:桌面桥应用可以在清单中将其启动任务设置为已启用,在这种情况下,它们不需要调用 RequestEnableAsync。但是,用户必须至少启动一次 Desktop Bridge 应用程序,然后它才能在启动或用户登录时启动。

【讨论】:

  • 如何在 wpf 应用中使用 startupTask?
  • @GAMINGBEX 你可能听不懂我说什么。您首先需要通过“桌面桥”打包您的 wpf 应用程序。例如,您可以Package your WPF application by using Visual Studio (Desktop Bridge)。然后,您可以在该项目中看到一个Package.appxmanifest 文件。之后,您按照StartupTask Class 文档在此Package.appxmanifest 中添加启动任务扩展声明。
  • 这对你来说没问题,我做到了。但我不清楚如何在运行时设置启动?
  • @GAMINGBEX 您不需要在运行时设置启动。当您在 Package.appxmanifest 中声明启动任务扩展时,您将其安装在您的机器上并至少启动一次。然后下次系统启动时再启动。
  • 我希望那个用户可以设置它。
猜你喜欢
  • 2014-01-26
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 2016-10-15
  • 1970-01-01
相关资源
最近更新 更多