C# 表单UWP-登录时启动应用程序
当用户登录到Windows时,我正在尝试启动我的应用程序。我在Package.appxmanifest中设置了相应的扩展StartupTask,我可以在登录到Windows时启动该应用程序,正如预期的那样。然而,该应用程序在显示启动屏幕大约一两秒钟后崩溃 在我的App.xaml.cs文件中,我覆盖了用户启动应用时调用的OnLaunched和Windows登录后系统启动应用时调用的OnActivated。两者调用相同的函数来初始化我的应用程序;但是,只有当应用程序从OnActivated函数初始化时,应用程序才会崩溃。从OnLaunched初始化时,它会按预期工作。以下是App.xaml.cs中的相关代码:C# 表单UWP-登录时启动应用程序,c#,xamarin,xamarin.forms,uwp,C#,Xamarin,Xamarin.forms,Uwp,当用户登录到Windows时,我正在尝试启动我的应用程序。我在Package.appxmanifest中设置了相应的扩展StartupTask,我可以在登录到Windows时启动该应用程序,正如预期的那样。然而,该应用程序在显示启动屏幕大约一两秒钟后崩溃 在我的App.xaml.cs文件中,我覆盖了用户启动应用时调用的OnLaunched和Windows登录后系统启动应用时调用的OnActivated。两者调用相同的函数来初始化我的应用程序;但是,只有当应用程序从OnActivated函数初始化
问题是,您试图强制转换IActivatedEventArgs以启动ActivatedEventArgs,但当启动激活发生时,类型实际上是StartupTaskActivatedEventArgs
幸运的是,您实际上只需要Xamarin.Forms.Forms.Init的e参数,它实际上接受IActivatedEventArgs,因此,您只需将InitializeApp的参数更改为IActivatedEventArgs,并删除强制转换以在OnActivated中启动ActivatedEventArgs。它只是在没有任何其他操作的情况下崩溃还是引发异常?@Johannes,因为OnActivated函数仅在应用程序登录Windows后启动时调用,我还没有找到一种方法来调试它,看看到底发生了什么。虽然在逐行注释并注销windows/再次登录后,我发现它在Xamarin.Forms.Forms.Init调用中崩溃,MartinZikmund在下面提到了这一点,关于rootFrame.NavigatetypeofMainPage,e.Arguments;,IActivatedEventArgs接口没有属性参数,因此提供空字符串会导致任何问题吗?抱歉,我没有注意到这一点。如果不使用辅助平铺、命令行参数或toast通知参数,只需删除这些参数,然后使用带有一个参数的导航重载即可。如果决定使用其中一个参数,则始终可以向IIInitializeApp方法添加第二个参数,它要么包含这些参数,要么是空的。到目前为止,我没有使用这些函数中的任何一个。我更新了代码,现在没有崩溃,太棒了。这是一个为参数提供第二个参数的好主意,它应该完全解决这个问题。再次感谢它的帮助:-,快乐编码!
// Called when the user launches the app
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
// Calling InitializeApp here, the app launches without problem
InitializeApp(e);
}
// Called when the system launches the app after Windows login
protected override void OnActivated(IActivatedEventArgs e)
{
base.OnActivated(e);
// Calling InitializeApp here will cause the app to crash
InitializeApp((LaunchActivatedEventArgs)e);
}
// initialize the app
async void InitializeApp(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;
var assembliesToInclude = new List<Assembly>()
{
typeof(CachedImage).GetTypeInfo().Assembly,
typeof(CachedImageRenderer).GetTypeInfo().Assembly
};
Xamarin.Forms.Forms.Init(e, assembliesToInclude);
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}
if (rootFrame.Content == null)
{
// When the navigation stack isn't restored navigate to the first page,
// configuring the new page by passing required information as a navigation
// parameter
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
// Ensure the current window is active
Window.Current.Activate();
if (session == null)
{
// prevent the app from stopping when minimized
session = new ExtendedExecutionSession();
session.Reason = ExtendedExecutionReason.Unspecified;
session.Revoked += (s, a) => { };
var result = await session.RequestExtensionAsync();
if (result == ExtendedExecutionResult.Denied)
System.Diagnostics.Debug.WriteLine("EXTENDED EXECUTION DENIED");
}
}