在 WPF 中为窗口创建抽象基类 [英] Creating an abstract base class for windows in WPF

查看:23
本文介绍了在 WPF 中为窗口创建抽象基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常的 WPF 架构:

Usual WPF architecture:

public partial class MainWindow: Window {

... InitializeComponent()

}

XAML: <Window x:Class="MainWindow"> </Window>

我想转移到什么地方:

public abstract class BaseWindow: Window {

    public System.Windows.Controls.TextBlock control1;
    public System.Windows.Shapes.Rectangle   control2;
    public System.Windows.Controls.TextBox   control3;

}

public partial class AWindowImplementation {

... InitializeComponent()

}

public partial class AnotherWindowImplementation{

... InitializeComponent() 

}

 XAML:
 <BaseWindow x:Class="AWindowImplementation"> </BaseWindow>
 <BaseWindow x:Class="AnotherWindowImplementation"> </BaseWindow>

以上是伪代码.这个新架构编译后,警告说实现隐藏了控制定义(因为我应该放置覆盖"关键字的地方与自动生成的 InitializeComponent 一起).不幸的是,控制字段没有被填充.

The above is pseudo-code. This new architecture compiles, with warnings that the implementations hide the control defintions (because the place where I should put the 'override' keywords are withing the auto-generated InitializeComponent). Unfortunately the control fields don't get populated.

这能实现吗?我想要做的是创建多个具有相同界面/控件的 UI 设计,以便其余代码可以与任一设计交互.

Is this achievable? What I am trying to do is create several UI designs with the same interface/controls so that the rest of the code can interact with either design.

感谢 pchajer 和 Yevgeniy,我现在有了以下可行的解决方案,但我仍然收到覆盖警告:

 public class MainWindowBase : Window
 {
     public TextBlock control1;
     public Rectangle control2;
     public TextBox   control3;

    static MainWindowBase()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MainWindowBase),
                   new FrameworkPropertyMetadata(typeof(MainWindowBase)));
    }

    public override void OnApplyTemplate()
    {
        control1 = (TextBlock) FindName("control1");
        control2 = (Rectangle) FindName("control2");
        control3 = (TextBox)   FindName("control3");
    }

 }   

 <Style TargetType="{x:Type views:MainWindowBase}" 
           BasedOn="{StaticResource {x:Type Window}}">
     <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type views:MainWindowBase}">
                    <ContentPresenter />                                 
            </ControlTemplate>
        </Setter.Value>
     </Setter>
     </Style>

 public partial class AWindowImplementation :MainWindowBase {

 ... InitializeComponent()

}

 <MainWindowBase x:Class="AWindowImplementation"> </MainWindowBase>

我想我将不得不在基类中使用不同的字段名称来消除警告,或者可能删除派生类中的 InitializeComponent.但无论如何它现在有效.

I guess I will have to use different field names in the base class to get rid of the warnings, or perhaps remove InitializeComponent in the derived class. But anyway it works now.

推荐答案

在 WPF 中,您可以创建一个从 Window 继承并具有 XAML 的基类.但有一个工作场所

In WPF you can create a Base class which inherits from Window and that has a XAML. But there is a workground

请参考此链接 - 如何创建通用的 WPF 基础窗口样式?

这篇关于在 WPF 中为窗口创建抽象基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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