自己搜索答案后找到了你的问题;在任何地方都没有找到太多帮助,但经过一些试验和错误后确实弄清楚了。
函数参数“提供者”无效或不匹配
原因是在 XAML 中,调用了一个特定的重载,即 DateTimeProperty.ToString(string, IFormatProvider)。
就我而言,我显示的任何值都在用户控件中,因此我为每个值添加了一个 CultureInfo 依赖属性并将其绑定到我的视图模型上的公共源。
如果是 C#,请添加:
using System.Globalization;
然后
public static readonly DependencyProperty CultureInfoProperty = DependencyProperty.Register(
"CultureInfo", typeof(CultureInfo), typeof(XyzReadoutView), new PropertyMetadata(default(CultureInfo)));
public CultureInfo CultureInfo
{
get { return (CultureInfo) GetValue(CultureInfoProperty); }
set { SetValue(CultureInfoProperty, value); }
}
这将创建 x:Bind 所需的本地实例,如果使用静态属性,则会发生编译错误。
和 XAML:
<TextBlock Text={x:Bind MyDateTime.ToString('h:mm tt', CultureInfo)} />
请注意,格式用'而不是'包围。
此外,这只会更新一次,因为 x:Bind 的模式默认为 Mode=OneTime; 如果要传播 DateTime 或 CultureInfo 上的更改,则必须将模式更改为 Mode=OneWay。
<TextBlock Text={x:Bind MyDateTime.ToString('h:mm tt', CultureInfo), Mode=OneWay} />
如果格式是用户可更改的,我会为它创建一个依赖属性,以便更新和轻松将控件绑定回视图模型,但这只是我个人的偏好。
public static readonly DependencyProperty DateTimeFormatProperty = DependencyProperty.Register(
"DateTimeFormat", typeof(string), typeof(XyzReadoutView), new PropertyMetadata(default(string)));
public string DateTimeFormat
{
get { return (string) GetValue(DateTimeFormatProperty); }
set { SetValue(DateTimeFormatProperty, value); }
}
和 XAML:
<TextBlock Text={x:Bind MyDateTime.ToString(DateTimeFormat, CultureInfo), Mode=OneWay} />