Xaml 使用x:Bind时指定签名

Xaml 使用x:Bind时指定签名,xaml,uwp,xbind,Xaml,Uwp,Xbind,UWP允许您绑定到静态方法。我试图通过使用获取时间字符串 <TextBlock Text={x:Bind MyDateTime.ToString(MyPatternString)} /> 其中MyPatternString是“h:mm tt” 问题是DateTime的ToString()方法有几个不同的签名。第一个接收一个IFormatProvider。因此,我得到一个生成错误: 函数参数“provider”无效或不匹配 有没有办法告诉它我希望使用接受字符串的签名?我原以为它会

UWP允许您绑定到静态方法。我试图通过使用获取时间字符串

<TextBlock Text={x:Bind MyDateTime.ToString(MyPatternString)} />

其中
MyPatternString
是“h:mm tt”

问题是
DateTime
ToString()
方法有几个不同的签名。第一个接收一个
IFormatProvider
。因此,我得到一个生成错误:

函数参数“provider”无效或不匹配


有没有办法告诉它我希望使用接受字符串的签名?我原以为它会自动知道这一点。

您需要使用IValueConverter来格式化文本以供显示。创建从IValueConverter继承的类

public class DateFormatter : IValueConverter
{
    // This converts the DateTime object to the string to display.
    public object Convert(object value, Type targetType, 
        object parameter, string language)
    {
        // Retrieve the format string and use it to format the value.
        string formatString = parameter as string;
        if (!string.IsNullOrEmpty(formatString))
        {
            return string.Format(
                new CultureInfo(language), formatString, value);
        }
        // If the format string is null or empty, simply call ToString()
        // on the value.
        return value.ToString();
    }

    // No need to implement converting back on a one-way binding 
    public object ConvertBack(object value, Type targetType, 
        object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
在页面上将其注册为资源,然后可以在绑定中指定转换器

<TextBlock Text={x:Bind MyDateTime, Converter={StaticResource DateFormatConverter}}, ConverterParameter="mm/dd/yyyy"}"/>

您只需在ViewModel中添加一个方法,然后使用它即可

这样可以将绑定表达式更改为:



请注意,这仅适用于Windows 10周年更新!更多关于这件事的信息

在自己搜索答案后找到了您的问题;没有在任何地方找到太多的帮助,但经过一些尝试和错误后找到了答案

函数参数“provider”无效或不匹配

原因是在XAML中,调用了一个特定的重载,即DateTimeProperty.ToString(string,IFormatProvider)

在我的例子中,我显示的任何值都在用户控件中,因此对于每个值,我都添加了一个CultureInfo依赖项属性,并将其绑定到视图模型上的公共源

如果是C#,则添加:

然后

这将创建x:Bind所需的本地实例,如果使用静态属性,则会发生编译错误

和XAML:

<TextBlock Text={x:Bind MyDateTime.ToString('h:mm tt', CultureInfo)} />
<TextBlock Text={x:Bind MyDateTime.ToString(DateTimeFormat, CultureInfo), Mode=OneWay} />
和XAML:

<TextBlock Text={x:Bind MyDateTime.ToString('h:mm tt', CultureInfo)} />
<TextBlock Text={x:Bind MyDateTime.ToString(DateTimeFormat, CultureInfo), Mode=OneWay} />


我知道如何使用转换器来完成我要做的事情。我的问题更多的是一个好奇的问题。似乎应该有一种方法来指定签名。避免转换器是x:Bind的目标之一。您不能只插入一个IFormatProvider(如CultureInfo.Invariant等)吗?否则,创建您自己的conversionmethod调用.Hm。看来这是我能找到的最接近的了。不得不这样做仍然让人感觉很笨拙,但总比什么都不做要好。
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); }
    }
<TextBlock Text={x:Bind MyDateTime.ToString(DateTimeFormat, CultureInfo), Mode=OneWay} />