首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UWP基础教程 - 重启应用

UWP基础教程 - 重启应用

作者头像
陈仁松
发布2018-03-20 16:36:06
1.2K0
发布2018-03-20 16:36:06
举报
文章被收录于专栏:陈仁松博客陈仁松博客

开发过Winform应用的同学应该都知道,会有一些场景需要将应用重启,如:重新应用配置,崩溃,内存泄漏等情况。这个时候我们只要使用一行代码就可以实现重启。

Application.Restart()

但在之前的Windows10 UWP应用开发中,我们只能提示用户手动重启,这个问题一直很困恼开发者。

在16226版本之后,微软终于实现了这个API。

AppRestartFailureReason result = await CoreApplication.RequestRestartAsync(String.Empty);
if (result == AppRestartFailureReason.NotInForeground || 
    result == AppRestartFailureReason.RestartPending ||
    result == AppRestartFailureReason.Other)
{
    Debug.WriteLine("RequestRestartAsync failed: {0}", result);
}

使用上面的代码就能实现UWP APP重启的功能,这个API还可以自定义启动参数,只要将上述代码String.Empty部分传入对应的参数,在OnActivated事件处进行处理即可。

protected override void OnActivated(IActivatedEventArgs args)
{
    switch (args.Kind)
    {
        case ActivationKind.Launch:
            LaunchActivatedEventArgs launchArgs = args as LaunchActivatedEventArgs;
            string argString = launchArgs.Arguments;
 
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame == null)
            {
                rootFrame = new Frame();
                Window.Current.Content = rootFrame;
            }
            rootFrame.Navigate(typeof(MainPage), argString);
            Window.Current.Activate();
            break;
    }
}

更详细的内容,大家可以参考微软官方博客写的How to Restart your App Programmatically

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档