2

我正在尝试创建一个继承自泛型类的 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 开了一张支持票,无论他们的解决方案是什么,我都会对其进行更新。

4

4 回答 4

3

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

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

于 2009-01-12T18:39:06.770 回答
1

不确定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 进行编译,它应该可以正常工作。

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

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

于 2008-12-30T17:02:00.143 回答
0

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

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

于 2008-12-30T16:45:48.490 回答
0

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

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 个类才能工作。

于 2011-11-10T22:18:34.440 回答