c# - 在 IValueConverter 的 Type targetType 参数中输入什么

标签 c# types windows-phone-8 ivalueconverter

我正在通过隐藏代码调用 IValueConverter 类,但我不确定要在 Type targetType 参数中放入什么。对象是 string 但使用它会给我“无效的表达式术语‘string’”

我调用转换器的代码

secondConverter.Convert(score, string, null, CultureInfo.CurrentCulture);

转换器类

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        TimeSpan ts = new TimeSpan(0, 0, (int)value);

        return String.Format("{0:D2}:{1:D2}:{2:D2}",
                        ts.Hours,
                        ts.Minutes,
                        ts.Seconds);
    }

最佳答案

您可以放置​​ typeof(string) 而不是字符串,但您的转换器似乎不使用或验证目标类型,因此您可以放置​​任何内容,包括 null。

通常,您的转换器至少应验证目标类型是否为字符串,如果不是则抛出异常。

关于c# - 在 IValueConverter 的 Type targetType 参数中输入什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16061489/

相关文章:

c# - WP8 : LongListSelector with ContextMenu to delete an item

c# - C#中的循环数组

postgresql - 如何创建作为一组枚举值的 PostgreSQL 列?

swift - 如何检测该参数是两种任意类型的元组?

css - 在 Windows Phone 中通过 z-index 锁定屏幕

c# - 使用反射从字典中设置属性

c# - 在服务器上安装了 Word 但找不到它以在组件服务中设置权限

c# - 获取网站服务的 tcp 端口

c# - 连接到集线器的 SignalR 2.2.0 WebSocket 错误

c++ - 为什么这个构造函数没有给出不完整的类型错误?