我有一個讓我發瘋的問題。當我嘗試在 Visual Studio 中構建我的 VB.NET WPF 專案時,出現錯誤“未定義型別‘MyProject.MyUC’”。在我對該解決方案中的其他檔案進行了一些更改(與該 UC 完全無關,它是后端代碼或使用該 UC 的視窗。完全獨立的代碼)后,出現此錯誤。我正在使用 GIT,并將舊分支與新分支進行比較,所以我知道這是一個事實。
使用控制元件的視窗定義如下:
<Window x:Class="MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:uc="clr-namespace:MyProject"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
Title="MyWindow" Height="350" Width="650">
<Grid>
<uc:MyUC x:Name="MyUC"></uc:MyUC>
</Grid>
</Window>
UC 定義如下:
<UserControl x:Class="MyUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:FLOATSOFT"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="600">
<Grid>
</Grid>
</UserControl>
它支持 VB 代碼:
Public Class MyUC
End Class
(沒有特定的命名空間宣告)
我已經洗掉了所有功能代碼來顯示結構。但即使我真的將我的代碼縮減為我參考的內容,它也不會編譯。
我不明白。MyUC肯定在MyProject命名空間中。如果我將滑鼠懸停在 VB 代碼中的類名上,工具提示會顯示:“Class MyProject.MyUC”,確認它肯定在正確的命名空間中。
為什么會發生這種情況?我仍然擁有我在另一個 GIT 分支中參考的所有內容,即使所有這些檔案都 100% 匹配,但其他分支仍會編譯,而這個分支卻給出了可怕的“未定義型別‘MyProject.MyUC’”錯誤。
我努力了:
重新啟動 Visual Studio - 這用于幫助解決此類錯誤;
清潔和重建;
重新創建所有這些檔案(MyWindow.XAML、MyWindow.XAML.VB、MyUC.XAML 和 MyUC.XAML.VB)并將代碼復制到這些新檔案中;
如果我注釋掉這個 UC 的用法并且沒有其他編譯錯誤,請確保我的代碼構建。
在 MyUC.XAML 中明確說明命名空間:
<UserControl x:Class="MyProject.MyUC"
在 MyUC.XAML.VB 中:
Namespace MyProject
Public Class MyUC
End Class
End Namespace
也沒有幫助。同樣的錯誤。
- 創建一個新專案并復制這 4 個檔案。它編譯。但是每次發生這種情況時都必須重新創建專案是荒謬的。就像我說的,它經常發生,但重新啟動 Visual Studio 曾經有幫助。這次不行。
And I am at complete loss. I have reviewed dozens of similar posts to this one, but haven't found the answer. One answer suggested that this might be due to different .NET versions of the project and referenced assemblies, but I don't have any of that.
One branch compiles, another one throws this error. Both have identical code in every file that could possibly be related to this UC. How do I find the cause?
EDIT: I forgot to mention that I also tried creating a new UC from scratch in this project branch, and if I try to use it like this MyUC, this project branch won't compile. But the other branch still compiles.
EDIT: VERY IMPORTANT: I just discovered that if I change
<Grid>
<uc:MyUC x:Name="MyUC"></uc:MyUC>
</Grid>
To
<Grid>
<uc:MyUC></uc:MyUC>
</Grid>
(省略 x:Name="MyUC"),然后代碼突然編譯,我沒有收到任何錯誤。如果我為這個 UC 設定一個名稱——任何名稱——它都不會編譯。我完全不知道發生了什么。
uj5u.com熱心網友回復:
我終于弄明白了。事實證明,這個問題與這個相同。簡而言之,我是對的 - 這 4 個檔案中沒有任何更改,它們應該已經編譯。但是,在一個完全獨立的檔案中,我錯誤地為其中一個模塊定義了命名空間。我應該寫:
Namespace MyModule
Public Module MyModule
End Class
End Namespace
但相反,我寫道:
Namespace MyProject.MyModule
Public Module MyModule
End Class
End Namespace
雖然這MyModule與MyUCor完全無關,但MyWindow顯然它創建了一個 namespace MyProject.MyProject,這就是為什么 MyWindow 不會編譯,因為它可能試圖搜索MyUCin MyProject.MyProject。這很奇怪,因為我得到的錯誤沒有暗示這一點。事實上,沒有任何地方暗示我有這種命名空間。就像我在問題中寫的那樣,在我除錯的任何地方,我發現MyUChas 是直接在MyProject.MyUC. 每個其他視窗都有相同的宣告,但它們編譯。
我必須承認 VS 編譯器拋出的錯誤非常不直觀,它在命名空間上提供的資訊對于 UC 來說是完全錯誤的,我只是偶然發現了解決方案。甚至物件查看器也顯示命名空間沒有問題(它沒有顯示我有MyProject.MyProject)。
如果有人遇到同樣的問題,希望這會有所幫助。我也很想聽聽關于如何使這些編譯器錯誤更直接地解決實際問題的任何建議,因為在這種情況下它完全是錯誤的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/402158.html
上一篇:我應該系結到我的控制元件的屬性,還是應該在OnPropertyChanged中更新它們?
下一篇:Xamarin--使用Syncfusion的SfPopupLayout時,在視圖模型中設定值后,單選按鈕視覺狀態不會更新
