我知道還有其他問題發佈,看起來是同一個問題,但它們都不能解決我的問題。「Kproj.Forms.frmSwitch」不能是XAML文件的根源,因爲它是使用XAML定義的
我是新來的WPF我試圖將程序從WinForms轉換爲WPF。我有一個主窗口「Kproj.Forms.frmLogin」,它繼承了繼承System.Windows.Window類的基類「Kproj.Forms.frmSwitch」。 WhenI了上述問題,我最初的XAML代碼是:
<Control:frmSwitch x:Class="Kproj.Forms.frmLogin"
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:Control="clr-namespace:Kproj.Forms"
mc:Ignorable="d"
Title="LOG IN" Height="309" Width="678">
<Grid Height="271" Width="662">
... Content
</Grid>
</Control:frmSwitch>
這些在代碼的屁股:
namespace Kproj.Forms
{
public partial class frmLogin : frmSwitch
{
}
}
namespace Kproj.Forms
{
public partial class frmSwitch : Window
{
}
}
經過進一步的研究,我發現,我需要做出frmSwitch到基沒有XAML的類。
namespace Kproj.Forms
{
public class frmSwitch2 : Window
{
}
}
我則相應調整主窗口XAML:
<Control:frmSwitch2 x:Class="Kproj.Forms.frmLogin"
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:Control="clr-namespace:Kproj.Forms"
mc:Ignorable="d"
Title="LOG IN" Height="309" Width="678">
<Grid Height="271" Width="662">
... Content
</Grid>
</Control:frmSwitch2>
,並在主窗口的代碼背後:
因此,我只是一般類的形式,看起來像這樣創建frmSwitch2現在namespace Kproj.Forms
{
public partial class frmLogin : frmSwitch
{
}
}
,我失去了原有的錯誤消息,但我得到了,指出
消息0即使它在主窗口XAML中鍵入「Control:」時顯示「frmSwitch2」,因此它知道它存在於名稱空間中。有什麼建議麼?
免責聲明:我試圖研究它作爲最好的,可能的,但所有我StackOverflow上發現帖子是這個問題,但都被從XAML \ CS分部類轉換爲獨奏代碼隱藏全XAML少類固定。
在WPF中,主窗口通常只是一個shell,它只是包含其他用戶控制的網格區域。首先重新安排您的主窗口,以包含網格行和列,因爲您希望佈置事物。然後創建其他控件,讓它們彼此分開,例如UCTitle for titles,UCNav for navigation,UCStatus for status bar,UCMain for main region in Main window。然後使用遏制而不是繼承..如果你真的需要繼承,那麼你真的需要研究MVVM –