在XAML UWP中转换原始类型的最佳方式是什么?

时间:2022-04-05 16:27:40

In WPF, Silverlight or Windows Phone Silverlight we had a nice way to convert to any type from a string which is specified in XAML.

在WPF、Silverlight或Windows Phone Silverlight中,我们有一种很好的方式,可以从XAML中指定的字符串转换为任何类型。

The only thing we had to do is to subclass from TypeConverter and then mark particular properties or classes with TypeConverterAttribure specifying which converter the framework should use.

我们只需从TypeConverter中子类化,然后用typeconverattribure指定框架应该使用哪个转换器来标记特定的属性或类。

This feature is totally missing in Universal App XAML and the worst of all Universal App XAML has a very limited ability for value conversion.

这个功能在通用应用XAML中完全没有,最糟糕的是,通用应用XAML的价值转换能力非常有限。

You can use only bool, int, double, string and that's about it. If you create a custom control with dependency property of types char or long you cannot assign those properties in XAML.

你只能使用bool, int, double, string这是关于它的。如果使用char或long类型的依赖属性创建自定义控件,则不能在XAML中分配这些属性。

The compiler says that it cannot convert from string to char for example. What would be the best work around if you need to assign control's properties in XAML for types like char?

编译器说它不能从字符串转换为char。如果需要为char之类的类型在XAML中分配控件的属性,那么最好的工作是什么?

So far I came up with an idea to use {x:Bind Converter={StaticResource PropertyConverter}, ConverterParameter=Value} where basically the Value is converted to the target property type with the help of PropertyConverter which simply calls Convert.ChangeType inside.

到目前为止,我想到了使用{x:Bind Converter={StaticResource PropertyConverter}, ConverterParameter=Value}的方法,在PropertyConverter的帮助下,将值转换为目标属性类型,它只调用Convert。ChangeType里面。

The problem is that this approach does not work in XAMLs which are in Themes directory, which are basically control templates. It works only for UserControls.

问题是,这种方法在主题目录中的XAMLs中不起作用,XAMLs基本上是控制模板。它只适用于用户控件。

Is there a better way, ideally a universal way, to convert any type from string to a particular type when assigning in XAML?

在XAML中赋值时,是否有更好的方法(理想情况下是通用的方法)将任何类型从字符串转换为特定类型?

2 个解决方案

#1


0  

The Converter is the right approach. The reason why you cant use x:Bind in your theme xaml file (e.g inside datatemplace) is it require difference setup. You can use Binding in place of x:Bind.

转换器是正确的方法。不能在主题xaml文件(e)中使用x:Bind的原因。g在datatemplace内部)是否需要设置不同的设置。可以用Binding代替x:Bind。

sample:

示例:

Visibility="{Binding IsMenuOpen, Converter={StaticResource BooleanToVisibility}}"

#2


0  

Looks like starting in the Creator's Update you can now use this method to replicate this behavior, from the article:

看起来从创建者的更新开始,您现在可以使用此方法来复制此行为,从本文开始:

namespace CustomControlWithType
{
    [Windows.Foundation.Metadata.CreateFromString(MethodName = "CustomControlWithType.Location.ConvertToLatLong")]
    public class Location
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public double Altitude { get; set; }

        public static Location ConvertToLatLong(string rawString)
        {
            string[] coords = rawString.Split(',');

            var position = new Location();
            position.Latitude = Convert.ToDouble(coords[0]);
            position.Longitude = Convert.ToDouble(coords[1]);

            if (coords.Length > 2)
            {
                position.Altitude = Convert.ToDouble(coords[2]);
            }

            return position;
        }
    }
}

#1


0  

The Converter is the right approach. The reason why you cant use x:Bind in your theme xaml file (e.g inside datatemplace) is it require difference setup. You can use Binding in place of x:Bind.

转换器是正确的方法。不能在主题xaml文件(e)中使用x:Bind的原因。g在datatemplace内部)是否需要设置不同的设置。可以用Binding代替x:Bind。

sample:

示例:

Visibility="{Binding IsMenuOpen, Converter={StaticResource BooleanToVisibility}}"

#2


0  

Looks like starting in the Creator's Update you can now use this method to replicate this behavior, from the article:

看起来从创建者的更新开始,您现在可以使用此方法来复制此行为,从本文开始:

namespace CustomControlWithType
{
    [Windows.Foundation.Metadata.CreateFromString(MethodName = "CustomControlWithType.Location.ConvertToLatLong")]
    public class Location
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public double Altitude { get; set; }

        public static Location ConvertToLatLong(string rawString)
        {
            string[] coords = rawString.Split(',');

            var position = new Location();
            position.Latitude = Convert.ToDouble(coords[0]);
            position.Longitude = Convert.ToDouble(coords[1]);

            if (coords.Length > 2)
            {
                position.Altitude = Convert.ToDouble(coords[2]);
            }

            return position;
        }
    }
}