2012-07-11 53 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生成的部分。

相關問題