I have some C# (MAUI) Color properties, and I've written the setter code to only do something if the new value is different from the old, but found it always thinks the new value is different, even when it isn't.
I tried casting to (Color)value and comparing, and that didn't work.
I tried converting both to strings - i.e. _foreground.tostring()!=value.tostring() - and the app would crash (null-reference for some reason)
...and now I've run out of ideas for things to try. Why is it thinking these 2 values are different? (the only thing I can think of is maybe there's a non-displaying character somewhere?)
private Color _foreground;
public Color Foreground
{
get {return _foreground;}
set {
if (_foreground!=value) {
System.Diagnostics.Trace.Write($"********************************** _foreground is {_foreground}\r\n");
System.Diagnostics.Trace.Write($"********************************** value is {value}\r\n");
_foreground=value;
// Rest of property change handling code...
}
}
}
Constructor
Foreground=Color.FromArgb(Preferences.Get(nameof(Foreground),"#FF00CC00"));
Saving (but no change has been made to FgColourEntry.Text in this case)
Colours.Foreground=Color.FromArgb(FgColourEntry.Text);
Output from above (i.e. when Foreground has already been set to hex #FF00CC00 previously, and no change this time, so it's the exact same hex string)...
********************************** _foreground is [Color: Red=0, Green=0.8, Blue=0, Alpha=1]
********************************** value is [Color: Red=0, Green=0.8, Blue=0, Alpha=1]
As far as I can see, identical, and yet this debugging code shouldn't be running in the first place if they're identical, only if the new value is different.
thanks, Donald.