.NET 4.5 Metro 应用程序依赖属性

发布于 2024-12-07 01:47:39 字数 2959 浏览 0 评论 0原文

我无法让 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重载方法匹配 'Windows.UI.Xaml.DependencyProperty.Register(string, string, string, Windows.UI.Xaml.PropertyMetadata)' 有一些无效参数 C:\Users\aleksandar.toplek\documents\visual studio 11\Projects\Project - XX\XX\MainPage.xaml.cs 21 68 XX

错误 2 参数 2:无法从“System.Type”转换为“字符串”C:\Users\aleksandar.toplek\documents\visual studio 11\Projects\Project - XX\XX\MainPage。 xaml.cs 22 20 XX

错误 3 参数 3:无法从“System.Type”转换为“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>()));

代码编译但抛出异常运行时

mscorlib.dll 中发生了类型为“System.TypeInitializationException”的第一次机会异常

文件中的 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:  
...

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.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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烟酒忠诚 2024-12-14 01:47:40

上找到此问题的解决方案

您可以在依赖属性声明 MSDN 链接

you can find the solution for this problem on

Dependency Property declaration MSDN link

绝情姑娘 2024-12-14 01:47:40

好的。进一步观察后,我注意到,正如 Aleksandar Toplek 指出的那样,是 XamlTypeInfo.g.cs 代码负责实例化类型。它使用 IXamlType CreateXamlType 方法来处理 XAML 定义的类型并发现定义的依赖关系属性。当我将 SelectedUser DP 类型从 User 更改为 string 时,我注意到了这一点。我还注意到这个开关实现中的一个转换器。我认为当我在我的资源中声明它时它就会拾取它。

所以我进行了实验:

我将命名空间引用到我的自定义类:

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 的类型被识别并实例化。这只是第一部分。在我的依赖属性上,我有一个回调来在选择时调用一些逻辑。

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 已使用正确的类型正确注册。

让绑定按照我期望的方式工作工作,是我的下一个挑战,

希望这会有所帮助,

安德烈斯·奥利瓦雷斯。

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

挽手叙旧 2024-12-14 01:47:40

我还遇到了声明 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”类型的第一次机会异常

其他信息:未将对象引用设置为对象的实例。

希望这有帮助,

安德烈斯·奥利瓦雷斯

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

旧城烟雨 2024-12-14 01:47:40

"Object" 作为 propertyTypeName 传递对我的情况有帮助

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

Passing "Object" as propertyTypeName helped in my case

public static readonly DependencyProperty MyProperty = DependencyProperty.Register("My", "Object", typeof(MyControl).FullName, new PropertyMetadata(null));
叹倦 2024-12-14 01:47:40

我们不支持 WinRT C# 中的自定义类型属性声明。但您可以尝试这个解决方法。

这可能会解决问题。

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

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>())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文