【发布时间】:2015-01-15 17:53:06
【问题描述】:
我的应用允许固定辅助磁贴,然后可用于启动应用。 我试图实现的行为是这样的:-
- 如果在单击辅助磁贴时应用程序已经在运行,它会启动一个 URI 并让应用程序继续运行
- 如果单击辅助磁贴时应用程序未运行,它会启动 URI,然后调用 Exit() 方法退出应用程序(这样用户就不会在应用程序切换视图中看到额外的应用程序)。
我用来在 app.xaml.cs 中的 OnLaunched() 中执行此操作的代码,如下所示。
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
{
this.DebugSettings.EnableFrameRateCounter = true;
}
#endif
// Check to see if app has been launched from a secondary tile
if (e.Kind==ActivationKind.Launch && e.TileId != "App")
{
//If so then launch Uri passed from tile
var uri = new Uri(e.Arguments);
Windows.System.Launcher.LaunchUriAsync(uri);
//If launched from a secondary tile, then close the app if it wasn't already running
if (e.PreviousExecutionState == ApplicationExecutionState.NotRunning)
{
Exit();
}
}
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
// TODO: change this value to a cache size that is appropriate for your application
rootFrame.CacheSize = 1;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
// TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
// Removes the turnstile navigation for startup.
if (rootFrame.ContentTransitions != null)
{
this.transitions = new TransitionCollection();
foreach (var c in rootFrame.ContentTransitions)
{
this.transitions.Add(c);
}
}
rootFrame.ContentTransitions = null;
rootFrame.Navigated += this.RootFrame_FirstNavigated;
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
if (!rootFrame.Navigate(typeof(SignInPage), e.Arguments))
{
throw new Exception("Failed to create initial page");
}
}
// Ensure the current window is active
Window.Current.Activate();
}
当我在模拟器中运行它时,它完全按照要求工作。 但是,当我在我的设备(诺基亚 925)上运行它时,从辅助磁贴启动时应用程序不会退出,因此任务切换器视图中总是会留下一个应用程序窗口,即使它之前没有运行。
编辑 我已经尝试在 OnLaunched() 中自己调用 Exit() 方法,它可以正确退出。我只能假设它是'if'检查来查看应用程序是否正在运行但它不起作用,因此没有调用 Exit()
if (e.PreviousExecutionState == ApplicationExecutionState.NotRunning)
知道发生了什么以及如何让手机像模拟器一样工作吗?
【问题讨论】:
-
我尝试在 Lumia 1520 上运行的应用程序的 OnLaunched 事件处理程序中调用 Exit() 方法,但应用程序确实退出了。
-
我尝试调用 Exit() 方法并且它有效。似乎是 if (e.PreviousExecutionState == ApplicationExecutionState.NotRunning) 逻辑不起作用......奇怪。
-
我很高兴我的评论有所帮助。
-
是的,感谢您提供的信息。我仍然有这个问题。知道为什么 if 语句中的 Exit() 在模拟器上运行正常但在手机上运行正常吗?我很困惑。
标签: c# windows-phone-8.1 windows-phone-8-emulator