2014-10-04 64 views
-2

這發生在我的基於WPF的解決方案中的幾個新舊項目中。我剛剛添加了一個名爲Dashboard的窗口,並在生成的'Dashboard.xaml.vb file, I entered the following code. Note it normally pastes InitializeComponent in for you when you create Sub New`中,但現在沒有。所以,我有:WPF解決方案非常奇怪

Public Class DashBoard 

    Public Sub New() 
     InitializeComponent() 
    End Sub 

End Class 

答編譯器會抱怨說:

錯誤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

要涉及你Dashboard.xaml.vb文件與現有的Dashboard.xaml文件,你需要:

  1. 聲明類partial
  2. 讓它從WPF window基類繼承,例如:

Public Partial Class Dashboard Inherits Window

只要你的類不從Window繼承,很明顯,它沒有找到InitializeComponent方法。

但是,這並不能解釋爲什麼不自動生成必要的代碼部分。 Dashboard.xaml文件是怎樣的?

+0

那不行,我試過了。 VS應該在VB.NET項目中爲黑盒魔術做所有這些事情。如果Ii不能使用C#,請告訴客戶填充它。 – ProfK 2014-10-04 16:47:33

+0

檢查'.xaml'和'.xaml.vb'文件是否通過關閉VS並在某些文本編輯器中打開'.vbproj'文件來相互連接,然後搜索'Dashboard.xaml.vb':它應該包含一個類似於' Dashboard.xaml '的條目,因爲這告訴VS這兩個文件「屬於一起」 – andreask 2014-10-04 17:02:55

+0

它們是這樣連接的。 – ProfK 2014-10-04 18:20:17