如果尚未打开,如何从 App.xaml.cs 打开页面

How to open a page from App.xaml.cs if not open already

我有一个使用 FreshMVVM 的 Xaml.Forms 应用程序。我从 app.xaml.cs 打开某个页面是这样的:

        Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
        {
            var navService = FreshIOC.Container.Resolve<IFreshNavigationService>(FreshMvvm.Constants.DefaultNavigationServiceName);
            Page page = FreshPageModelResolver.ResolvePageModel<SomePageModel>();
            await navService.PushPage(page, null);

    ...
        });

但是如果此页面已经打开,我需要添加一个检查以防止执行此操作。我怎样才能进行这样的检查?

在App中添加静态bool值class检测页面是否打开:

public partial class App : Application
{
    public static bool isPageOpened;
    public App()
    {
        InitializeComponent();

        MainPage = new MainPage();
    }

    public void test()
    {

        if (App.isPageOpened = false)
        {
            Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
            {
                var navService = FreshIOC.Container.Resolve<IFreshNavigationService>(FreshMvvm.Constants.DefaultNavigationServiceName);
                Page page = FreshPageModelResolver.ResolvePageModel<SomePageModel>();

                App.isPageOpened = true;

                await navService.PushPage(page, null);
            });
        }
    }
}

然后在页面的 OnDisappearing 方法中,将 isPageOpened 设置为 false:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnDisappearing()
    {
        base.OnDisappearing();

        App.isPageOpened = false;
    }
}