Xaml 为什么在WinRT中设置枚举依赖项属性的默认值失败?
我试图使用枚举作为依赖属性的类型,但是任何默认设置似乎都失败了 编译成功了,但是当我尝试在XAML中使用该对象时,我得到了一个“指定的强制转换无效”错误和对象上的蓝色下划线 使用WinRT的VS2012 我做错了什么 以下是我正在使用的代码:Xaml 为什么在WinRT中设置枚举依赖项属性的默认值失败?,xaml,windows-runtime,dependency-properties,winrt-xaml,Xaml,Windows Runtime,Dependency Properties,Winrt Xaml,我试图使用枚举作为依赖属性的类型,但是任何默认设置似乎都失败了 编译成功了,但是当我尝试在XAML中使用该对象时,我得到了一个“指定的强制转换无效”错误和对象上的蓝色下划线 使用WinRT的VS2012 我做错了什么 以下是我正在使用的代码: public class WheelPanel : Panel { public enum ItemAlignmentOptions { Left, Center, Right } /// <summ
public class WheelPanel : Panel
{
public enum ItemAlignmentOptions
{
Left, Center, Right
}
/// <summary>
/// Identifies the <see cref="ItemAlignment" /> dependency property.
/// </summary>
// This fails using null
//public static readonly DependencyProperty ItemAlignmentProperty = DependencyProperty.Register(
// ItemAlignmentPropertyName,
// typeof(ItemAlignmentOptions),
// typeof(WheelPanel),
// new PropertyMetadata(null,new PropertyChangedCallback(ItemAlignmentChanged)));
// This fails using a default value
//public static readonly DependencyProperty ItemAlignmentProperty = DependencyProperty.Register(
// ItemAlignmentPropertyName,
// typeof(ItemAlignmentOptions),
// typeof(WheelPanel),
// new PropertyMetadata(ItemAlignmentOptions.Center,new PropertyChangedCallback(ItemAlignmentChanged)));
// This works!
public static readonly DependencyProperty ItemAlignmentProperty = DependencyProperty.Register(
ItemAlignmentPropertyName,
typeof(ItemAlignmentOptions),
typeof(WheelPanel),
new PropertyMetadata(new PropertyChangedCallback(ItemAlignmentChanged)));
/// <summary>
/// The <see cref="ItemAlignment" /> dependency property's name.
/// </summary>
public const string ItemAlignmentPropertyName = "ItemAlignment";
/// <summary>
/// Gets or sets the value of the <see cref="ItemAlignment" />
/// property. This is a dependency property.
/// </summary>
public ItemAlignmentOptions ItemAlignment
{
get { return (ItemAlignmentOptions)GetValue(ItemAlignmentProperty); }
set { SetValue(ItemAlignmentProperty, value); }
}
公共级车轮面板:面板
{
公共枚举项对齐选项
{
左、中、右
}
///
///标识依赖项属性。
///
//使用null时失败
//公共静态只读DependencyProperty ItemAlignmentProperty=DependencyProperty.Register(
//ItemAlignmentPropertyName,
//类型(ItemAlignmentOptions),
//类型(车轮板),
//新PropertyMetadata(null,新PropertyChangedCallback(ItemAlignmentChanged));
//使用默认值时失败
//公共静态只读DependencyProperty ItemAlignmentProperty=DependencyProperty.Register(
//ItemAlignmentPropertyName,
//类型(ItemAlignmentOptions),
//类型(车轮板),
//新的PropertyMetadata(ItemAlignmentOptions.Center,新的PropertyChangedCallback(ItemAlignmentChanged));
//这管用!
公共静态只读DependencyProperty ItemAlignmentProperty=DependencyProperty.Register(
ItemAlignmentPropertyName,
类型(ItemAlignmentOptions),
类型(车轮板),
新PropertyMetadata(新PropertyChangedCallback(ItemAlignmentChanged));
///
///依赖项属性的名称。
///
public const string ItemAlignmentPropertyName=“ItemAlignment”;
///
///获取或设置
///属性。这是一个依赖属性。
///
公共项目对齐选项项目对齐
{
获取{return(ItemAlignmentOptions)GetValue(ItemAlignmentProperty);}
set{SetValue(ItemAlignmentProperty,value);}
}
尝试使用“1”作为值。枚举的基本类型是Int32(默认情况下),可能它需要一个整数作为默认值。这似乎有效。FWIW-当我在XAML中从默认值更改为非默认值时,会出现一个简短的com错误,但在编译时它会消失。可能相关,也可能不相关。