开发者

.NET 4.5 Metro Application DependencyProperty

I cant get DependencyProperty.Register to work.

It requires (string, string, string, PropertyMetadata) instead of string, Type, Type, UIPropertyMeatdata)...

I changed the UI-PropertyMetadata part but can't get string part of it to work. I tried typeof(T).ToString() and just "T" but it doesn't work.

My code looks like this

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>()));

And errors that i get

Error 1 The best overloaded method match for 'Windows.UI.Xaml.DependencyProperty.Register(string, string, string, Windows.UI.Xaml.Prope开发者_JAVA技巧rtyMetadata)' 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

-- EDIT --

When I do this

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

Code compiles but throws exception in runtime

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

in file 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:  
...


you can find the solution for this problem on

Dependency Property declaration MSDN link


OK. After observing a little more, I noticed, just as Aleksandar Toplek pointed out, it is the XamlTypeInfo.g.cs code responsible for instantiating types. It uses the IXamlType CreateXamlType method for BOTH XAML defined types and discovering defined Dependency Properties. I noticed this when I change my SelectedUser DP type from User to string. I also noticed one of my converters in this switch implementation. I figured it picked it up when I declared it in my resources.

So I experimented:

I referenced the namespace to my custom class:

xmlns:model="using:AccountManagement.Model"

I added a sample model to "register" this type into XamlTypeInfo.g.cs implementation:

<!--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;

I run my code and the type for my DP is recognized and instantiated. That's only the first part. On my dependency property I have a CallBack to invoke some logic upon selection.

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

I set up my bindings:

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

But this did not invoke the CallBack. :(. I eventually got it working using a handler that assigns the dp, and at that assignment it does trigger the callback. This confirms that the DP was properly registered with the proper type.

Getting the binding to work the way I expect it to work, is my next challenge.

Hope this helps,

Andres Olivares


I am also running into issues declaring a dependency property for a UserControl. Tried this:

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

Where User is a custom class and UsersView is a UserControl, but still get the following error:

A first chance exception of type 'System.NullReferenceException' occurred in accountreader.exe

Additional information: Object reference not set to an instance of an object.

Hope this helps,

Andres Olivares


Passing "Object" as propertyTypeName helped in my case

public static readonly DependencyProperty MyProperty = DependencyProperty.Register("My", "Object", typeof(MyControl).FullName, new PropertyMetadata(null));


We don't have support for the Custom Type property declaration in WinRT C#. But you can try this workaround.

This may solve the problem.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜