Difference between revisions of "Traits"
(→SetValueCommand) |
|||
| (4 intermediate revisions by the same user not shown) | |||
| Line 4: | Line 4: | ||
This feature is particularly useful for data that only becomes known at runtime or varies significantly from instance to instance. Please note that traits do not use lazy loading, making them act rather "heavy weight" from a user's perspective. | This feature is particularly useful for data that only becomes known at runtime or varies significantly from instance to instance. Please note that traits do not use lazy loading, making them act rather "heavy weight" from a user's perspective. | ||
| + | |||
| + | Supported types: | ||
| + | * int | ||
| + | * double | ||
| + | * string | ||
| + | * bool | ||
| + | * date time | ||
{{Attention|This feature must be used wisely, too many traits could impact the performance. Traits do not replace properties.}} | {{Attention|This feature must be used wisely, too many traits could impact the performance. Traits do not replace properties.}} | ||
| Line 35: | Line 42: | ||
=== 3. Values (Raw Data) === | === 3. Values (Raw Data) === | ||
| − | + | See [[XAML#Values.5BMP_PROPERTY.5D|Values]] | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
---- | ---- | ||
| Line 50: | Line 50: | ||
=== SetValueCommand === | === SetValueCommand === | ||
This command on the <code>ContentViewModel</code> orchestrates value assignments and handles the '''creation of new traits''' if necessary. | This command on the <code>ContentViewModel</code> orchestrates value assignments and handles the '''creation of new traits''' if necessary. | ||
| − | * '''If | + | * '''If a trait exists:''' The value will be overwritten. |
| − | * '''If a property exists:''' The property's value will be overwritten (if it is not locked/read-only). | + | * '''If a editable property exists:''' The property's value will be overwritten (if it is not locked/read-only). |
| − | * ''' | + | * '''Otherwise:''' A trait is added to the {{UBIK}} content. |
'''XAML Example (Setting a value):''' | '''XAML Example (Setting a value):''' | ||
| Line 58: | Line 58: | ||
<syntaxhighlight lang="xml"> | <syntaxhighlight lang="xml"> | ||
| + | xmlns:classes="clr-namespace:UBIK.MAUI.Classes;assembly=UBIK.MAUI" | ||
| + | |||
<Button Command="{Binding SetValueCommand}" Text="Set"> | <Button Command="{Binding SetValueCommand}" Text="Set"> | ||
<Button.CommandParameter> | <Button.CommandParameter> | ||
| Line 72: | Line 74: | ||
[[Category:Client|Traits]] | [[Category:Client|Traits]] | ||
| − | |||
[[Category:Mobile|Traits]] | [[Category:Mobile|Traits]] | ||
| + | [[Category:WinX|Traits]] | ||
| + | [[Category:Version 5.1|Traits]] | ||
Latest revision as of 08:55, 28 April 2026
Contents
Traits
UBIK® Traits provide the ability to define a dynamic list of properties for individual instances (UBIK® objects). Unlike traditional UBIK® Properties, which are rigidly bound to a MetaClass, instances of the same entity can contain a different list of traits.
This feature is particularly useful for data that only becomes known at runtime or varies significantly from instance to instance. Please note that traits do not use lazy loading, making them act rather "heavy weight" from a user's perspective.
Supported types:
- int
- double
- string
- bool
- date time
| This feature must be used wisely, too many traits could impact the performance. Traits do not replace properties. |
ViewModel Architecture & Data Access
To work with traits in the UI (XAML), the ContentViewModel provides several dedicated accessors and collections.
1. ValueAccessors (Read/Write)
The ValueAccessors acts as a combined dynamic value resolver. It provides a built-in fallback logic:
- Trait Lookup: First attempts to resolve the value via a trait with the specified name.
- Property Fallback: If no trait is found, it queries a regular UBIK® Property with that name (see PropertyByName).
- Default: If neither exists, it returns
null.
XAML Example (Reading a value):
(Replace NAME with the placeholder of your actual trait or property name)
2. TraitByName (Traits Only)
TraitByName is a dedicated collection (ObservableTraitDictionary) that exclusively loads and initializes traits. Unlike the ValueAccessors, this does not fall back to regular properties.
- Usage: When you explicitly want to query only traits (e.g., read-only display in the UI).
XAML Example (Reading only Trait):
(Replace TRAIT_NAME with the placeholder of your actual trait)
3. Values (Raw Data)
See Values
Commands (Set & Create Values)
SetValueCommand
This command on the ContentViewModel orchestrates value assignments and handles the creation of new traits if necessary.
- If a trait exists: The value will be overwritten.
- If a editable property exists: The property's value will be overwritten (if it is not locked/read-only).
- Otherwise: A trait is added to the UBIK® content.
XAML Example (Setting a value):
To create traits entirely from scratch (or explicitly overwrite an existing value), pass the Name and Value parameters to the SetValueCommand.
<Button Command="{Binding SetValueCommand}" Text="Set">
<Button.CommandParameter>
<classes:KeyValueList>
<classes:KeyValueParameter Key="Name" Value="MY_TRAIT_NAME" />
<classes:KeyValueParameter Key="Value" Value="MY_TRAIT_VALUE" />
</classes:KeyValueList>
</Button.CommandParameter>
</Button>
(Note: Replace MY_TRAIT_NAME & MY_TRAIT_VALUE with the placeholder of your actual trait or property name/value)
