我创建了一个使用多个独立窗口的应用程序。但打开独立的应用程序窗口后,我遇到了导航问题。 为了打开独立窗口,我在核心项目“库类”解决方案中创建了一个接口,并在 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之后,我无法在MainPage()中使用App.Current.Mainpage = new AnotherPage();
有人知道是否有可能以任何方式实现它吗? 任何帮助将不胜感激🙌🏿
我找到了问题的解决方案。我将尝试解释我的理解。如果我错了请随时纠正我。
Xamarin.Forms
Mainpage = new SpecificPage()
仅适用于 Window.Current.Active()
功能。这意味着,如果您打开一个独立窗口(AppWindow),更改 Mainpage
将在此独立窗口上生效。
如果您要使用 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 functionalities 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);
}
}
警告:上述功能将使用
rootFrame.Navigate(typeof(MainPage));
模拟应用程序的初始启动。
因为它将导航到 UWP 项目中的 MainPage,您首先在其中通过构造函数启动应用程序。
如果您不希望应用程序再次执行首次启动过程,则需要遵守一些条件:
第一个条件:您必须在核心项目的
SecondaryWindowHasBeenOpen
中添加一个新的布尔值public App()
,这将帮助您验证是否打开了独立页面。您还需要一个布尔值 OpenMainPage
来指示应用程序不要打开“主页”的新独立页面 (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;
最后也是最重要的条件:在应用程序中的任何位置,当您想要在主窗口中执行主页的修改时,请应用以下命令:
在CORE项目中创建一个静态类和要调用的函数
public static class MainWindow
{
public static void Activate()
{
if (App.SecondaryWindowHasBeenOpen)
DependencyService.Get<IOpenWindow>().ResetToMainWindow();
}
}
在需要的地方调用静态函数
MainWindow.Activate();
这就是我解决问题的方法。