Min*_*wky 3 progressive-web-apps blazor blazor-webassembly
Blazor Web Assembly PWA 中有没有办法添加“安装”对话框提示?类似于 Youtube Music 的东西
粗略的流程是
beforeinstallprompt
window.addEventListener('beforeinstallprompt', function (e) {
e.preventDefault();
// Stash the event so it can be triggered later.
// where you store it is up to you
window.PWADeferredPrompt = e;
// Notify C# Code that it can show an alert
// MyBlazorInstallMethod must be [JSInvokable]
DotNet.invokeMethodAsync("MyBlazorAssembly", "MyBlazorInstallMethod");
});
Run Code Online (Sandbox Code Playgroud)
然后,此 C# 方法可以显示一条警报,告知用户可以安装为桌面应用程序并提供“安装/取消”按钮。
window.BlazorPWA = {
installPWA: function () {
if (window.PWADeferredPrompt) {
// Use the stashed event to continue the install process
window.PWADeferredPrompt.prompt();
window.PWADeferredPrompt.userChoice
.then(function (choiceResult) {
window.PWADeferredPrompt = null;
});
}
}
};
Run Code Online (Sandbox Code Playgroud)
[JSInvokable]
public async Task MyBlazorInstallMethod()
{
// show an alert and get the result
...
// tell browser to install
if (UserChoseInstall)
{
await jSRuntime.InvokeVoidAsync("BlazorPWA.installPWA");
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1433 次 |
最近记录: |