如何使用 Xamarin.Forms 重新启动应用程序

问题描述 投票:0回答:6

如果我在我的应用程序中管理会话(会话的任何概念都适用),并且我认为会话已过期,无论出于何种原因,我如何以编程方式重新启动应用程序,普遍适用于 iOS、Android、WinPhone?

xamarin xamarin.forms xamarin.android session-timeout application-restart
6个回答
6
投票

您无法显式重新启动应用程序 - iOS 明确禁止这样做,并且没有通用机制可以在其他平台上执行此操作。如果您确定会话已过期,则需要提示用户登录,并手动执行任何初始化。


5
投票

您可以创建 MainPage 的新实例

(Application.Current).MainPage = new NavigationPage(new MainPage());

2
投票

MainPage 的新实例

从任何地方打电话。 如果只是从异步创建是行不通的

void Reset()//require in the main Thread, also the app will crash
{
    (App.Current as App).MainPage.Dispatcher.Dispatch(() =>
    {
        (App.Current as App).MainPage = new AppShell();
    });
}

0
投票

它的价值;我正在使用 Blazor Xamarin 设置(混合应用程序,我认为它现在是 .Net MAUI),这就是我根据本文中的其他答案和建议重新加载桌面应用程序所做的事情。

应用程序.cs

public class App : Xamarin.Forms.Application
{
    private static IHost _host;

    public App()
    {
        // ... hostBuilder stuff. Configure services, etc.

        _host = hostBuilder.Build();

        Reload();
    }

    public static void Reload()
    {
        Current.MainPage = new ContentPage();
        NavigationPage.SetHasNavigationBar(Current.MainPage, false);
        _host.AddComponent<Main>(parent: Current.MainPage);
    }
}

App.razor

我将其保留在使用按钮调用的主 App.razor 中。我们的想法是,如果出现错误上升到顶部并需要重新加载应用程序,则使用它。

@* Show button if there is an error *@
<button class="btn btn-sm btn-danger" @onclick="Reload">Reload</button>

@code {
    void Reload()
    {
        Application.Current.MainPage.Dispatcher.BeginInvokeOnMainThread(() => 
        {
            MyHybridApplication.App.Reload();
        });
    }
}

0
投票

您无法重新启动毛伊岛应用程序,但可以

  1. 设置新的文化信息等
  2. 重新初始化所需的服务
  3. 设置一个新创建的 MainPage,使用新的上下文

0
投票

MainPage 的新实例对我很有用。 (对于 Xamarin.Forms)

Device.BeginInvokeOnMainThread(() =>
{
    App.Current.MainPage = new AppShell();
});
© www.soinside.com 2019 - 2024. All rights reserved.