2016-03-18 16 views
-2

我有一個窗體中有很多按鈕。WPF在c#函數中傳遞一個按鈕

每個按鈕的功能幾乎相同。

  1. 檢查一個bool看到按鈕
  2. 的狀態發送基於所述狀態的命令。
  3. 更改按鈕的背景以反映狀態
  4. 重置bool以更正狀態。
private void button_DSK1_LowerThird_Click(object sender, RoutedEventArgs e) 
    { 

     if (button_DSK1_LowerThird_Click== false) 

     { 

      string newmessage = server.TCPmessage(Vars.ComandSDK1LowerThird); 
      string dataReturn = server.Connect(Vars.IPAdress, 5250, newmessage); 
      textBlock_output.Text = dataReturn.ToString(); 

      if (dataReturn.Contains("202")) 
      { 

       button_DSK1_LowerThird_State = true; 
       Uri resourceUri = new Uri("white-glossy-lit.png", UriKind.Relative); 
       StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri); 

       BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream); 
       var brush = new ImageBrush(); 
       brush.ImageSource = temp; 

       button_DSK1_LowerThird.Background = brush; 


      } 
      else 
      { 

       button_DSK1_LowerThird_State = false; 

       Uri resourceUri = new Uri("white-glossy-rectangle-button-md.png", UriKind.Relative); 
       StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri); 

       BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream); 
       var brush = new ImageBrush(); 
       brush.ImageSource = temp; 

       button_DSK1_LowerThird.Background = brush; 
      } 
     } 
     else 
     { 
      string newmessage = server.TCPmessage("CG 1-20 STOP 1"); 
      string dataReturn = server.Connect(Vars.IPAdress, 5250, newmessage); 
      textBlock_output.Text = dataReturn.ToString(); 

      if (dataReturn.Contains("202")) 
      { 

       button_DSK1_LowerThird_State = false; 
       Uri resourceUri = new Uri("white-glossy-rectangle-button-md.png", UriKind.Relative); 
       StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri); 

       BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream); 
       var brush = new ImageBrush(); 
       brush.ImageSource = temp; 

       button_DSK1_LowerThird.Background = brush; 


      } 
      else 
      { 

       button_DSK1_LowerThird_State = true; 

       Uri resourceUri = new Uri("white-glossy-lit.png", UriKind.Relative); 
       StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri); 

       BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream); 
       var brush = new ImageBrush(); 
       brush.ImageSource = temp; 

       button_DSK1_LowerThird.Background = brush; 
      } 
     } 

我有這一切工作正常。我想將button_DSK1_LowerThird_Click中的所有代碼移動到一個函數中,而不是複製和粘貼它。 然後我可以調用該函數,而不是複製並粘貼所有代碼25次。 我可以做到這一點。我無法弄清楚該如何做的功能。 這一行button_DSK1_LowerThird.Background = brush;

我需要傳遞什麼函數來替換button_DSK1_LowerThird?

當我打電話我認爲,至少在我的大腦的功能,我可以把它像這樣

ButtonState(string command, Button button, bool state); 

然後我可以有我的功能檢查,並做需要發生該按鈕的每一件事情。 這是我的功能,因爲我擁有它。

public void ButtonState(string command, Button button, bool state) 
    { 
     CasparCG server = new CasparCG(); 

     if (state == false) 

     { 

      string newmessage = server.TCPmessage(command); 
      string dataReturn = server.Connect(Vars.IPAdress, 5250, newmessage); 

      if (dataReturn.Contains("202")) 
      { 

       state = true; 
       Uri resourceUri = new Uri("white-glossy-lit.png", UriKind.Relative); 
       StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri); 

       BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream); 
       var brush = new ImageBrush(); 
       brush.ImageSource = temp; 

       button.Background = brush; 


      } 
      else 
      { 

       state = false; 

       Uri resourceUri = new Uri("white-glossy-rectangle-button-md.png", UriKind.Relative); 
       StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri); 

       BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream); 
       var brush = new ImageBrush(); 
       brush.ImageSource = temp; 

       button.Background = brush; 
      } 
     } 
     else 
     { 
      string newmessage = server.TCPmessage("CG 1-20 STOP 1"); 
      string dataReturn = server.Connect(Vars.IPAdress, 5250, newmessage); 


      if (dataReturn.Contains("202")) 
      { 

       state = false; 
       Uri resourceUri = new Uri("white-glossy-rectangle-button-md.png", UriKind.Relative); 
       StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri); 

       BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream); 
       var brush = new ImageBrush(); 
       brush.ImageSource = temp; 

       button.Background = brush; 


      } 
      else 
      { 

       state = true; 

       Uri resourceUri = new Uri("white-glossy-lit.png", UriKind.Relative); 
       StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri); 

       BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream); 
       var brush = new ImageBrush(); 
       brush.ImageSource = temp; 

       button.Background = brush; 
      } 
     } 

我更新的編碼我知道有更好的方法來做到這一點,我只是還沒有學到它們。

+0

只需使用一個切換按鈕https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton(v=vs.110).aspx –

+1

歡迎堆棧溢出,請採取幫助 - >遊覽,您的問題是vauge,顯示沒有代碼可以幫助我們。參觀完之後,向我們展示您的代碼,並解釋爲什麼您需要我們的幫助 – BugFinder

回答

1

對於所有按鈕和類型轉換髮件人單擊按鈕並按鈕,您可以執行您的功能。

private void button_Click(object sender, RoutedEventArgs e) 
     { 
      if (sender is Button) 
      { 
       var btn = sender as Button; 
       // Here you can carry out your functionality 
      }   
     } 
相關問題