【发布时间】:2011-09-28 17:25:51
【问题描述】:
我无法让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:
...
【问题讨论】:
-
从代码来看,好像是个bug。将 typeof(xxxx) 分配给静态成员时会得到什么类型。那么在编译过程中会发生什么?
-
这些错误来自编译器。我不明白你的意思
when you assign the typeof(xxxx) to static members -
不是在参数中说 typeof(MainPage) ,而是可以添加一个静态成员,DependencyProperty 可以在其参数中使用。似乎编译器认为 typeof(xx) 应该是字符串,我只是想知道它是否认为对于显式声明的成员也是一样的。
-
F1 将我带到MSND Docs 现在,这意味着编译器没有错误......但我无法正确获取字符串。还有其他人有这个问题吗?没有人使用 DependencyPropertyes?
-
又是一场噩梦,代码发生了很多变化,首先是 wpf,然后是 silverlight,现在是 winrt,当您的应用程序正确时,将会有新的东西出现。
标签: c# microsoft-metro windows-runtime .net-4.5