我做了一个后台任务,让它监听 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()");
}
}
}
有可能这样做吗?