接着上一篇"如何为面向 Windows 的 MAUI Blazor 应用程序设置窗口标题?"
Tips: 总所周知,MAUI 除了 Windows App 其他平台窗口是没有 Title 这回事的.
在 Blazor 里面可以直接给页面打上
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using WinRT.Interop;
...
namespace MauiApp7test.WinUI
{
public partial class App : MauiWinUIApplication
{
public static object CurrentWindow;
public static AppWindow AppWindow;
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
base.OnLaunched(args);
CurrentWindow = Application.Windows[0].Handler?.PlatformView;
IntPtr _windowHandle = WindowNative.GetWindowHandle(CurrentWindow);
var windowId = Win32Interop.GetWindowIdFromWindow(_windowHandle);
AppWindow = AppWindow.GetFromWindowId(windowId);
SetTitle("MauiApp7test");
}
public static void SetTitle(string title) => AppWindow.Title = title;
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
@code {
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
#if WINDOWS
WinUI.App.SetTitle("MauiApp7test - Index");
#endif
}
}
}
@code {
private WeatherForecast[] forecasts;
protected override async Task OnInitializedAsync()
{
#if WINDOWS
WinUI.App.SetTitle("MauiApp7test - Fetchdata");
#endif
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
}
}
MAUI 还是一个新鲜事物,在官方还没支持的一些骚操作的情况下多发散思维,总能填坑的.
标题设置这里只是写了个方法去设置,也可以写成接口各平台实现,注入服务方式调用,理论上会更加通用一点.
https://gitee.com/densen2014/BlazorMaui
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名AlexChow,不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系 。
页面更新:2024-05-04
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号