时间: 2022-12-04 23:43:15 作者: 媒介星软文平台
Tips: 总所周知,MAUI 除了 Windows App 其他平台窗口是没有 Title 这回事的.
在 Blazor 里面可以直接给页面打上 <PageTitle>MauiApp7test</PageTitle> 动态设置页面标题,在 Windows 的 MAUI Blazor 应用程序设置是没有效果的,因为这个只是设置了 BlazorWebView 控件的标题,并不是真正的窗口标题, 接着上一篇的知识改造一下动态设置标题:
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 还是一个新鲜事物,在官方还没支持的一些骚操作的情况下多发散思维,总能填坑的.
标题设置这里只是写了个方法去设置,也可以写成接口各平台实现,注入服务方式调用,理论上会更加通用一点.
扫一扫,添加好友!
免责声明:本文系转载,版权归原作者所有;旨在传递信息,不代表本站的观点和立场和对其真实性负责,如因作品内容,版权和其他问题需要同本网站联系的,请邮件联系2290/781984@qq.com
全网媒体直线发稿、24小时自助发稿平台、助您提升营销效率!