-
Notifications
You must be signed in to change notification settings - Fork 1.9k
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Enhancement] Creating a bindable property with enum backing does not select enum default #2826
Comments
If there is going to be a default, it should be explicit. Placing a value first in an enum does not imply that it is intended to be a default. No need to make bindings even more hacky than they are already. |
I should have been clear about default enum values. You're right that the first enumeration isn't always the default one. Here's an example:
Official documentation for bindable properties mentions the following for the
which, to me, implies that I'd say the documentation should make it clear that for enums and structs, an explicit initialization must be provided or the |
The documentation is wrong. The default value is of type object and the default is null. When the property type is a value type, the code is trying to cast null to a value type and of course that throws an exception. So either the code needs to change so it uses default(T) in case the default value is null, or the documentation should make it clear that the provided default value should be something that can be casted the property type. |
Fixing the code to do what the documentation says looks pretty easy. Adding this to the BindableProperty constructor should do it:
I'll create a PR for it and see what they say. |
See PR #2827 |
Description
When I create a bindable property for an enum, I have to explicitly set a default value in the Bindable.Create() method for that enum otherwise a runtime error will occur. Why is that? In my opinion, by default, the first member of an enum type should be chosen by the Create() method. See attached.
This issue seems to exist for structs as well. For example, if you create a bindable property for
TimeSpan
, you need to explicitly donew TimeSpan()
ordefault(TimeSpan)
while in reality aTimeSpan
defaults to "00:00:00".Reproduction Link
Demo.zip
The text was updated successfully, but these errors were encountered: