从后台任务中执行程序

本文关键字:执行程序 后台任务 | 更新日期: 2023-09-27 18:29:34

我做了一个后台任务,让它监听NetworkStateChange,效果很好。

我正试图通过Windows.System.Launcher.LaunchUriAsync启动一个程序,但尝试时出现访问被拒绝异常:

这是任务的来源:

namespace Tasks
{
    public sealed class Upload : IBackgroundTask
    {
        BackgroundTaskDeferral _deferral = null;
        private bool wifiConnected = false;
        public bool WiFiConnected
        {
            get
            {
                return wifiConnected;
            }
            set
            {
                if (value == wifiConnected)
                    return;
                wifiConnected = value;
            }
        }

        public void Run(IBackgroundTaskInstance taskInstance)
        {
            var InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
            WiFiConnected = InternetConnectionProfile != null;
            if (!WiFiConnected)
            {
                LaunchFlexCheck();
                return;
            }

            _deferral = taskInstance.GetDeferral();
            _deferral.Complete();
        }
        private async void LaunchFlexCheck()
        {
            Debug.WriteLine("BEFORE LaunchFlexCheck()");
            bool result = false;
            try
            {
                result = Windows.System.Launcher.LaunchUriAsync(new System.Uri("myapp:MainPage")).GetResults();
            }
            catch (Exception e)
            {
                result = false;
                Debug.WriteLine("LaunchFlexCheck exception:" +  e.Message );
            }
            Debug.WriteLine("LaunchFlexCheck succes:" + (result ? "true" : "false"));
            Debug.WriteLine("AFTER LaunchFlexCheck()");
        }
    }
}

有可能这么做吗?

从后台任务中执行程序

从后台任务启动应用程序意味着用户在执行任务的过程中会被打断,以启动他尚未请求的应用程序。这与Windows Phone的设计准则完全矛盾,在该准则中,用户必须始终保持控制。因此,我相信这是不可能的。