论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
朋友圈
看朋友圈动态,了解ToB世界。
ToB门户
了解全球最新的ToB事件
博客
Blog
排行榜
Ranklist
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
导读
Guide
相册
Album
记录
Doing
搜索
本版
文章
帖子
ToB圈子
用户
免费入驻
产品入驻
解决方案入驻
公司入驻
案例入驻
登录
·
注册
只需一步,快速开始
账号登录
立即注册
找回密码
用户名
Email
自动登录
找回密码
密码
登录
立即注册
首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
圈子
SAAS
ToB企服应用市场:ToB评测及商务社交产业平台
»
论坛
›
软件与程序人生
›
后端开发
›
.Net
›
WPF 已知问题 dotnet 6 设置 InvariantGlobalization 之 ...
WPF 已知问题 dotnet 6 设置 InvariantGlobalization 之后将丢失默认绑定转 ...
tsx81429
金牌会员
|
2023-1-9 23:05:21
|
显示全部楼层
|
阅读模式
楼主
主题
914
|
帖子
914
|
积分
2742
在设置了 InvariantGlobalization 为 true 之后,将会发现原本能正常工作的 XAML 可能就会抛出异常。本文将告诉大家此问题的原因
这是有开发者在 WPF 仓库上给我报告的 bug 我才找到的问题。问题的现象是 XAML 抛出异常,步骤有些复杂:
升级到 dotnet 6 版本。 因为此问题是在 dotnet 6 下才能复现,在 dotnet 6 以下,如 dotnet 5 和 dotnet core 3.1 是没有问题的
要求设置 InvariantGlobalization 为 true 的值
在 XAML 绑定静态的非字符串类型的属性,例如 int 类型的属性,如以下代码
这是 MainWindow.xaml.cs 的代码:
using System.Windows;
namespace repro
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static string IWillNotCauseException { get; set; }
public static int IWillCauseException { get; set; }
}
}
复制代码
这是在 XAML 的代码
<Window x:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:repro"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBlock Text="{Binding Source={x:Static local:MainWindow.IWillNotCauseException}}" />
<TextBlock Text="{Binding Source={x:Static local:MainWindow.IWillCauseException}}" />
</Grid>
</Window>
复制代码
运行之后,将会看到 XAML 抛出异常。详细请看
https://github.com/dotnet/wpf/issues/6477
抛出的异常包含以下信息
System.Globalization.CultureNotFoundException: 'Only the invariant culture is supported in globalization-invariant mode. See https://aka.ms/GlobalizationInvariantMode for more information. (Parameter 'name')
复制代码
原因是在 dotnet 6 设置了 InvariantGlobalization 为 true 之后,在调用 CultureInfo.GetCultureInfoByIetfLanguageTag 方法时,将抛出异常,如下图
这是符合
官方文档
描述的
如
Breaking change: Culture creation and case mapping in globalization-invariant mode
文档所述:
Starting in .NET 6 when globalization-invariant mode is enabled:
If an app attempts to create a culture that's not the invariant culture, a CultureNotFoundException exception is thrown.
复制代码
因此这个问题其实是 dotnet 6 的符合预期的行为,也不是 WPF 的问题
附设置 InvariantGlobalization 为 true 的方法如下
编辑 csproj 项目文件,添加 true 到 PropertyGroup 里面,如以下代码
<PropertyGroup>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
复制代码
或者是编辑 runtimeconfig.json 文件,添加如下代码
{
"runtimeOptions":
{
"configProperties":
{
"System.Globalization.Invariant": true
}
}
}
复制代码
参考文档:
Breaking change: Culture creation and case mapping in globalization-invariant mode
runtime/globalization-invariant-mode.md at main · dotnet/runtime · GitHub
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
本帖子中包含更多资源
您需要
登录
才可以下载或查看,没有账号?
立即注册
x
回复
使用道具
举报
0 个回复
正序浏览
返回列表
快速回复
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
or
立即注册
本版积分规则
发表回复
回帖并转播
发新帖
回复
tsx81429
金牌会员
这个人很懒什么都没写!
楼主热帖
MySQL 主从复制数据不一致,怎么办? ...
什么是 SSL、TLS 和 HTTPS?
Android修行手册 - SeekBar
Kali Linux三种网络攻击方法总结(DDoS ...
面了个拿 30K 出来的测试,见识到了什 ...
轻量级CI/CD发布部署环境搭建及使用_07 ...
微信小程序项目实例——体质计算器 ...
美团大脑百亿级知识图谱的构建及应用进 ...
数据分析引擎百花齐放,为什么要大力投 ...
2流高手速成记(之五):Springboot整 ...
标签云
存储
挺好的
服务器
快速回复
返回顶部
返回列表