【问题标题】:.NET 4.5 Metro Application DependencyProperty.NET 4.5 Metro 应用程序依赖属性
【发布时间】: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


【解决方案1】:

你可以找到这个问题的解决方案

Dependency Property declaration MSDN link

【讨论】:

    【解决方案2】:

    好的。在观察了一点之后,我注意到,正如 Aleksandar Toplek 指出的那样,它是负责实例化类型的 XamlTypeInfo.g.cs 代码。它对 XAML 定义的类型和发现定义的依赖属性都使用 IXamlType CreateXamlType 方法。当我将 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 的类型被识别和实例化。这只是第一部分。在我的依赖属性上,我有一个 CallBack 在选择时调用一些逻辑。

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

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

    希望这会有所帮助,

    安德烈斯·奥利瓦雷斯

    【讨论】:

      【解决方案3】:

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

      附加信息:对象引用未设置为对象的实例。

      希望这会有所帮助,

      安德烈斯·奥利瓦雷斯

      【讨论】:

        【解决方案4】:

        "Object" 传递为propertyTypeName 对我有帮助

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

        【讨论】:

          【解决方案5】:

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

          这可能会解决问题。

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

          【讨论】:

          • 我不知道我是不是做错了什么,但我仍然收到XamplTypeIngo.g.cs 异常...
          猜你喜欢
          • 2010-11-20
          • 1970-01-01
          • 1970-01-01
          • 2015-06-27
          • 1970-01-01
          • 2020-07-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多