2 回答
![?](http://img1.sycdn.imooc.com/54586431000103bb02200220-100-100.jpg)
TA贡献1830条经验 获得超3个赞
问题很简单,当ActualAction值更改时,绑定命令的 CanExecute 状态不会重新评估。
要求CommandManager.InvalidateRequerySuggested()强制重新评估。
private void StartedAction()
{
_actualAction = ActualAction.DoingAction;
CommandManager.InvalidateRequerySuggested();
}
![?](http://img1.sycdn.imooc.com/54584d080001566902200220-100-100.jpg)
TA贡献1833条经验 获得超4个赞
您正在 UI 线程上进行后台工作。不要在那里做,在另一个线程中做,并使用轮询、事件或其他回调方法来更新 UI(在 UI 线程上)。
例如,您可以这样做:
Task.Run(() => { OtherDll.DoWork(); };
这将启动外螺纹上的其他工作。
如果您需要更多控制,您可以将其他 dll 的功能单独包装在一个线程中。
Public Class OtherDLLThread
{
Thread _internalThread;
public OtherDLLThread()
{
_internalThread = new Thread(ThreadMainLoop);
}
public void ThreadMainLoop()
{
OtherDLL.DoWork();
}
public static void Start()
{
_internalThread.Start();
}
}
像这样使用它:
OtherDLLThread other = new OtherDLLThread();
other.Start();
这是另一个将代码插入 UI 线程的函数:
/// <summary>
/// Runs the action on UI thread.
/// </summary>
/// <param name="action">The action.</param>
public static void RunOnUIThread(Action action)
{
try
{
if (Application.Current != null)
Application.Current.Dispatcher.Invoke(action);
}
catch (Exception ee)
{
_logger.Fatal("UI Thread Code Crashed. Action detail: " + action.Method, ee);
//SystemManager.Instance.SendErrorEmailToCsaTeam("Kiosk Application Crashed", "UI Thread Code Crashed. Action detail: " + action.Method);
throw;
}
}
像这样使用它:
RunOnUITHread(() => lblStatus.Text = "Working...");
- 2 回答
- 0 关注
- 252 浏览
添加回答
举报