直接在xaml中声明Vector3值

nfg76nw0  于 2022-12-07  发布在  其他
关注(0)|答案(2)|浏览(103)

我想在ResourcesDictionary中将Vector3值实现为常量值,但不幸的是,出现了一个错误,说“Vector3不支持直接内容”。有没有办法做到这一点?

我希望Vector3可以直接应用在xaml中,如x:Double、x:String等

cgh8pdjw

cgh8pdjw1#

目前UWP xaml中仅支持四种内部数据类型。

*x:布尔值
*x:字符串
*x:双精度浮点数
*x:整数32

您可以从本文档XAML intrinsic data types中获得更多信息。
所以你不能在xaml中使用Vector3,你需要在你的代码后面创建这个结构。

gzszwxb4

gzszwxb42#

正如上面提到的@JunjieZhu-MSFT,除了这四个值之外,没有办法创建数据类型结构值,所以我的工作如下:

<x:string x:key="ShadowOffsetXY">0 2 0</x:string>

然后,我创建了一个类来实现IValueConverter,将此字符串值转换为Vector 3 Value

public class StringToVector3Converter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value != null && value.GetType() == typeof(string))
            {
                var values = ((string)value).Split(" ");
                return new Vector3(float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]));
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

然后在要从中绑定此源值的控件的属性中使用此转换器

<ui:Effects.Shadow>
   <media:AttachedCardShadow Offset="{Binding Source="{StaticResource ShadowOffsetXY}, Converter={StaticResource StringToVector3Converter}" }" />
</ui:Effects.Shadow>

相关问题