2 回答

TA貢獻1830條經驗 獲得超3個贊
問題很簡單,當ActualAction值更改時,綁定命令的 CanExecute 狀態不會重新評估。
要求CommandManager.InvalidateRequerySuggested()強制重新評估。
private void StartedAction()
{
_actualAction = ActualAction.DoingAction;
CommandManager.InvalidateRequerySuggested();
}

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 關注
- 288 瀏覽
添加回答
舉報