c# - 继承WPF布局 - 来自Window的窗口 [英] c# - inheritance WPF layout - Window from Window

查看:276
本文介绍了c# - 继承WPF布局 - 来自Window的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的继承'Window'有问题,我不明白问题是什么?

I have problem with my inheritance of 'Window', I don't understand what the problem is?

我认为,我的布局(MediaLibrary.xaml)有继承MainWindow ...但我不知道怎么做:/

I think, my layout (MediaLibrary.xaml) have to inherit of MainWindow... But I don't know how do that :/

有2个类:

MainWindow.xaml

<Window x:Class="WindowsMediaPlayerV2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MyWindowsMediaPlayer v2" Height="350" Width="525" MinHeight="350" MinWidth="525">
    <Grid>
    </Grid>
</Window>

MainWindow.xaml.cs

namespace WindowsMediaPlayerV2
{
    public partial class MainWindow : Window
    {

        public MediaLibrary myMediaLibrary = new MediaLibrary();

        public MainWindow()
        {
            InitializeComponent();
        }
     }
}

MediaLibrary.xaml

<Window x:Class="WindowsMediaPlayerV2.MediaLibrary"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MediaLibrary" Height="350" Width="300" MinHeight="350" MinWidth="300" Closing="Window_Closing">
    <Grid>
    </Grid>
</Window>

MediaLibrary.xaml.cs

namespace WindowsMediaPlayerV2
{
    public partial class MediaLibrary : MainWindow //problem here when I run
    {
        public MediaLibrary()
        {
            InitializeComponent();

        }
     }
}

错误时我跑:

FR:Lesdéclarationspartiellesde'WindowsMediaPlayerV2.MediaLibrary'ne doiventpasspécifierdesclasses debasedifférentes

FR: Les déclarations partielles de 'WindowsMediaPlayerV2.MediaLibrary' ne doivent pas spécifier des classes de base différentes

EN:'WindowsMediaPlayerV2.MediaLibrary'的部分声明不得指定不同的基类
我们可以帮助我吗?谢谢

EN: Partial declarations of 'WindowsMediaPlayerV2.MediaLibrary' must not specify different base classes Can we help my ? Thanks

推荐答案

您不能将可视化的.XAML类子类化。只能继承C#代码中构建的类。

You can not subclass a visual .XAML class such as you are attempting. Only classes that are built in your C# code can be inherited.

现在,您可以根据样式,颜色创建自己的窗口视觉主题,等(或在代码中执行),然后从代码构建您的类。

Now, that said, you can create your own visual theme of a window with respect to style, colors, etc (or do in code), then build your classes upon that class from code.

public class MyWindow : Window
{
   public class MyWindow()
   {
      SomeProperty = SomeValue;
   }

   protected void SomeCustomFunction(int AnyParameter)
   {
      SomethingCommon = AnyParameter;
   }

   etc...
}

构建你的项目,这样这个类就可以用于派生目的。

Build your project so this class is then known/available for derivation purposes.

现在,当你创建基于.XAML的Window时,让它创建基于的默认值窗口。然后,修改.XAML.cs和.cs版本,并将对MyWindow类的引用更改为......

Now, when you create your .XAML-based Window, let it create the default based on "Window". Then, modify both your .XAML.cs and your .cs versions and change the reference to YOUR "MyWindow" class something like...

来自

<Window x:Class="blah...

<myLib:MyWindow x:Class="blah
   xmlns:myLib="clr-namespace:MyWpfClassLibrary"

在XAML中,您还需要添加引用您的类库命名空间,例如...如果您的窗口类在另一个项目/命名空间中,则为xmlns。 myLib就像是该类库的别名,因此它可以在XAML的其余部分中使用,它知道如何/在何处解析类引用。

in the XAML, you will also need to add reference to your class library namespace, something like... the xmlns if your window class is in another project/namespace. The "myLib" is like an "alias" to that class library so it can be used within the rest of the XAML, it knows how/where to resolve the class references.

在.cs代码中,更改

public partial class blah : Window

public partial class blah : MyWindow

如果您的类库位于同一名称空间中,那么您应该很高兴。如果没有,您可以使用MyLibrary添加

If your class library is in the same namespace, you should be good to go. If not, you can either add a

using MyLibrary;   before the public partial class -- OR

public partial class blah : MyLibrary.MyWindow

这篇关于c# - 继承WPF布局 - 来自Window的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆