是否有更好的方法来继承WPF中的窗口 [英] Is there a better way to inherit window in WPF

查看:79
本文介绍了是否有更好的方法来继承WPF中的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在winform中,我们可以轻松继承.但是在WPF中,我们不能继承包含XAML的类.因此,每当需要概括某些窗口的属性时,我都会创建一个没有XAML的基类.例如,我要使所有窗口都在中心屏幕上启动.我在基类中使用了后面的代码(此类不包含XAML)

In winform, we can inherit easily. But in WPF, we can't inherit class which contains XAML. So whenever I need to generalize some window's attribute, I create a base class without XAML. For example, I want to make all windows start up at center screen. I use code behind in the base class (this class doesn't contain XAML)

namespace VBDAdvertisement
{
    public class BaseWindow:Window
    {
        public BaseWindow()
        {
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }
    }
}

这只是一个简单的任务.我认为,对于更复杂的任务,逐行使用代码不是一个好主意.所以我想知道是否有更好的方法可以在WPF中继承窗口(更接近Winform继承)?

That is just for a simple task. In my opinion, for more complex task, using code behind line by line is not a good idea. So I wonder if there is a better way to inherit window in WPF (something closer to winform inheritance) ?

推荐答案

This post explains what you already know as far as xaml not supporting visual inheritance. The solution they have provided is creating a UserControl that provides your common properties.

这篇关于是否有更好的方法来继承WPF中的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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