【问题标题】:UserControl that has a generic class in its inheritance treeUserControl 在其继承树中有一个泛型类
【发布时间】:2008-12-30 16:39:00
【问题描述】:

我正在尝试创建一个继承自通用类的 UserControl。它不是直接继承自一个泛型类,而是通过一个不使用泛型的中间类。这会在运行时编译并运行,但在设计时出现错误。

这是我的通用父类:

Public Class GenericParent(Of T)
    Inherits UserControl
End Class

这是我的非泛型父类:

Public Class NonGenericParent
    Inherits GenericParent(Of String)
End Class

这是我的 XAML:

<local:NonGenericParent x:Class="SilverlightApplication5.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:SilverlightApplication5"
    Width="400" Height="300">
    <StackPanel>
        <Button Content="Hello"/>
    </StackPanel>
</local:NonGenericParent>

IntelliSense 解析器给出以下错误:

  1. 在“NonGenericParent”类型中找不到属性“Width”。
  2. 在“NonGenericParent”类型中找不到属性“Height”。
  3. “NonGenericParent”类型不支持直接内容。

就好像 IntelliSense 无法查看 GenericParent 类之后的继承树。我试过直接在 SilverlightApplication5.Page 类、NonGenericParent 类上指定 ContentPropertyAttribute,但它不起作用。

我了解到 Silverlight 2.0 不支持 TypeArguments 属性。这就是我创建中间 NonGenericParent 类的原因。

如果有人对如何消除这些错误有任何想法,我很想听听。

更新:我们已经向 MSFT 开具了支持票证,无论他们的解决方案是什么,我都会对此进行更新。

【问题讨论】:

    标签: silverlight generics xaml intellisense silverlight-2.0


    【解决方案1】:

    我们已收到 Microsoft 的消息,称此问题不太可能在未来的版本中得到解决。在他们围绕试图找到责任组提出问题后,似乎这个问题属于他们的 WPF 开发人员组,这就是“不会修复它”答案的来源。

    与此同时,我们更新了代码以从父类中抽出泛型,直到我猜是 XAML 2009。

    【讨论】:

      【解决方案2】:

      不确定silverlight,但这可以在c#中按预期编译和运行:

      
      class GenericObject[T] : UserControl
      {
      }
      
      class StaticObject : GenericObject[Int32]
      {
          public Int32 wide { get { return this.Width; } }
      }
      
      private void Form1_Load(object sender, EventArgs e)
      {
          StaticObject so = new StaticObject();
          this.Text = so.wide.ToString();
      }
      

      因此,如果它针对 clr 进行编译,它应该可以正常工作。

      正如您所建议的那样,可能只是一个智能感知错误。通常我会建议不要忽略编译器警告,但在这种情况下,警告似乎无效。

      编辑:用方括号替换尖括号会导致它们被剥离。

      【讨论】:

      • 我希望能够隐藏此消息,但我不知道该怎么做。根据 IntelliSense,它实际上是一个错误,这就是它在错误列表窗口中的显示方式。
      【解决方案3】:

      尽管是 2.0 silverlight(尤其是针对 silverlight 的 VS2008 调整)仍然非常年轻。 IDE 的东西仍然有怪癖。

      即使在成功构建之后,您仍然遇到问题吗?

      【讨论】:

      • 该问题在构建后暂时消失,然后一旦 IntelliSense 赶上备份错误再次出现。在我的开发工作站上,这需要 1 秒到 20 秒。有几次我很兴奋,以为我已经解决了问题,只是在长时间延迟后它又出现了;)
      【解决方案4】:

      这篇博文似乎与您的问题有关:

      http://blogs.msdn.com/b/wpfsldesigner/archive/2010/01/22/known-issue-controls-deriving-from-a-generic-base-class-must-be-in-separate-assembly.aspx

      对于 Silverlight,您似乎必须有 3 个类才能工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-19
        • 2011-08-19
        • 1970-01-01
        • 2018-09-27
        • 2015-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多