我创建了一个测试应用程序,我可以让我的应用程序从 toast 通知启动,但我无法让它与我的真实应用程序一起工作???
我的 MainPage 有一个框架,可以导航到具有 2 个框架的页面。我希望第二个框架在单击 Toast 通知时加载某个页面。
这是我的 OnLaunched 代码
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
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();
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
// Otherwise an action is provided
if (e.Arguments != "")
{
rootFrame.Navigate(typeof(MainPage));
FramePage.Instance.setSecondFrame("Email");
}
else
{
if (rootFrame.Content == null)
rootFrame.Navigate(typeof(MainPage));
}
// Ensure the current window is active
Window.Current.Activate();
它在 OnActivated 中工作得很好。有什么想法吗??
请您参考如下方法:
从您发布的代码来看,您似乎正在使用 OnLaunched 方法处理来自 toast 通知的激活。然而
In Windows 10, Microsoft has updated the toast activation behavior so that when a toast (or an action inside toast) triggers a foreground activation, OnActivated is invoked instead of OnLaunched, with a new activation kind – ToastNotification. Thus, we are able to easily distinguish a toast activation and perform tasks accordingly.
在 Windows 10 中,随着自适应模板和自定义操作的添加,应用可能需要处理 3 种不同类型的激活。
- 使用 Windows 10 自适应模板从 toast 通知激活前台;
- 使用 Windows 10 自适应模板从 toast 通知进行后台激活;
- 旧版:使用旧版模板从 Toast 通知激活前台。
对于第一种,我们应该通过覆盖OnActivated method来处理它。不是OnLaunched method .只有在使用遗留模板时,您才需要使用 OnLaunched 方法来处理激活。但如果您正在开发新的 Windows 10 通用应用程序,则强烈建议使用新的自适应模板。
All legacy toast templates can be easily achieved by using the new adaptive template. By using the new adaptive template, you can have a consistent way and place to handle toast activation.
更多信息,请参阅Send a local toast notification from UWP apps: Handling activation .还有 code sample on GitHub .
