Xamarin UWP -打开独立Appwidow后的导航问题

8i9zcol2  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(151)

我创建了一个应用程序,在其中我使用多个独立窗口。但我在打开独立的应用程序窗口后遇到了导航问题。为了打开独立窗口,我在Core项目“库类”解决方案中创建了一个接口,并在UWP中创建了一个依赖项服务。接口代码:

public interface IOpenWindow
{
    Task OpenSecondWindow();
}

依赖项的代码

public class OpenWindow : IOpenWindow
{
    private AppWindow appWindow;
    private Frame appWindowFrame = new Frame();
    public async Task OpenSecondWindow()
    {
        // Only ever create one window. If the AppWindow already exists call TryShow on it to bring it to foreground.
        if (appWindow == null)
        {
            // Create a new window
            appWindow = await AppWindow.TryCreateAsync();
            // Make sure we release the reference to this window, and release XAML resources, when it's closed
            appWindow.Closed += delegate { appWindow = null; appWindowFrame.Content = null; };

            //Navigate the frame to the page we want to show in the new window
            appWindowFrame.Navigate(typeof(TheStandalonePage)); // I created a page different than MainPage in UWP project

            // Attach the XAML content to our window
            ElementCompositionPreview.SetAppWindowContent(appWindow, appWindowFrame);
        }

        // Now show the window
        await appWindow.TryShowAsync();
    }
}

然后在UWP项目中的Xaml页面'TheStandalonePage'中,我再次加载应用程序,如下所示:

public TheStandalonePage()
    {
        this.InitializeComponent();
        //This is a boolean used to select in 'COREAPP.App.' the correct MainPage to show
        COREAPP.App.OpenStandAlonePage = true;
        LoadApplication(new COREAPP.App());
        MyDependencies.RegisterAll();
    }

然后在COREAPP中,我确保指定如何打开正确的页面,并选择如下界面:

public static bool OpenStandAlonePage { get; set; } = false;
    public App()
    {
        InitializeComponent();

        if (OpenStandAlonePage)
        {
            MainPage = new MyStandAlonePage();
        }
        else
        {
            MainPage = new MainPage();
        }
    }

在新窗口打开时,'MyStandAlonePage'接管MainPage后,我无法使用App.Current.Mainpage = new AnotherPage();MainPage()
有人知道是否有一个possibity使它发生在任何方式?任何帮助将不胜感激🙌🏿

lmyy7pcs

lmyy7pcs1#

我找到了解决问题的方法。我会试着解释我所理解的。如果我错了,请随时纠正我。

  1. Xamarin.Forms 'Mainpage = new SpecificPage()'仅适用于使用UWP时的Window.Current.Active()功能。这意味着,如果您打开独立窗口(AppWindow),更改'Mainpage'将在此独立窗口上生效。
    1.如果你想使用Xamarin.Forms应用一个'Mainpage'的改变,你需要在UWP项目中改变Window.Current.Active()。

这就是我解决问题的方法

首先,我更新了用于独立页面的界面,如下所示。* 您可以在原始问题中找到'Task OpenSecondWindow()'*

public interface IOpenWindow
{
    Task OpenSecondWindow(); // Used to open a standalone window
    void ResetToMainWindow(); // Used the reset the current window to the mainpage
}

其次,我在UWP项目的依赖注入类中创建了相关函数

public class OpenWindow : IOpenWindow
{
    private AppWindow appWindow;
    private Frame appWindowFrame = new Frame();

    public void ResetToMainWindow()
    {
        // You can find the same functionnalities in the 'App' class in UWP project
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame != null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();
            rootFrame.NavigationFailed += OnNavigationFailed;

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;

            // If we're currently not on a page, navigate to the main page
            if (rootFrame.Content == null)
            {
                COREAPP.App.OpenMainPage = true;
                rootFrame.Navigate(typeof(MainPage));
            }

            // Ensure the current window is active
            Window.Current.Activate();
        }
    }

    private void OnNavigationFailed(object sender, Windows.UI.Xaml.Navigation.NavigationFailedEventArgs e)
    {
        throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
    }
}

WARNING-上述函数将模拟应用程序的初始启动,使用rootFrame.Navigate(typeof(MainPage));.

因为它将导航到UWP项目中的MainPage,您将在此通过构造函数首次启动应用程序。
如果您不希望应用程序再次执行第一次启动过程,则需要遵守以下几个条件:

第一个条件

你必须在核心项目中的'public App()'中添加一个新的布尔值'SecondaryWindowHasBeenOpen',它将帮助你验证是否打开了一个独立页面。你还需要一个布尔值'OpenMainPage'来指示应用程序不打开'MainPage'(MainPage = new MainPage)的新独立页面。

public static bool OpenStandAlonePage { get; set; } = false;
    public static bool SecondaryWindowHasBeenOpen { get; set; } = false;
    public static bool OpenMainPage { get; set; } = false;
    public App()
    {
        InitializeComponent();
        
        if (OpenStandAlonePage || SecondaryWindowHasBeenOpen)
        {
            if (!OpenMainPage)
            {
                // This part ensure the creation of a new standalone page
                MainPage = new MyStandAlonePage();
            }
            else
            {
                //This part ensure to not create a new standalone page
                //It will make sure to not use the process for first app launch...
                //and at the same time, will ensure your app to proceed with...
                //the modification of 'MainPage' in your code in execution
                OpenMainPage = false;
            }
        }
        else
        {
            MainPage = new MainPage();
        } 
    }

第二条件:您需要在UWP项目的独立页面的构造函数中将布尔值'SecondaryWindowHasBeenOpen'标记为true。

public TheStandalonePage()
    {
        this.InitializeComponent();
        //This is a boolean used to select in 'COREAPP.App.' the correct MainPage to show
        COREAPP.App.OpenStandAlonePage = true;
        
        //This will ensure that in COREAPP.App...
        //when you want to proceed to the modification of MainPage...
        //it will not load the application as a first launch
        COREAPP.App.SecondaryWindowHasBeenOpen = true;
        
        LoadApplication(new COREAPP.App());
        MyDependencies.RegisterAll();
    }

第三个条件:在上面的依赖注入代码中,这个条件已经完全满足,您可以在其中找到'COREAPP.App.OpenMainPage = true;”
最后也是最重要的条件:在应用程序中的任何位置,当您希望在主窗口中执行对主页的修改时,请应用以下内容:

1.创建一个静态类和要在CORE项目中调用的函数

public static class MainWindow
{
    public static void Activate()
    {
        if (App.SecondaryWindowHasBeenOpen)
            DependencyService.Get<IOpenWindow>().ResetToMainWindow();
    }
}

1.在需要的地方调用静态函数MainWindow.Activate();

这就是我解决问题的方法

相关问题