.NET 4.5 Metro 应用程序依赖属性
我无法让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
上找到此问题的解决方案
您可以在依赖属性声明 MSDN 链接
you can find the solution for this problem on
Dependency Property declaration MSDN link
好的。进一步观察后,我注意到,正如 Aleksandar Toplek 指出的那样,是 XamlTypeInfo.g.cs 代码负责实例化类型。它使用 IXamlType CreateXamlType 方法来处理 XAML 定义的类型并发现定义的依赖关系属性。当我将 SelectedUser DP 类型从 User 更改为 string 时,我注意到了这一点。我还注意到这个开关实现中的一个转换器。我认为当我在我的资源中声明它时它就会拾取它。
所以我进行了实验:
我将命名空间引用到我的自定义类:
我添加了一个示例模型以将此类型“注册”到 XamlTypeInfo.g.cs 实现中:
我运行我的代码,我的 DP 的类型被识别并实例化。这只是第一部分。在我的依赖属性上,我有一个回调来在选择时调用一些逻辑。
我设置了绑定:
但这并没有调用回调。 :(。我最终使用分配 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:
I added a sample model to "register" this type into XamlTypeInfo.g.cs implementation:
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.
I set up my bindings:
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
我还遇到了声明 UserControl 的依赖属性的问题。尝试过:
其中 User 是自定义类,UsersView 是 UserControl,但仍然出现以下错误:
希望这有帮助,
安德烈斯·奥利瓦雷斯
I am also running into issues declaring a dependency property for a UserControl. Tried this:
Where User is a custom class and UsersView is a UserControl, but still get the following error:
Hope this helps,
Andres Olivares
将
"Object"
作为propertyTypeName
传递对我的情况有帮助Passing
"Object"
aspropertyTypeName
helped in my case我们不支持 WinRT C# 中的自定义类型属性声明。但您可以尝试这个解决方法。
这可能会解决问题。
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.