2

仕事ができませんDependencyProperty.Register

(string, string, string, PropertyMetadata)代わりに必要ですstring, Type, Type, UIPropertyMeatdata)...

UI-PropertyMetadata部分を変更しましたが、文字列部分を機能させることができません。試しtypeof(T).ToString()てみまし"T"たが、うまくいきません。

私のコードは次のようになります

public ObservableCollection<RingSegment> RingSegments {
    get { return (ObservableCollection<RingSegment>)GetValue(RingSegmentsProperty); }
    set { SetValue(RingSegmentsProperty, value); }
}
public static readonly DependencyProperty RingSegmentsProperty = DependencyProperty.Register(
    "RingSegments", typeof(ObservableCollection<RingSegment>), typeof(MainPage), new PropertyMetadata(new ObservableCollection<RingSegment>()));

そして私が得るエラー

Error 1 The best overloaded method match for 'Windows.UI.Xaml.DependencyProperty.Register(string, string, string, Windows.UI.Xaml.PropertyMetadata)' has some invalid arguments C:\Users\aleksandar.toplek\documents\visual studio 11\Projects\Project - XX\XX\MainPage.xaml.cs 21 68 XX

Error 2 Argument 2: cannot convert from 'System.Type' to 'string' C:\Users\aleksandar.toplek\documents\visual studio 11\Projects\Project - XX\XX\MainPage.xaml.cs 22 20 XX

Error 3 Argument 3: cannot convert from 'System.Type' to 'string' C:\Users\aleksandar.toplek\documents\visual studio 11\Projects\Project - XX\XX\MainPage.xaml.cs 22 63 XX

- 編集 -

私がこれをするとき

public static readonly DependencyProperty RingSegmentsProperty = DependencyProperty.Register(
    "RingSegments", "ObservableCollection<RingSegment>", "MainPage", new PropertyMetadata(new ObservableCollection<RingSegment>()));

コードはコンパイルされますが、実行時に例外がスローされます

A first chance exception of type 'System.TypeInitializationException' occurred in mscorlib.dll

ファイル内XamplTypeIngo.g.cs

System.TypeInitializationException was unhandled by user code
  Message=The type initializer for 'XX.MainPage' threw an exception.
  Source=mscorlib
  TypeName=XX.MainPage
  StackTrace:
       at System.Runtime.CompilerServices.RuntimeHelpers._RunClassConstructor(RuntimeType type)
       at System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(RuntimeTypeHandle type)
       at Disk_Visualizer.XamlTypeInfo.XamlUserType.RunInitializer() in c:\Users\aleksandar.toplek\Documents\Visual Studio 11\Projects\Project - XX\XX\obj\Debug\XamlTypeInfo.g.cs:line 277
  InnerException: System.NullReferenceException
       Message=Object reference not set to an instance of an object.
       Source=Windows.UI.Xaml
       StackTrace:
            at Windows.UI.Xaml.DependencyProperty.Register(String name, String propertyTypeName, String ownerTypeName, PropertyMetadata typeMetadata)
            at Disk_Visualizer.MainPage..cctor() in c:\Users\aleksandar.toplek\Documents\Visual Studio 11\Projects\Project - XX\XX\MainPage.xaml.cs:line 21
       InnerException:  
...
4

5 に答える 5

4

あなたはこの問題の解決策を見つけることができます

依存関係プロパティの宣言MSDNリンク

于 2011-10-19T13:43:37.727 に答える
1

わかった。もう少し観察した後、Aleksandar Toplekが指摘したように、型のインスタンス化を担当するのはXamlTypeInfo.g.csコードであることに気付きました。これは、XAMLで定義されたタイプと定義された依存関係プロパティの両方にIXamlTypeCreateXamlTypeメソッドを使用します。SelectedUserDPタイプをUserからstringに変更したときにこれに気づきました。また、このスイッチの実装でコンバーターの1つに気づきました。私が自分のリソースでそれを宣言したとき、私はそれがそれを拾ったと思いました。

だから私は実験しました:

名前空間をカスタムクラスに参照しました。

xmlns:model="using:AccountManagement.Model"

このタイプをXamlTypeInfo.g.cs実装に「登録」するためのサンプルモデルを追加しました。

<!--Sample Model-->
<model:User x:Key="sampleUser"/>

// Generates the following case in CreateXamlType 
case "AccountManagement.Model.User":
   userType = new XamlUserType(this, typeName, typeof(AccountManagement.Model.User), GetXamlTypeByName("Windows.Foundation.Object"));
   userType.Activator = Activate_1_User;
   xamlType = userType;
   break;

コードを実行すると、DPのタイプが認識され、インスタンス化されます。それは最初の部分にすぎません。依存関係プロパティには、選択時にロジックを呼び出すCallBackがあります。

public static readonly DependencyProperty SelectedUserProperty =
        DependencyProperty.Register("SelectedUser", typeof(User).FullName, typeof(UsersView).FullName,
        new PropertyMetadata(null, OnSelectedUserChanged));

バインディングを設定しました:

<GridView 
   ... 
   SelectedItem="{Binding SelectedUser, ElementName=root}"
/>
<!--root being my UserControl (x:Name="root")-->

しかし、これはコールバックを呼び出しませんでした。:(。私は最終的にdpを割り当てるハンドラーを使用して動作させ、その割り当てでコールバックをトリガーします。これにより、DPが適切なタイプで適切に登録されたことが確認されます。

バインディングを期待どおりに機能させることが、私の次の課題です。

お役に立てれば、

Andres Olivares

于 2011-10-04T04:50:40.580 に答える
0

WinRT C#のカスタムタイププロパティ宣言はサポートされていません。ただし、この回避策を試すことができます。

これで問題が解決する場合があります。

public static readonly DependencyProperty RingSegmentsProperty = DependencyProperty.Register(
    "RingSegments", "ObservableCollection<"Object>", "MainPage", new PropertyMetadata(new ObservableCollection<"Object>())
于 2011-09-30T04:15:13.380 に答える
0

また、UserControlの依存関係プロパティを宣言する際に問題が発生しています。これを試しました:

public static readonly DependencyProperty SelectedUserProperty =
DependencyProperty.Register("SelectedUser", typeof(User).FullName, typeof(UsersView).FullName,
        new PropertyMetadata(default(User), OnSelectedUserChanged));

Userがカスタムクラスで、UsersViewがUserControlであるが、それでも次のエラーが発生する場合:

accountreader.exeでタイプ「System.NullReferenceException」の最初のチャンスの例外が発生しました

追加情報:オブジェクト参照がオブジェクトのインスタンスに設定されていません。

お役に立てれば、

Andres Olivares

于 2011-10-04T02:59:49.253 に答える
0

私の場合は助けられたよう"Object"に合格propertyTypeName

public static readonly DependencyProperty MyProperty = DependencyProperty.Register("My", "Object", typeof(MyControl).FullName, new PropertyMetadata(null));
于 2012-02-19T18:55:59.280 に答える