我想在一个名为MyWindow的ControlLibrary中创建一个Window,以便我可以为所有新应用程序定义标准外观(例如我想在MyWindow中放置一个Icon)和一些基本逻辑,以便我可以继承其他Wpf-Applications中的窗口。
但如果我尝试在另一个应用程序中继承MyWindow,我会收到错误...
有人能给我一个如何解决这个问题的提示吗?
我的基本窗口如下所示:
<Window x:Class="BaseWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Icon="GeoAS.ico"
Title="BaseWindow" Height="300" Width="300"
Background="black">
<Grid>
</Grid>
</Window>
这在命名空间MyControllLib中。 我已将此Lib添加为我的项目的参考,我想将此窗口用作我的主窗口。
我希望在新窗口中添加这样的代码是我的Codebehind:
Class Window1
Inherits MyControlLib.BaseWindow
End Class
但编译器发出错误(我必须翻译,因为错误是德语): Window1-Class的Baseclass与myControlLib.BaseWindow-Baseclass没有什么不同。
我不知道该做什么
答案 0 :(得分:0)
如果你想继承一个Class(在你的情况下是MyWindow),你需要用纯代码声明它,不允许使用xaml,因为xaml不能被继承。
您可以将xaml的根标记设置为您定义的类。 在这里,您必须从Window派生您的基类,然后您可以从基类派生您的Windows类。抱歉,我不知道vb.net,所以这段代码在C#中
public class MyWindow : Window
{
.....//your basic logic here
}
你可以从这样的类派生你的页面类:
public partial class MyDeriveClass : MyWindow
{
.............
}
和你的xaml写
<y:MyWindow y="add your Namesapace here" and add other attribute of Page also like default namespace and xmlns:x>
...............
</y:MyWindow>
希望这会有所帮助!!
答案 1 :(得分:0)
您的BaseWindow类应该是纯代码。像这样:
public class BaseWindow : Window
{
public BaseWindow()
{
// Code modified here
Uri iconUri = new Uri("pack://application:,,,/MyControlLib;component/my.ico", UriKind.RelativeOrAbsolute);
Icon = BitmapFrame.Create(iconUri);
Title = "BaseWindow";
Height = 300;
Width = 300;
Background = Brushes.Black;
}
}
并且您的派生类可以是纯代码,也可以是xaml和代码。在第一种情况下,您需要这样做:
public partial class Window1 : BaseWindow
{
public Window1()
{
InitializeComponent();
}
}
但在第二种情况下,您还需要驾驶xaml,如下所示:
<local:BaseWindow x:Class="ErrorTemplatePOC.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyControlLib;assembly=MyControlLib">
</local:BaseWindow>