设置UWP程序自启动(Automate launching Windows 10 UWP apps)

在开发UWP程序的过程中,有时候需要设置程序的自启。本人实现的步骤如下:

1.在VS中激活Protocol

 (Package.appxmanifest --> Declarations --> Add Protocol),图示如下:


2.编译并发布项目(Build and Deploy)

 发布之后Protocol被激活,在(控制面板 --> 程序 --> 默认程序 --> 设置关联)中可以找到MYAPPLICATION的关联。

3.命令行启动UWP程序

 打开命令行提示符,我们可以使用URI来启动,示例如下:

>start myapplication:                   //不带参数
>start myapplication:test-parameter     //带参数

 输入上述命令即可启动应用。

4.程序中添加重写方法

 在上述步骤完成后,虽然可以启动UWP应用程序,但是程序中要重写OnActivated方法来导航到需要显示的页面。

 在App.xaml.cs中重写OnActivated方法。

protected override void OnActivated(IActivatedEventArgs args)
{
    Frame rootFrame = Window.Current.Content as Frame;
    if (rootFrame == null)
    {
        rootFrame = new Frame();
        Window.Current.Content = rootFrame;
    }

    var protoclForResultsArgs = (ProtocolActivatedEventArgs)args;
    rootFrame.Navigate(typeof(MainPage), protoclForResultsArgs);
    Window.Current.Activate();          
}

5.使用Windows服务在命令行启动程序

 命令行实现URI程序启动后,可以编写Windows服务程序来监听某些条件,从而启动UWP程序。需要注意的点如下:

 (1).System.Diagnostics.Process可以实现程序中执行cmd命令;

 (2).Windows服务启动UWP程序,需要在ProjectInstaller中设置serviceProcessInstaller的Account属性为User,并指定相应的用户;

 (3).如何创建Windows服务的链接:

  https://msdn.microsoft.com/zh-cn/library/zt39148a(v=vs.110).aspx#BK_CreateProject

posted @ 2016-12-19 11:09  L_rocking  阅读(3189)  评论(5编辑  收藏  举报