翼度科技»论坛 编程开发 .net 查看内容

WPF 已知问题 dotnet 6 设置 InvariantGlobalization 之后将丢失默认绑定转

8

主题

8

帖子

24

积分

新手上路

Rank: 1

积分
24
在设置了 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 的代码:
  1. using System.Windows;
  2. namespace repro
  3. {
  4.     /// <summary>
  5.     /// Interaction logic for MainWindow.xaml
  6.     /// </summary>
  7.     public partial class MainWindow : Window
  8.     {
  9.         public MainWindow()
  10.         {
  11.             InitializeComponent();
  12.         }
  13.         public static string IWillNotCauseException { get; set; }
  14.         public static int IWillCauseException { get; set; }
  15.     }
  16. }
复制代码
这是在 XAML 的代码
  1. <Window x:
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6.         xmlns:local="clr-namespace:repro"
  7.         mc:Ignorable="d"
  8.         Title="MainWindow" Height="450" Width="800">
  9.     <Grid>
  10.         <TextBlock Text="{Binding Source={x:Static local:MainWindow.IWillNotCauseException}}" />
  11.         <TextBlock Text="{Binding Source={x:Static local:MainWindow.IWillCauseException}}" />
  12.     </Grid>
  13. </Window>
复制代码
运行之后,将会看到 XAML 抛出异常。详细请看 https://github.com/dotnet/wpf/issues/6477
抛出的异常包含以下信息
  1. 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 文档所述:
  1. Starting in .NET 6 when globalization-invariant mode is enabled:
  2.     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 里面,如以下代码
  1. <PropertyGroup>
  2.     <InvariantGlobalization>true</InvariantGlobalization>
  3. </PropertyGroup>
复制代码
或者是编辑 runtimeconfig.json 文件,添加如下代码
  1. {
  2.     "runtimeOptions":
  3.     {
  4.         "configProperties":
  5.         {
  6.             "System.Globalization.Invariant": true
  7.         }
  8.     }
  9. }
复制代码
参考文档:
Breaking change: Culture creation and case mapping in globalization-invariant mode
runtime/globalization-invariant-mode.md at main · dotnet/runtime · GitHub

来源:https://www.cnblogs.com/lindexi/archive/2023/01/09/17038303.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具