为什么我创建的新WPF窗口没有继承自Window类?

6
当我使用“添加项目”对话框创建新窗口时,我创建的窗口(例如NewWindow)并未继承自Window类型。它只具有与Object类型相同的接口。例如:
下面是我的弹出窗口代码。该代码仅看到IHavePassword成员,而不是`LoginPopup'的其余成员,如其控件。
Public Class LoginPopup
    Implements IHavePassword

    Public ReadOnly Property Password As SecureString Implements IHavePassword.Password
        Get
            'Return Me.PasswordBox.????
        End Get
    End Property

    Public Event PasswordChanged As RoutedEventHandler Implements IHavePassword.PasswordChanged
    Public Sub PassWordChangedHandler(sender As Object, e As EventArgs)
        PasswordChangedEvent(sender, e)
    End Sub

    Public Sub Close() Implements IHavePassword.Close
        Throw New NotImplementedException
    End Sub

End Class

哦,这里还需要XAML:

<Window x:Class="ApptEase.Client.Prism.Views.LoginPopup"
....
<StackPanel Orientation="Vertical" Margin="0">
    <DockPanel LastChildFill="True">
        <TextBlock Text="{Binding Path=UsernameLabel}" DockPanel.Dock="Left" TextAlignment="Right" Margin="5,9,5,5" MinWidth="70" />
        <TextBox Text="{Binding Path=Username, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" Width="Auto" Margin="5" />
    </DockPanel>
    <DockPanel LastChildFill="True">
        <TextBlock Text="{Binding Path=PasswordLabel}" DockPanel.Dock="Left" TextAlignment="Right" Margin="5" MinWidth="70" />
        <PasswordBox x:Name="PasswordBox" PasswordChanged="PassWordChangedHandler" Width="Auto" Margin="5" />
    </DockPanel>
    <DockPanel LastChildFill="True" Height="59">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Content="Log in" Command="{Binding Path=LoginCommand}" CommandParameter="{Binding ElementName=This}" Margin="5" Padding="15,10,15,10" />
            <Button Content="Cancel" Command="{Binding Path=CancelCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" Margin="5" Padding="15,10,15,10"/>
        </StackPanel>
    </DockPanel>
</StackPanel>

Me上的IntelliSense列出了IHavePassword的成员,例如:

enter image description here

我希望在这里看到控件和基本的Window成员,而不是接口的成员。我该如何解决这个问题?


1
有没有谁识字可以解释一下这个踩的原因?我的问题非常清楚,我问的是什么。这是个人问题还是别的什么原因? - ProfK
1
只是为了记录:我没有给你的问题投反对票。 - haindl
1
仅供记录,我从未怀疑过那是你,@haindl。 - ProfK
1
这是项目/解决方案特定的问题还是VS安装特定的问题?也就是说,创建一个新项目并添加一个窗口是否会重现此行为?另外,我自己无法重现它。 - Grx70
4个回答

4
我认为您只是忘记将类与您的命名空间括起来了:
Namespace ApptEase.Client.Prism.Views
    Public Class LoginPopup
        Implements IHavePassword

        '...

    End Class
End Namespace

1
不,我没有忘记,“ApptEase.Client.Prism.Views.LoginPopup”不是命名空间,而是类型名称。我将其包含在“namespace ApptEase.Client.Prism.Views”中,这是Visual Studio自动转换为“namespace Views”的方式。也就是说,它会从任何“namespace”语句中删除根命名空间,只留下扩展到根的部分,在这种情况下是“Views”。 - ProfK
1
@ProfK 您关于类型名称的观点是完全正确的。我犯了一个快速复制/粘贴错误,并编辑了我的帖子以进行更正。不幸的是,我更多地是一名C#开发人员,而我上一次使用VB.NET已经过去很多年了。在我的快速测试中,错误的命名空间恰好是您遇到问题的原因,但我想这种错误还有更多潜在的原因。嗯,我认为这就是VB.NET所具有的“魔力”:如果它能工作,那就太棒了。如果不能,祝你好运。根本原因当然是VB.NET没有从“Window”继承LoginPopup,所以它只是从Object派生。 - haindl
1
它确实从Window继承,也在幕后进行。 - ProfK
1
@ProfK 如果您打开 Visual Studio 对象浏览器并选择您的 LoginPopup 类,会看到什么?您是否看到 Public Class LoginPopup Inherits System.Windows.Window?当您展开“基本类型”节点时,您看到了什么?您看到了 IHavePasswordWindow 吗?(当然,如果您愿意,也可以使用任何其他工具,如 Reflector、ILSpy、JustDecompile、dotPeek 或 ILDASM。) - haindl
1
不,它只继承自对象,但是在幕后生成的LoginPopup部分类的其他部分,在obj\Debug\Views中称为LoginPopup.g.vb,包含声明Inherits System.Windows.Window,第三部分称为LoginPopup.g.i.vb也包含相同的声明'Partial Public Class LoginPopup。现在所有基于Window`的视图都是如此。我将Partial添加到主类中,在解决方案资源管理器中可见的那个类中,没有任何区别。我首先用C#进行重写,这样我就可以快速完成2天过期的演示。 - ProfK
显示剩余3条评论

1

为了确保,您在评论中说LoginPopup是一个部分类,但在您的代码中,您没有“Partial语句”。我主要是C#开发人员,所以不确定,但应该有“Partial Public LoginPopup”吧?


VB的神秘力量会处理这个。 - ProfK

1
回答正在被问的问题。
添加一个新的“窗口…”而不是使用“添加项…”并创建一个新的通用类。然后添加您的自定义代码。

我特别添加一个窗口,而不是一个新的通用类,无论它是什么。 "添加项目...->窗口" 和 "添加...->窗口" 是完全相同的,它们都添加了一个完整配备的窗口。 - ProfK
当命名空间与xaml文件中的不匹配时,我会得到相同的行为。在LoginPopup类声明周围添加命名空间,这为我解决了问题。 - NRiding

0

首先,这与部分类或从Window继承无关,正如许多评论中所建议的那样。 VB WPF窗口的项模板会正确处理所有内容;我们很久以前就停止了“手工编码”UI窗口和控件。

然后,我已经放弃了找出是什么原因导致了这个问题,除了项目文件或VS本身存在问题之外。我创建了一个新的VB WPF项目,在这个项目中,当我添加一个新的Window时,一切都很好,即继承是正确的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接