3

仮想プロパティを持つ一般的なルックレス コントロールを作成しました。

public abstract class TestControlBase<TValue> : Control
{
    public static readonly DependencyProperty ValueProperty;

    static TestControlBase()
    {
        ValueProperty = DependencyProperty.Register("Value", typeof(TValue),
                                                    typeof(TestControlBase<TValue>));
    }

    protected TestControlBase()
    {
        Focusable = false;
        Value = default(TValue);
    }

    public virtual TValue Value
    {
        get
        {
            return (TValue)GetValue(ValueProperty);
        }
        set
        {
            SetValue(ValueProperty, value);
        }
    }
}

次に、それから派生したコントロールを作成し、Value プロパティをオーバーライドしました。

public class TestControl : TestControlBase<int>
{
    public override int Value
    {
        get
        {
            return base.Value;
        }
        set
        {
            base.Value = value;
        }
    }
}

したがって、ウィンドウ XAML で使用します。

    <TestControls:TestControl />

デザイナーでウィンドウを開くとすべて問題ありませんが、マウスカーソルをこの行またはデザイナーのこのコントロールに置くと、例外が発生します。

Exception has been thrown by the target of an invocation.
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)


Ambiguous match found.
   at System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
   at System.Type.GetProperty(String name)
   at MS.Internal.ComponentModel.DependencyPropertyKind.get_IsDirect()
   at MS.Internal.ComponentModel.DependencyPropertyKind.get_IsAttached()
   at MS.Internal.ComponentModel.APCustomTypeDescriptor.GetProperties(Attribute[] attributes)
   at MS.Internal.ComponentModel.APCustomTypeDescriptor.GetProperties()
   at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultExtendedTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetProperties()
   at System.ComponentModel.TypeDescriptor.GetPropertiesImpl(Object component, Attribute[] attributes, Boolean noCustomTypeDesc, Boolean noAttributes)
   at System.ComponentModel.TypeDescriptor.GetProperties(Object component)
   at MS.Internal.Model.ModelPropertyCollectionImpl.GetProperties(String propertyNameHint)
   at MS.Internal.Model.ModelPropertyCollectionImpl.<GetEnumerator>d__0.MoveNext()
   at MS.Internal.Designer.PropertyEditing.Model.ModelPropertyMerger.<GetFirstProperties>d__0.MoveNext()
   at MS.Internal.Designer.PropertyEditing.PropertyInspector.UpdateCategories(Selection selection)
   at MS.Internal.Designer.PropertyEditing.PropertyInspector.OnSelectionChangedIdle()

誰がこの問題を知っていますか? 説明してください:) WPFデザイナーがジェネリックを好まないことを除いて、私には考えがありません。Objectジェネリックをすべて置き換えればOKです。

4

3 に答える 3

2

イワン、

答えは少し遅れているかもしれませんが、他の人もそれを使用できます。私は同じ問題を抱えていて、これがバグであると読んだとき、非常にがっかりしました。しかし、ググった後、この種の継承の使用方法を示すブログを見つけました。xamlにいくつかのプロパティを含めて問題が解決したようです。お役に立てば幸いです。

于 2009-02-27T00:54:20.803 に答える
1

これはかなり古いものですが、私は(一種の)つまずきました...
よくわかりませんが、デフォルト値なしでnull不可の依存関係プロパティを設定しています。
設定されていないプロパティの値を決定する方法がわかりません。そこに問題があると思います。

于 2010-04-26T08:50:28.110 に答える
1

あなたにできることは何もないと思います。ただのバグです。WF コントロールを開発するときも同じことが起こります。

Windowsフォームコントロールを開発している私の同僚は、一般的ではない2番目のコントロールを作成します。これは単なる回避策ですが、機能します。デザイナーで作業したい場合は、ジェネリック (<>) にコメントを付けてください。作業が完了したら、コメントを外してください。

私はそれがあなたを助けることを願っています:)

于 2008-12-23T20:49:37.550 に答える