2012-07-11 51 views
1

我有一个新的VB.Net WPF应用程序。该MainWindow.xaml包含无非一个 '测试' 按钮更多:Visual Basic .NET:名称空间中的WPF窗口

<Window x:Class="MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Button Content="Test" 
       Name="btnTest" /> 
    </Grid> 
</Window> 

的Application.xaml是不变:

<Application x:Class="Application" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    StartupUri="MainWindow.xaml"> 
    <Application.Resources> 

    </Application.Resources> 
</Application> 

后面的代码如下所示。我所做的只是双击按钮,以便事件处理程序自动添加。我还将MainWindow添加到View命名空间。

Namespace View 
    Class MainWindow 
     ' A test button on the main window 
     Private Sub btnTest_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnTest.Click 
      MessageBox.Show("Hello world!") 
     End Sub 
    End Class 
End Namespace 

当我构建它时,它不编译。我得到的错误消息是:

句柄子句需要在包含类型或其基类型中定义的WithEvents变量。

当我从View命名空间中删除MainWindow时,一切都很好。很显然,命名空间是一个问题。我可以向命名空间添加一个窗口,并且是否需要在应用程序中更改其他内容以使其正常工作?

回答

5

当您将其放入命名空间时,您正打破部分类。除了移动VB.NET代码后面的命名空间,你需要移动x:Class属性,以及:

<Window x:Class="View.MainWindow" ... /> 

而且

Namespace View 
    Class MainWindow 
     '... 
    End Class 
End Namespace 

,Visual Studio生成的部分VB.NET类,它是一部分与您的代码背后。由于您将代码移到了另一个名称空间,因此它不再是Visual Studio生成的部分。

相关问题