我挣扎了一会儿,在这里找到了解决方法:
https://github.com/dotnet/maui/issues/11746
此解决方法向执行页面导航的 blazor Router 添加了一个属性。
基于标准的 MAUI Blazor 模板项目,执行以下操作:
主.razor
添加StartPath 属性并注入NavigationManager。当 StartPath 属性更改时,导航到新的 StartPath。
@inject NavigationManager NavigationManager
<Router AppAssembly="@typeof(Main).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
@code
{
[Parameter] public string StartPath { get; set; }
protected override void OnParametersSet()
{
if (!string.IsNullOrEmpty(StartPath))
{
NavigationManager.NavigateTo(StartPath);
}
base.OnParametersSet();
}
}
主页.xaml
在BlazorWebView组件中添加x:Name
<BlazorWebView x:Name="blazorWebView" HostPage="wwwroot/index.html">
<BlazorWebView.RootComponents>
<RootComponent Selector="#app" ComponentType="{x:Type local:Main}" />
</BlazorWebView.RootComponents>
</BlazorWebView>
MainPage.xaml.cs
将 StartPath 属性添加到 RootComponent
public partial class MainPage : ContentPage
{
public MainPage(string startPath = null)
{
InitializeComponent();
if (startPath != null)
{
blazorWebView.RootComponents[0].Parameters = new Dictionary<string, object>
{
{ "StartPath", startPath },
};
}
}
}
而已!
您现在可以使用以下代码打开一个新窗口,指向柜台页。
var counterWindow = new Window
{
Page = new MainPage("/counter")
};
Application.Current.OpenWindow(counterWindow);