You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Maui设置阿拉伯语为当前区域时的从右到左(RTL)问题

.NET Maui 阿拉伯语区域下窗口按钮显示与点击区域错位问题

当执行代码Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ar");将当前线程区域设置为阿拉伯语后,窗口的关闭、最小化、最大化按钮视觉上显示在窗口右侧,但实际可点击区域仍停留在左侧,这是RTL(从右到左)布局适配的异常问题。

问题演示

解决方法

  • 采用Maui官方本地化配置替代直接修改线程Culture
    避免直接操作Thread.CurrentThread.CurrentCulture,改用Maui内置的本地化配置流程,在MauiProgram.cs中添加:

    builder.Services.Configure<LocalizationOptions>(options =>
    {
        options.DefaultCulture = new CultureInfo("ar");
        options.SupportedCultures = new List<CultureInfo> { new CultureInfo("ar") };
        options.SupportedUICultures = new List<CultureInfo> { new CultureInfo("ar") };
    });
    

    并在App.xaml.cs的构造函数中初始化生效:

    var localizationOptions = Services.GetService<IOptions<LocalizationOptions>>();
    if (localizationOptions != null)
    {
        CultureInfo.DefaultThreadCurrentCulture = localizationOptions.Value.DefaultCulture;
        CultureInfo.DefaultThreadCurrentUICulture = localizationOptions.Value.DefaultCulture;
    }
    
  • 针对Windows平台手动修正标题栏RTL布局
    在Windows平台的特定代码中调整标题栏的RTL属性,打开Platforms/Windows/App.xaml.cs,修改OnLaunched方法:

    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        base.OnLaunched(args);
        var window = Application.Windows[0].Handler.PlatformView as Microsoft.UI.Xaml.Window;
        if (window != null)
        {
            window.AppWindow.TitleBar.RightToLeftLayout = true;
        }
    }
    
  • 更新.NET Maui到最新版本
    该问题可能是旧版本的平台适配bug,尝试升级到最新稳定版的.NET Maui SDK,官方可能已修复此类RTL布局下的窗口标题栏错位问题。

内容的提问来源于stack exchange,提问作者Shehab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 21:05:24