1

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

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

MyPatternString“h:mm tt”在哪里。

问题是DateTime'sToString()方法有几个不同的签名。第一个收到IFormatProvider. 因此,我得到一个构建错误:

函数参数“提供者”无效或不匹配

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

4

3 回答 3

2

您可以向 ViewModel 添加一个方法并使用它!

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

<TextBlock Text={x:Bind FormatDateToString(MyDateTime)} />

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

于 2017-02-21T15:19:57.207 回答
0

自己搜索答案后找到了你的问题;在任何地方都没有找到太多帮助,但经过一些试验和错误后确实弄清楚了。

函数参数“提供者”无效或不匹配

原因是在 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} />
于 2018-01-16T02:10:28.363 回答
-1

您需要使用 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"}"/>

https://docs.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.Data.IValueConverter

于 2017-02-20T17:52:53.767 回答