From 37a3ec77ec3324e54f8cfc86c08ec963f8eee922 Mon Sep 17 00:00:00 2001 From: David Hancock Date: Wed, 20 Sep 2023 20:01:42 +0100 Subject: [PATCH] Add work around for WinAppSdk 1.4.1 issue More of a bodge than a work around... --- SudokuSolver/ViewModels/PuzzleViewModel.cs | 35 +++++++++++++--------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/SudokuSolver/ViewModels/PuzzleViewModel.cs b/SudokuSolver/ViewModels/PuzzleViewModel.cs index 85751b4..acaec0c 100644 --- a/SudokuSolver/ViewModels/PuzzleViewModel.cs +++ b/SudokuSolver/ViewModels/PuzzleViewModel.cs @@ -92,7 +92,7 @@ public void UpdateCellForKeyDown(int index, int newValue) UpdateView(); undoHelper.Push(model); IsModified = true; - RaiseCanExecuteChanged(); + UpdateMenuItemsDisabledState(); } else { @@ -124,7 +124,7 @@ public void Open(Stream stream) { UpdateView(); undoHelper.Push(model); - RaiseCanExecuteChanged(); + UpdateMenuItemsDisabledState(); } } @@ -148,7 +148,7 @@ public void New() UpdateView(); undoHelper.Push(model); IsModified = false; - RaiseCanExecuteChanged(); + UpdateMenuItemsDisabledState(); } public bool ShowPossibles @@ -224,7 +224,7 @@ public void ExecuteUndo(object? param) model = undoHelper.PopUndo(); UpdateView(); IsModified = true; - RaiseCanExecuteChanged(); + UpdateMenuItemsDisabledState(); } } @@ -238,7 +238,7 @@ public void ExecuteRedo(object? param) model = undoHelper.PopRedo(); UpdateView(); IsModified = true; - RaiseCanExecuteChanged(); + UpdateMenuItemsDisabledState(); } } @@ -249,18 +249,25 @@ public void Puzzle_SelectedIndexChanged(object sender, Views.Cell.SelectionChang else if (selectedIndex == e.Index) selectedIndex = -1; - RaiseCanExecuteChanged(); + UpdateMenuItemsDisabledState(); } - private void RaiseCanExecuteChanged() + private void UpdateMenuItemsDisabledState() { - UndoCommand.RaiseCanExecuteChanged(); - RedoCommand.RaiseCanExecuteChanged(); - CutCommand.RaiseCanExecuteChanged(); - CopyCommand.RaiseCanExecuteChanged(); - DeleteCommand.RaiseCanExecuteChanged(); - PasteCommand.RaiseCanExecuteChanged(); - MarkAsGivenCommand.RaiseCanExecuteChanged(); + // an unfortunate work around for https://github.com/microsoft/microsoft-ui-xaml/issues/8894 + // The menu flyout item will loose it's foreground color if disabled from within the ICommands execute + // method when the system theme is light and the app's theme is dark. Fire and forget, so always need to + // call the CanExecute method first when executing to check it's still valid to execute. + Task.Run(() => + { + UndoCommand.RaiseCanExecuteChanged(); + RedoCommand.RaiseCanExecuteChanged(); + CutCommand.RaiseCanExecuteChanged(); + CopyCommand.RaiseCanExecuteChanged(); + DeleteCommand.RaiseCanExecuteChanged(); + PasteCommand.RaiseCanExecuteChanged(); + MarkAsGivenCommand.RaiseCanExecuteChanged(); + }); } private bool CanCutCopyDelete(object? param = null) => (selectedIndex >= 0) && Cells[selectedIndex].HasValue;