İçinde çok fazla düğme bulunan bir form var.WPF, t # c işlevinde bir düğmeden geçiriliyor
Her düğme neredeyse aynı şeyi yapar.
- çek düğmesi
- durumunu görmek için bir bool durumunu temel alan komutu gönderir.
- , durumu düzeltmek için durumu
- resets bool yansıtması için düğmenin arka planını değiştirir.
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;
}
}
Tüm bu düzgün çalıştığından var. Bir kaç kez kopyalayıp yapıştırmak yerine, button_DSK1_LowerThird_Click öğesinin içindeki tüm kodu taşımak istiyorum. Daha sonra bu işlevi çağırabilir ve tüm kodu 25 kez kopyalayıp yapıştırabilirim. Bunu yapabilirim. fonksiyonda nasıl yapılacağını anlayamıyorum. bu satır button_DSK1_LowerThird.Background = brush;
button_DSK1_LowerThird'in yerini alacak işleve geçmek için neye ihtiyacım var? ben en azından beynimde, düşünce işlevini çağırdığınızda
i sonra benim işlev kontrolünden ve bu düğme için gerçekleşmesi gereken her şeyi yapabileceğini böylece
ButtonState(string command, Button button, bool state);
gibi diyebiliriz. Bu, sahip olduğum işlevim.
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;
}
}
i, ben sadece onları henüz öğrenmemiş bunu yapmak için daha iyi yollar var biliyorum kodlama için yeni duyuyorum.
Sadece kullandığınız bir ToggleButton https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton(v=vs.110).aspx –
taşma yığını hoş geldiniz, yardım-> tur atın lütfen, sorunuz vauge, size yardımcı olması için hiçbir kod göstermiyor. Turu aldıktan sonra, kodunuzu gösterin ve neden yardımımıza ihtiyaç duyduğunuzu açıklayın. – BugFinder