Visual Basic:&#39; <name>&#39;不是&#39; <classname>&#39;

时间:2016-08-16 14:49:22

标签: wpf vb.net xaml

在您阅读之前,在发布此问题之前,我已经看了几页:

'Button_Click' : is not a member of 'ButtonTest::MainPage'。这个问题关于C ++,据我所知,它没有用。根据唯一的答案,您需要在MainPage.xaml.h文件中定义MainPage :: Button_Click的原型作为类的成员。&#34;

visual basic error BC30456 'Form1' is not a member of 'WindowsApplication1'。这个问题也经历了同样的错误,但它是由其他东西引起的。 OP回答this answer,说它解决了问题:

  

如果您重命名了启动form1,则可能还需要更改“启动”表单设置。您可以找到此设置以打开“我的项目”&#39;在“解决方案资源管理器”中。选择“应用程序”部分,更改“启动表单”&#39;酌情。

就我而言,自从制作项目以来,我没有重命名任何内容,错误指向的代码是在&#39; popup.g.i.vb&#39;

中添加处理程序的行。

我也查看了MSDN:Creating Event Handlers for WPF Controls。该页面使用以下语法对单击的按钮作出反应:

<Button Click="Button2_Click" />

Sub Button2_Click(ByVal Sender As Object, ByVal e As RoutedEventArgs)
    'Code here
End Sub

这与我使用的语法完全相同。

我的代码

我目前正在使用WPF和Visual Basic制作程序,如果用户导致错误,我想弹出一个小窗口。

我有一个名为popup.xaml的WPF窗口,我有一个名为&#39; popup&#39;它继承自&#39; window&#39;。我想创建一个窗口,然后在用户点击“确定”时将其关闭。按钮。在C#中相当于以下内容:

popup errorWindow = new popup("Error message here");
errorWindow.ShowDialog();

调用此方法时,会调用popup()的覆盖,并初始化并将错误消息作为参数。

public partial class popup : Window
{
    public popup()
    {
        InitializeComponent();
    }

    public popup(string errorMessage)
    {
        InitializeComponent();
        this.errorMessage.Text = errorMessage;
    }

    private void okButton_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }
}

当&#39; ok&#39;单击按钮,调用okButton_Click()方法并关闭窗口。

在我的visual basic应用程序中,我有错误窗口的相同WPF代码:

<Window x:Class="FacialRecognitionVB.popup"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:FacialRecognitionVB"
    mc:Ignorable="d"
    Title="Error." MinHeight="150" MaxHeight="150" Height="150" MinWidth="300" MaxWidth="300" Width="300" >
<Grid>
    <Border VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Gray" />
    <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5,5,5,5" >
        <TextBlock x:Name="errorMessage" FontSize="18" FontWeight="Medium" Text="An error has occured." TextWrapping="Wrap" />
    </StackPanel>
    <Button x:Name="okButton" Template="{StaticResource ButtonTemplate}" Content="OK" Width="40" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="5,5,5,5" Click="okButton_Click" />
</Grid>

尽管如此,点击按钮的事件处理程序正在播放。我的弹出类的VB代码几乎完全相同:

Partial Public Class popup

    Inherits Window

    Public Sub Popup()
    End Sub

    Public Sub Popup(ByVal message As String)

        Dim errorMessage As TextBlock
        errorMessage.Text = message

    End Sub

    Private Sub okButton_Click(sender As Object, e As RoutedEventArgs)

        Me.Close()

    End Sub

End Class

我在MainWindow.vb中使用此代码创建了该类的实例:

    Dim errorWindow As New popup()
    errorWindow.Popup("An error has occured.")

当我尝试构建它时,我收到错误&#39; okButton_Click&#39;不是&#39; popup&#39;的成员。双击错误列表中的错误,我需要popup.g.i.vb并突出显示以下内容:

#ExternalSource ("..\..\popup.xaml", 14)
AddHandler okButton.Click, New RoutedEventHandler(AddressOf Me.okButton_Click)

#End ExternalSource

除了&#; okButton_Click&#39;之外,它不会给我任何进一步的信息。不是&#39; popup&#39;

的成员

我该如何解决这个问题?我的主页上实际上有几个按钮可以完美地使用visual basic!由于某种原因,这一个按钮不会。有什么不同我需要做的,因为它作为弹出窗口的一部分在一个单独的窗口上?

更新

我今天又看了一眼,我在MSDN上找到了'{name}' is not a member of '{classname}',它给出了以下两个解决方案:

  

1:检查会员姓名以确保其准确无误。

     

2:使用班级的实际成员。

我的按钮名为okButton,方法是okButton_Click()。该错误在popup.g.i.vb文件中突出显示,表示Me.okButton_Click不是我的弹出类的成员。我没有看到上面的任何一个修补程序是如何应用的,因为我使用正确的名称并且成员确实存在

1 个答案:

答案 0 :(得分:1)

您的构造函数未正确定义 - 构造函数在VB.NET中始终命名为New - 您需要从构造函数中调用InitializeComponent。您也在执行Dim errorMessage As TextBlock,因为它是在XAML中定义的,所以不应该这样做。

似乎有些东西破坏了XAML和代码隐藏文件之间的联系,并且删除与窗口相关的所有文件并从头开始可能更容易,但是你的代码背后应该是这样的:

Partial Public Class popup
    Inherits Window

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
    End Sub

    Public Sub New(ByVal message As String)
        ' This call is required by the designer.
        InitializeComponent()
        errorMessage.Text = message
    End Sub

    Private Sub okButton_Click(sender As Object, e As RoutedEventArgs)
        Me.Close()
    End Sub

End Class

在VS2015中,后面的代码没有PartialInherits Window部分,因此可能不需要这些部分,但可能不会伤害任何内容。

显示窗口也应该看起来像C#版本:

Dim errorWindow As New popup("An error has occured.")
errorWindow.ShowDialog()