gpt4 book ai didi

c# - 为许多控件调用委托(delegate)

转载 作者:行者123 更新时间:2023-11-30 22:49:57 26 4
gpt4 key购买 nike

C# 2008 SP1

下面的函数将从另一个线程调用。因此必须调用控件本身,以便创建它们的正确线程可以更改属性。

但是,因为我有很多控件需要更新。我真的不想为每个人都写下所有这些委托(delegate)。我在下面做了一个。但是,我认为这是很多代码。有没有办法缩短这个?

非常感谢,

public void SetIdleState(string callStatusMsg)
{
this.btnCallAnswer.Text = CATWinSIP_MsgStrings.Call;
this.btnEndCallReject.Text = CATWinSIP_MsgStrings.EndCall;
this.btnHoldUnhold.Text = CATWinSIP_MsgStrings.Hold;

this.btnCallAnswer.Enabled = true;
this.btnRedial.Enabled = true;
this.btnEndCallReject.Enabled = false;
this.btnHoldUnhold.Enabled = false;

if (this.statusDisplay1.InvokeRequired)
{
statusDisplay1.Invoke(new UpdateCallStatusDelegate(this.UpdateCallStatus), callStatusMsg);
}
else
{
this.statusDisplay1.CallStatus = callStatusMsg;
}
}

// Delegate for marshalling the call on the correct thread.
private delegate void UpdateCallStatusDelegate(string callStatusMsg);
private void UpdateCallStatus(string callStatusMsg)
{
this.statusDisplay1.CallStatus = callStatusMsg;
}

最佳答案

怎么样:

Dispatcher.BeginInvoke(new DispatcherOperationCallback((param) =>

{

this.statusDisplay1.CallStatus = callStatusMsg;

return null;

}), DispatcherPriority.Background, new object[] { null });

}

关于c# - 为许多控件调用委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/855985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com