我正在开发一个UWP测试应用程序,需要添加协议激活。 激活逻辑写在类库中,然后将其作为参考添加到UWP测试应用程序中。 问题是如果我在测试App的OnActivated事件中编写逻辑它工作正常,但是当在类库中的函数中编写相同的逻辑并且从App.xaml.cs OnActivated事件中调用此函数时,则这个OnActivated事件被无限循环调用。
是否需要在OnActivated事件中创建新帧?
答案 0 :(得分:4)
当调用OnActivated时,您必须检查它是否已经初始化(因为未调用OnLaunched
) - 创建框架并激活Window,以防它不是。通常可以在OnLaunched
和OnActivated
事件之间共享此初始化代码。
protected override void OnActivated(IActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
// Do not repeat app initialization when the Window already has content
if (rootFrame == null)
{
// Create a Frame to act as the navigation context
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;
}
//
// Handle protocol activation here
//
// Ensure the current window is active
Window.Current.Activate();
}