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

.NET MAUI XAML中格式化日期时如何指定自定义文化?

.NET MAUI XAML日期格式化指定自定义文化的解决方法

问题描述

在我的.NET MAUI应用中,希望在XAML里格式化日期/时间,显示星期缩写和日期。例如日期24.11.2024需要显示为So 24.11.24(德语“Sonntag”的缩写)。采用以下实现后,实际显示的是Su 24.11.24(英文星期缩写):

<Label>
    <Label.FormattedText>
        <FormattedString>
             <Span Text="{Binding StartDate, StringFormat='{0:ddd}'}" FontAttributes="Bold" />
                <Span Text=" " />
                <Span Text="{Binding StartDate, StringFormat='{0:dd.MM.yy}'}" />
        </FormattedString>
    </Label.FormattedText>                                    
</Label>

请问是否可以在XAML中转换日期时指定自定义文化?


解决方案

方法1:自定义IValueConverter(通用兼容方案)

创建一个可指定目标文化的日期格式化转换器,能灵活控制单个绑定的文化:

using System.Globalization;
using Microsoft.Maui.Controls;

namespace YourAppNamespace.Converters
{
    public class LocalizedDateConverter : IValueConverter
    {
        // 可在XAML中指定目标文化
        public CultureInfo TargetCulture { get; set; } = CultureInfo.InvariantCulture;

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is DateTime date && parameter is string formatString)
            {
                return date.ToString(formatString, TargetCulture);
            }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

在页面的资源中声明转换器(指定德语文化de-DE):

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:converters="clr-namespace:YourAppNamespace.Converters"
             x:Class="YourAppNamespace.MainPage">
    <ContentPage.Resources>
        <converters:LocalizedDateConverter x:Key="GermanDateConverter" TargetCulture="de-DE" />
    </ContentPage.Resources>

    <!-- 页面内容 -->
    <Label>
        <Label.FormattedText>
            <FormattedString>
                <Span Text="{Binding StartDate, Converter={StaticResource GermanDateConverter}, ConverterParameter='ddd'}" 
                      FontAttributes="Bold" />
                <Span Text=" " />
                <Span Text="{Binding StartDate, Converter={StaticResource GermanDateConverter}, ConverterParameter='dd.MM.yy'}" />
            </FormattedString>
        </Label.FormattedText>
    </Label>
</ContentPage>

方法2:全局设置应用默认文化

如果整个应用都需要使用德语文化,可以在App.xaml.cs中全局配置:

using System.Globalization;

namespace YourAppNamespace
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
            // 设置全局文化为德语
            CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("de-DE");
            CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("de-DE");
            MainPage = new MainPage();
        }
    }
}

设置后,你原来的XAML代码会自动使用德语格式化日期,无需修改绑定。

方法3:直接在Binding中指定Culture(.NET MAUI 7+专属)

从.NET MAUI 7版本开始,Binding新增了Culture属性,可直接在XAML中为单个绑定指定文化,无需写转换器:

<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span FontAttributes="Bold">
                <Span.Text>
                    <Binding Path="StartDate" StringFormat="{0:ddd}" Culture="de-DE" />
                </Span.Text>
            </Span>
            <Span Text=" " />
            <Span>
                <Span.Text>
                    <Binding Path="StartDate" StringFormat="{0:dd.MM.yy}" Culture="de-DE" />
                </Span.Text>
            </Span>
        </FormattedString>
    </Label.FormattedText>
</Label>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 01:34:51