简体   繁体   English

Xamarin.Forms UWP-登录时启动应用程序

[英]Xamarin.Forms UWP - Launch app on login

I'm trying to launch my app when the user logs in to Windows. 我正在尝试在用户登录Windows时启动我的应用程序。 I have the appropriate Extension ( StartupTask ) set in Package.appxmanifest , and I can get the app to launch when I log in to Windows, as expected. 我在Package.appxmanifest中设置了适当的ExtensionStartupTask ),并且可以按预期在登录Windows时启动该应用程序。 However, the app crashes after showing the Splash screen for about a second or two. 但是,应用程序在显示“启动画面”大约一两秒钟后崩溃。

In my App.xaml.cs file, I have overridden the OnLaunched (called when the user launches the app) and OnActivated (called when the system launches the app after Windows login). 在我的App.xaml.cs文件中,我覆盖了OnLaunched (在用户启动应用程序时调用)和OnActivated (在Windows登录后系统启动应用程序时调用)。 Both call the same function to initialize my app; 两者都调用相同的函数来初始化我的应用程序; however, the app crashes only when the app is initialized from the OnActivated function. 但是,仅当从OnActivated函数初始化应用程序后,该应用程序才会崩溃。 When initialized from OnLaunched , it works as expected. OnLaunched初始化后,它会按预期工作。 Here is the relevant code, from App.xaml.cs : 这是来自App.xaml.cs的相关代码:

// 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");
    }
}

The problem is that you are trying to cast the IActivatedEventArgs to LaunchActivatedEventArgs , but when startup activation happens, the type is actually StartupTaskActivatedEventArgs . 问题是您试图将IActivatedEventArgsLaunchActivatedEventArgs ,但是当启动启动发生时,类型实际上是StartupTaskActivatedEventArgs

Luckily, you actually need the e parameter only for Xamarin.Forms.Forms.Init which actually accepts IActivatedEventArgs , so you can just change the parameter of InitializeApp to be IActivatedEventArgs and remove the cast to LaunchActivatedEventArgs in OnActivated . 幸运的是,实际上只需要Xamarin.Forms.Forms.Inite参数,该参数实际上接受IActivatedEventArgs ,因此您只需将InitializeApp的参数更改为IActivatedEventArgs并在OnActivated LaunchActivatedEventArgsLaunchActivatedEventArgs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM