MAUI Blazor (Windows) App 动态设置窗口标题
2023-03-20 15:24:47 时间
原文链接 [https://www.cnblogs.com/densen2014/p/16950996.html]
接着上一篇"如何为面向 Windows 的 MAUI Blazor 应用程序设置窗口标题?"
Tips: 总所周知,MAUI 除了 Windows App 其他平台窗口是没有 Title 这回事的.
在 Blazor 里面可以直接给页面打上 <PageTitle>MauiApp7test</PageTitle>
动态设置页面标题,在 Windows 的 MAUI Blazor 应用程序设置是没有效果的,因为这个只是设置了 BlazorWebView
控件的标题,并不是真正的窗口标题, 接着上一篇的知识改造一下动态设置标题:
工程文件 Platforms -> Windows -> App.xaml.cs
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();
}
}
页面文件 Pages ->PagesIndex.razor
@code {
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
#if WINDOWS
WinUI.App.SetTitle("MauiApp7test - Index");
#endif
}
}
}
页面文件 FetchData.razor
@code {
private WeatherForecast[] forecasts;
protected override async Task OnInitializedAsync()
{
#if WINDOWS
WinUI.App.SetTitle("MauiApp7test - Fetchdata");
#endif
forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
}
}
运行效果
总结
MAUI 还是一个新鲜事物,在官方还没支持的一些骚操作的情况下多发散思维,总能填坑的.
标题设置这里只是写了个方法去设置,也可以写成接口各平台实现,注入服务方式调用,理论上会更加通用一点.
项目源码
知识共享许可协议
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名AlexChow(包含链接: https://github.com/densen2014 ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系 。
AlexChow
今日头条 | 博客园 | 知乎 | Gitee | GitHub
相关文章
- 金融服务领域的大数据:即时分析
- 影响大数据、机器学习和人工智能未来发展的8个因素
- 从0开始构建一个属于你自己的PHP框架
- 如何将Hadoop集成到工作流程中?这6个优秀实践必看
- SEO公司使用大数据优化其模型的5种方法
- 关于Web Workers你需要了解的七件事
- 深入理解HTTPS原理、过程与实践
- 增强分析:数据和分析的未来
- PHP协程实现过程详解
- AI专家:大数据知识图谱——实战经验总结
- 关于PHP的错误机制总结
- 利用数据分析量化协同过滤算法的两大常见难题
- 怎么做大数据工作流调度系统?大厂架构师一语点破!
- 2019大数据处理必备的十大工具,从Linux到架构师必修
- OpenCV中的KMeans算法介绍与应用
- 教大家如果搭建一套phpstorm+wamp+xdebug调试PHP的环境
- CentOS下三种PHP拓展安装方法
- Go语言HTTP Server源码分析
- Go语言HTTP Server源码分析
- 2017年4月编程语言排行榜:Hack首次进入前五十