c# - inheritance WPF layout - Window from Window

后端 未结 1 1084
长发绾君心
长发绾君心 2021-01-26 05:39

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

I think, my layout (MediaLibrary.xaml) have to inherit of MainWindow... But I

相关标签:
1条回答
  • 2021-01-26 06:09

    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.

    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...

    From

    <Window x:Class="blah...
    

    to

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

    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.

    In the .cs code, change

    public partial class blah : Window
    

    to

    public partial class blah : MyWindow
    

    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
    
    0 讨论(0)
提交回复
热议问题