如何使用Maui更改BlazorWebView中的页面?例如,页面'/'被打开,我需要打开'/fetch'。
我发现如何通过js返回到上一个链接。
创建自定义导航管理器:
public class CustomNavigationManager
{
private static IJSRuntime JSRuntime { get; set; }
public CustomNavigationManager(IJSRuntime jSRuntime)
{
JSRuntime = jSRuntime;
}
public static async Task Navigation(string url)
{
//Microsoft.Maui.Platform;
if (JSRuntime!= null)
{
await JSRuntime.InvokeVoidAsync("navigation", url);
}
}
}
调用Js代码。调用Js代码。我把它放在了wwwroot/index.html
<script type="text/javascript">
window.navigation = (url) => {
window.location.href = url; // Error: There is no content at fetch.
//history.back();
//window.location="https://0.0.0.0/fetch"; //Error: There is no content at fetch.
}
</script>
注册服务
builder.Services.AddTransient<CustomNavigationManager>();
并注入Shared/MainLayout.razor
@page "/"
@inject CustomNavigationManager navigation
我在maui使用它
await CustomNavigationManager.Navigation("/fetch");
如果我使用js代码history.back();
然后一切工作,
window.location.href = url;
然后我得到一个错误:There is no content at fetch.
取回。剃须刀页
@page "/fetch"
@page "/fetch/{id}"
<h1>Test!</h1>
Fetch.razor
@page "/fetch"
@page "/fetch/{text}"
<h3>@Text</h3>
@code
{
[Parameter]
public string Text { get; set; }
}
MainLayout.razor
@inherits LayoutComponentBase
@inject CustomNavigationManager navigation
<div class="page">
<div class="sidebar">
<NavMenu />
</div>
<main>
<div class="top-row px-4">
<a href="https://learn.microsoft.com/aspnet/" target="_blank">About</a>
</div>
<article class="content px-4">
@Body
</article>
<div>@Url</div>
</main>
</div>
@code
{
[Inject]
private NavigationManager MyNavigationManager { get; set; }
private string Url;
protected override void OnInitialized()
{
base.OnInitialized();
MyNavigationManager.LocationChanged += OnLocationChanges;
Url = MyNavigationManager.Uri;
}
private void OnLocationChanges(object sender, LocationChangedEventArgs e)
{
Url = e.Location;
StateHasChanged();
}
}
CustomNavigationManager.cs
public class CustomNavigationManager
{
private static NavigationManager _navigationManager;
public CustomNavigationManager(NavigationManager MyNavigationManager)
{
_navigationManager = MyNavigationManager;
}
public static void Navigation(string url)
{
if (_navigationManager!=null)
{
_navigationManager.NavigateTo(url);
}
}
}
Decided so:订阅了导航更改事件。实现了服务,并调用了NavigateTo。它对j不起作用。注意:BlazorWebView必须已经下载了项目,否则将无法工作)