WPF解决方案有点奇怪

时间:2014-10-04 16:27:09

标签: wpf vb.net

在基于WPF的解决方案中,这种情况发生在几个旧项目或新项目中。我刚刚添加了一个名为Dashboard的窗口,并在生成的'Dashboard.xaml.vb file, I entered the following code. Note it normally pastes InitializeComponent in for you when you create Sub New`中添加了一个窗口,但现在却没有。所以,我有:

Public Class DashBoard

    Public Sub New()
        InitializeComponent()
    End Sub

End Class

Ans编译器抱怨说:

  

错误15'未声明InitializeComponent'。它可能无法访问   由于其保护水平。

就好像IDE不知道这是Window背后的代码。缺少某种部分类或链接,但在vbproj文件中,我们仍然找到了链接:

<Compile Include="Bridge.xaml.vb">
  <DependentUpon>Bridge.xaml</DependentUpon>
</Compile>

我在所有项目中使用PostSharp进行自动错误记录,但它从未给我带来麻烦。它表现得非常好。

增加:

`Dashboard.xaml'看起来像:

<Window x:Class="DashBoard"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DashBoard" Height="300" Width="300">
    <Grid>

    </Grid>
</Window>

和`Dashboard.xaml.vb'看起来像这样:

Public Class DashBoard

End Class

1 个答案:

答案 0 :(得分:1)

要将Dashboard.xaml.vb文件与现有Dashboard.xaml文件关联,您需要:

  1. 声明课程partial
  2. 使其继承自WPF window基类,例如:
  3. Public Partial Class Dashboard Inherits Window

    只要您的班级不继承Window,就会发现它找不到InitializeComponent方法。

    但是,这并不能解释为什么不会自动生成必要的代码部分。 Dashboard.xaml文件的外观如何?