Repository navigation
Guard BrushToHexConverter against non-brush input - #4123
Open
karlmoriaty wants to merge 1 commit into
Open
karlmoriaty wants to merge 1 commit into
karlmoriaty wants to merge 1 commit into
Conversation
The converter casts its input straight to SolidColorBrush. At design time the DataContext of the hex labels can fall back to the mock view model the XAML designer builds for d:DesignInstance, so that cast throws and the designer reports the error described in MaterialDesignInXAML#1411. Return null for anything that is not a SolidColorBrush; the runtime path always passes a brush, so its behaviour is unchanged. Assisted-by: DeepSeek:deepseek-v4.1-flash:WorkBuddy Signed-off-by: karlmoriaty <3070687583@qq.com>
Author
|
@Keboo the Pull Request Verification workflow is waiting on first-time contributor approval. Could you approve it when you get a chance? The change is the one-line cast replacement described above. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
tl;dr
BrushToHexConvertercasts its input straight toSolidColorBrush. In the XAMLdesigner that input can be the mock view model, so the cast throws and the design
surface fails with the error from #1411. I added a type check.
What
One line in
src/MaterialDesignDemo.Shared/Converters/BrushToHexConverter.cs:the explicit cast is replaced by a pattern match that returns
nullwhen theinput is not a brush.
Why
ColorTool.xamlbinds the hex labels like this:At runtime
PrimaryHueMidBrushalways resolves, so the converter always gets abrush and nothing changes.
In the designer the lookup can fail.
DataContextthen falls back to theinherited one, which is the mock object the designer creates for
d:DesignInstance Type=sharedDomain:ColorToolViewModel. The cast throws:That is #1411. With the type check the label renders empty in the designer
instead of breaking the design surface.
Scope
Type guard only. The design-time view model suggested in #1411 would also give
the designer real data, but that is a bigger change and I would rather keep
this one small.
Fixes #1411
Verification
dotnet build src/MainDemo.Wpf/MaterialDesignDemo.csproj -f net8.0-windows: 0 warnings, 0 errorsSolidColorBrush