【问题标题】:Set a property from type 'type' with global style in XAML在 XAML 中设置具有全局样式的类型“类型”的属性
【发布时间】:2012-04-19 07:48:57
【问题描述】:

我想在 xaml 中设置类型“类型”的依赖属性。这可以正常工作,但是当我以隐式或显式样式设置此值时,将引发异常(未处理的异常)。

我创建了一个空的 Silverlight 应用程序并添加了一个用户控件 (DataFormControl)。 下面是这个控件的代码:

    public DataFormControl()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(DataFormControl), null);
    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }

    public static readonly DependencyProperty TypeToReflectProperty = DependencyProperty.Register("TypeToReflect", typeof(Type), typeof(DataFormControl), null);
    public Type TypeToReflect
    {
        get { return (Type)GetValue(TypeToReflectProperty); }
        set { SetValue(TypeToReflectProperty, value); }
    }

    public string GetCombo()
    {
        string returnValue = (Title ?? "no title") + " ; " + (TypeToReflect != null ? TypeToReflect.Name : "unkown Type");
        return returnValue;
    }


    private void Refresh_Button(object sender, RoutedEventArgs e)
    {
        this.ResultBox.Text = GetCombo();
    }

这里是 XAML 代码:

<Grid x:Name="LayoutRoot">
    <StackPanel Orientation="Horizontal">
        <Button Click="Refresh_Button">Refresh</Button>            
        <TextBlock x:Name="ResultBox" />
    </StackPanel>
</Grid>

现在问题出现在引用 this 并使用全局样式的控件中:

<StackPanel>
        <StackPanel.Resources>
            <Style TargetType="local:DataFormControl">
                <Setter Property="Title" Value="Implicit Name" />
                <Setter Property="TypeToReflect" Value="local:DataFormControl" />
            </Style>
        </StackPanel.Resources>
        <TextBlock FontWeight="Bold">Test App</TextBlock>

        <local:DataFormControl Title="123" />
        <local:DataFormControl Title="Kuh" />
        <local:DataFormControl TypeToReflect="local:DataFormControl" />
        <local:DataFormControl  />
    </StackPanel>

如果我删除“TypeToReflect”-Setter,那么一切正常。 title 属性的全局样式也可以正常工作。

这是一个错误还是有解决方法?

我需要该类型,因为我想对其使用反射。

编辑:

异常信息:

Message is always.  [Line: 0 Position: 0]  
ExceptionType: Unhandled Exception  
ExceptionObject: XamlParseException

堆栈跟踪

 at MS.Internal.XcpImports.CheckHResult(UInt32 hr)  
   at MS.Internal.XcpImports.ConvertStringToTypedCValue(IntPtr pContext, UInt32 cClrTypeName, String clrTypeName, UInt32 cValue, String value, CValue& outVal, Int32& typeIndex)  
   at MS.Internal.SilverlightTypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)  
   at MS.Internal.XcpImports.GetManagedPropertyValueFromStyle(Boolean useBuiltInStyle, IManagedPeerBase obj, DependencyProperty property, Object& value)  
   at System.Windows.FrameworkElement.GetValueFromStyle(DependencyProperty property, Object& value)  
   at System.Windows.DependencyObject.EvaluateBaseValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)  
   at System.Windows.DependencyObject.EvaluateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry newEntry, ValueOperation operation)  
   at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)  
   at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty property)  
   at MS.Internal.FrameworkCallbacks.InvalidateProperty(IntPtr nativeTarget, UInt32 propertyId)  

InnerException 为空。

【问题讨论】:

  • 异常的InnerException是什么?堆栈跟踪是什么样子的?
  • 看看这个类似问题的答案stackoverflow.com/a/677285/1161647
  • stackoverflow.com/questions/6028276/… 你是对的。有一个类似的解决方案。
  • 在这个例子中,样式的类型将被定义。这目前对我有用。我可以使用显式和隐式样式。但是 TypeToReflect-Property 的 Setter 行不起作用。我在引用的链接上找不到解释。

标签: c# silverlight xaml


【解决方案1】:

你可以写:

{x:Type Type}

没有更多的文字。

【讨论】:

  • 我认为在 silverlight 中我不能使用 x:Type 关键字。
猜你喜欢
  • 2017-11-20
  • 2020-05-23
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 2019-05-18
  • 1970-01-01
相关资源
最近更新 更多