2017-09-21 126 views
-2

我想減少下面的代碼,以便我可以從任一窗口(KPIViewVAS或KPIView)調用'KeyHandler_F'並使用'ToggleWindow'執行對窗口狀態的更改。以下作品的代碼,但不是幹:爲兩個WPF窗口創建抽象

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) 
     { 
     } 
    } 

} 

窗戶「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); 
    } 
} 
+0

的基本問題是,編譯器不會有關於'KPIViewVAS'的任何信息。在你的第二個例子中,這不是一種類型。這是一個_parameter_類型,可以使用_any_類型的通用類型「A 」替換爲代碼。不幸的是,你沒有打算提供一個好的[mcve]或足夠的其他上下文來理解你使用的類型是什麼樣的,或者一個泛型類可能的樣子。它可能有一個基本類型,你可以在約束中使用它來提供編譯器需要的信息,或者你需要使用'dynamic',或者你可能只是不需要使它成爲通用的。 –

回答

0

使用使用動態代碼而沒有任何錯誤減少的彼得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; 
      } 
     } 
    } 
}