我不能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:
...