System.ComponentModel.TypeConverter 的 WinRT 替换ComponentModel、System、WinRT、TypeConverter

2023-09-06 14:12:52 作者:色狼的最高境界是专一

它看起来不像 TypeConverter 可以使用.建议用什么替换它?

It doesn't look like TypeConverter is available to use. What is recommended to replace this?

我本来打算创建自己的 TypeConverter 类来替换它,但如果 WinRT 中有新的或更好的方法可以做到这一点,我会走那条路.我还需要重新创建许多其他类;像所有的默认类型转换器一样.

I was going to go and create my own TypeConverter class to use to replace it, but if there is a new or better way in WinRT to do it, I'd go that route. There are also many other classes that I would need to recreate; like all the default type converters.

推荐答案

WinRT 中没有 TypeConverter 类,团队尚未宣布任何计划将其包含在未来的版本中.您有多种选择.

There is no TypeConverter class in the WinRT and the team has not announced any plans to include it in a future release. You have a number of options.

选项 1:如果要将转换作为数据绑定的一部分进行,请使用 Dennis 提到的 IValueConverter 接口.

Option 1: If the conversion is to be done as part of a data binding use the IValueConverter interface as Dennis mentioned.

选项 2:如果您是该类型的创建者,您可以添加自己的显式或隐式运算符来支持强制转换:

Option 2: If you are the creator of the type you can add your own explicit or implicit operators to support casting:

http://msdn.microsoft.com/en-US/library/xhbhezf4(v=vs.80).aspx

http://msdn.microsoft.com/en-US/library/z5z9kes2(v=vs.80).aspx

选项 3:您可以创建自己的 TypeConverter 类.

Option 3: You could create your own TypeConverter class.

选项 4:(如果不是绑定的一部分,我会这样做)您可以添加自己的扩展方法:

Option 4: (The way I'd do it if not part of a binding) You can add your own extension methods:

static public class ConverterExtensions
{
    static public string ToFixedString(this double value)
    {
        return value.ToString("D");
    }
}

这会让你编写这样的代码:

Which would let you write code like this:

double d = 123.45;
string str = d.ToFixedString(); // str now equals "123"