我有一个新的VB.Net WPF应用程序.MainWindow.xaml只包含一个"测试"按钮:
<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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
后面的代码显示在这里.我所做的就是双击按钮,以便自动添加事件处理程序.我还将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
Run Code Online (Sandbox Code Playgroud)
当我构建它时,它不会编译.我得到的错误信息是:
Handles子句需要在包含类型或其基类型之一中定义的WithEvents变量.
当我从View命名空间中删除MainWindow时,一切都很好.很明显,命名空间是一个问题.我可以在命名空间中添加一个Window吗?我是否需要在应用程序中更改其他内容才能使其正常工作?
当您将它放入命名空间时,您正在破坏它.除了将VB.NET代码移到命名空间之外,还需要移动x:Class属性:
<Window x:Class="View.MainWindow" ... />
Run Code Online (Sandbox Code Playgroud)
和
Namespace View
Class MainWindow
'...
End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)
Visual Studio生成一个部分VB.NET类,它隐藏了您的代码.由于您将代码移到了另一个命名空间,因此它不再是Visual Studio生成的部分.
| 归档时间: |
|
| 查看次数: |
2003 次 |
| 最近记录: |