为两个WPF Windows创建一个抽象

时间:2017-09-21 19:08:40

标签: c# wpf abstract

我正在尝试减少下面的代码,以便我可以从任一窗口(KPIViewVAS或KPIView)调用'KeyHandler_F',并使用'ToggleWindow'执行对窗口状态的更改。下面的代码有效,但不是DRY:

namespace KPI_Monitor_WPF.UI
{

class FullScreen
{
    public abstract class A<KPIViewVAS>
    {
        public abstract void KeyHandler_F(object sender, KeyEventArgs e, KPIViewVAS KVV);
        protected abstract void ToggleWindow(KPIViewVAS KVV);
    }

    public class B : A<KPIViewVAS>
    {
        public override void KeyHandler_F(object sender, KeyEventArgs e, KPIViewVAS KVV)
        {
            if (e.Key == Key.F)
            {
                ToggleWindow(KVV);
            }
        }
        protected override void ToggleWindow(KPIViewVAS KVV)
        {
            switch (KVV.WindowState)
            {
                case (WindowState.Maximized):
                    {
                        KVV.WindowState = WindowState.Normal;
                        KVV.WindowStyle = WindowStyle.ToolWindow;
                    }
                    break;

                default:
                    {
                        KVV.WindowState = WindowState.Maximized;
                        KVV.WindowStyle = WindowStyle.None;
                    }
                    break;
            }
        }
    }

    public class C : A<KPIView>
    {
        public override void KeyHandler_F(object sender, KeyEventArgs e, KPIView KVV)
        {
            if (e.Key == Key.F)
            {
                ToggleWindow(KVV);
            }
        }
        protected override void ToggleWindow(KPIView KVV)
        {
            switch (KVV.WindowState)
            {
                case (WindowState.Maximized):
                    {
                        KVV.WindowState = WindowState.Normal;
                        KVV.WindowStyle = WindowStyle.ToolWindow;
                    }
                    break;

                default:
                    {
                        KVV.WindowState = WindowState.Maximized;
                        KVV.WindowStyle = WindowStyle.None;
                    }
                    break;
            }
        }
    }

}
}

我开始使用下面的示例减少代码,但是当我在抽象类下移动'ToggleWindow'的命令时,我得到了错误,指出'KVV.WindowState'无法找到:

class FullScreen
{
    public abstract class A<KPIViewVAS>
    {
        public virtual void KeyHandler_F(object sender, KeyEventArgs e, KPIViewVAS KVV) {
            if (e.Key == Key.F)
            {
                ToggleWindow(KVV);
            }
        }
        public virtual void ToggleWindow(KPIViewVAS KVV)
        {
            switch (KVV.WindowState)
            {
                case (WindowState.Maximized):
                    {
                        KVV.WindowState = WindowState.Normal;
                        KVV.WindowStyle = WindowStyle.ToolWindow;
                    }
                    break;

                default:
                    {
                        KVV.WindowState = WindowState.Maximized;
                        KVV.WindowStyle = WindowStyle.None;
                    }
                    break;
            }
        }
    }
    }

    public class B : A<KPIViewVAS>
    {
        public override void KeyHandler_F(object sender, KeyEventArgs e, KPIViewVAS KVV)
        {

        }
        public override void ToggleWindow(KPIViewVAS KVV)
        {
        }
    }

    public class C : A<KPIView>
    {
        public override void KeyHandler_F(object sender, KeyEventArgs e, KPIView KVV)
        {
        }
        protected override void ToggleWindow(KPIView KVV)
        {
        }
    }

}

Windows“KPIViewVAS”和“KPIView”位于

之下
    public partial class KPIViewVAS : Window
{

    public KPIViewVAS()
    {
        InitializeComponent();
    }

    private void KeyHandler_F(object sender, KeyEventArgs e)
    {
        FullScreen.B switchScreen = new FullScreen.B();
        switchScreen.KeyHandler_F(sender, e, this);
    }
}

 public partial class KPIView : Window
{

    public KPIView()
    {
        InitializeComponent();
    }

    private void KeyHandler_F(object sender, KeyEventArgs e)
    {
        FullScreen.A switchScreen = new FullScreen.A();
        switchScreen.KeyHandler_F(sender, e, this);
    }
}

1 个答案:

答案 0 :(得分:0)

使用Peter Duniho建议使用动态代码减少没有任何错误。任何窗口都可以通过

使用代码
    private void KeyHandler_F(object sender, KeyEventArgs e)
    {
        fullScreen.adjustScreen switchScreen = new fullScreen.adjustScreen();
        switchScreen.KeyHandler_F(sender, e, this);
    }

并且课程被简化为:

public class fullScreen
{
public class adjustScreen
    {
        public void KeyHandler_F(object sender, KeyEventArgs e, dynamic KVV)
        {
            if (e.Key == Key.F)
            {
                ToggleWindow(KVV);
            }
        }
        private void ToggleWindow(dynamic KVV)
        {
            switch (KVV.WindowState)
            {
                case (WindowState.Maximized):
                    {
                        KVV.WindowState = WindowState.Normal;
                        KVV.WindowStyle = WindowStyle.ToolWindow;
                    }
                    break;

                default:
                    {
                        KVV.WindowState = WindowState.Maximized;
                        KVV.WindowStyle = WindowStyle.None;
                    }
                    break;
            }
        }
    }
}