时间:2026-03-08 12:37
人气:
作者:admin
新的一年开始了,公司提了新的要求:
开发部的应对是:从wpf转换Maui:
话说华为的鸿蒙,为了国产化,在华为P40手机上运行了Maui的缺省项目,天真的以为鸿蒙支持Maui Android。于是去买了个华为的平板,然后悲剧了。亲测:P40手机的鸿蒙4.2支持,新平板的5.0、6.0不支持。要不要为了这个牛绳去买头牛,学一般华为的那什么javascript的超集typescript的超集,叫什么来着?
谁能推荐个安卓平板?
华为的事情是个庭外话,题内化就是开始新一轮的造轮子。
你说,轮子到处都是,非得自己造?
是的,有毛病: 不相信别人,别人走过的路,自己也要走,除非自己不会走。
废话到此为止。
微软本地化文档,报了个到。
一、准备工作
<Resources>
<Resource Language="x-generate"/>
</Resources>
替换为
<Resources>
<Resource Language="en-US"/>
<Resource Language="zh-CN"/>
</Resources>
表示本Demo支持中英文。
二、LunZi添加资源


备注一下:回头弄个程序来添加资源。
三、Demo使用资源
xmlns:lunzi="clr-namespace:LunZi.Resouces;assembly=LunZi"
使用资源
<Label Text="{x:Static lunzi:Resource.Test}"/>
整个文件变成
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:lunzi="clr-namespace:LunZi.Resouces;assembly=LunZi"
x:Class="Demo.MainPage">
<VerticalStackLayout>
<Label Text="{x:Static lunzi:Resource.Test}"/>
</VerticalStackLayout>
</ContentPage>

四、中英文动态切换
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:lunzi="clr-namespace:LunZi.Resouces;assembly=LunZi"
x:Class="Demo.MainPage">
<VerticalStackLayout HorizontalOptions="Center" VerticalOptions="Center" Spacing="10">
<Label x:Name="LabelTest"/>
<HorizontalStackLayout Spacing="10">
<Button Text="中文" Clicked="BtnCn_Clicked"/>
<Button Text="English" Clicked="BtnEn_Clicked"/>
</HorizontalStackLayout>
</VerticalStackLayout>
</ContentPage>
using System.Globalization;
namespace Demo;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
this.LabelTest.Text = LunZi.Resouces.Resource.Test;
}
private void BtnEn_Clicked(object sender, EventArgs e)
{
SetCulture("en-US");
this.LabelTest.Text = LunZi.Resouces.Resource.Test;
}
private void BtnCn_Clicked(object sender, EventArgs e)
{
SetCulture("zh-Hans");
this.LabelTest.Text = LunZi.Resouces.Resource.Test;
}
private void SetCulture(string cultureCode)
{
var culture = new CultureInfo(cultureCode);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
}


好了,就这样了。