question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AvaloniaProperty default value factory

See original GitHub issue

Is your feature request related to a problem? Please describe. It should be possible to set default value factory.

Describe the solution you’d like

public static StyledProperty<TValue> Register<TOwner, TValue>(string name, Func<TValue> defaultValueFactory, bool inherits = false, BindingMode defaultBindingMode = BindingMode.OneWay, Func<TValue, bool> validate = null, Func<IAvaloniaObject, TValue, TValue> coerce = null, Action<IAvaloniaObject, bool> notifying = null) where TOwner : IAvaloniaObject;
public static AttachedProperty<TValue> RegisterAttached<TOwner, THost, TValue>(string name, Func<TValue> defaultValueFactory, bool inherits = false, BindingMode defaultBindingMode = BindingMode.OneWay, Func<TValue, bool> validate = null, Func<IAvaloniaObject, TValue, TValue> coerce = null) where THost : IAvaloniaObject;
public static AttachedProperty<TValue> RegisterAttached<THost, TValue>(string name, Type ownerType, Func<TValue> defaultValueFactory, bool inherits = false, BindingMode defaultBindingMode = BindingMode.OneWay, Func<TValue, bool> validate = null, Func<IAvaloniaObject, TValue, TValue> coerce = null) where THost : IAvaloniaObject;

Additional context WPF doesn’t have this feature. But UWP does: CreateDefaultValueCallback Delegate

Property = DependencyProperty.Register("MyProperty",
    propertyType, ownerType,
    PropertyMetadata.Create(createDefaultValue));

Possible use cases

  1. Storing mutable objects inside of property, and default value should be new for each avalonia object having it - https://devblogs.microsoft.com/oldnewthing/20191002-00/?p=102950 (actually it’s a pretty BAD idea, and this topic describes why).
  2. When default value is read from global settings class or any other static source that can be mutated, and default value should be recalculated.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
grokyscommented, Jan 18, 2021

The UWP implementation has a serious problem: https://devblogs.microsoft.com/oldnewthing/20191002-00/?p=102950

It may be possible for Avalonia to avoid that problem, but it’s a bit tricky.

EDIT: Oh I just noticed you already linked to that, sorry, Anyway I think that unless we can avoid the problem described there I’d be 👎 on adding default value factories.

0reactions
robloocommented, Jan 19, 2021

I would be willing to bet Microsoft has changed the intended use/functionality of this. Originally, it might have been a full factory created to facilitate instantiating objects used on the UI thread from another thread. Then they did some backtracking and clarified in the documents it should only be used for the very narrow case. That could explain why everyone has different impressions and tests dont always match the docs. It could also be that it never worked correctly so they narrowed scope to the only tested and validated use case.

Reguardless, treating this as a factory in Avalonia would be great. It could be used to instantiate collections for each dependency object (equivalent in Avalonia) without doing so in the constructor which is cleaner IMO.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Defining Properties
defaultValue : This gives the property a default value. Be sure to only pass value types and immutable types here as passing a...
Read more >
The default value for a XAML dependency property should ...
The default value of a dependency property is the value used if no value has been set: Nobody has explicitly assigned a value,...
Read more >
How can a property take the default values defined in ...
I have a class with non-nullable values whose default values I have defined in the constructor. class AirConPreferences { bool allowOn; int ...
Read more >
ReactiveProperty documentation
The value property raise the PropertyChanged event. Implements the IObservable<T> interface. Yes, The value property can bind to XAML control's property. And ...
Read more >
equadrat.Framework.Core.UI.Maui 5.8.5
- Added extension method GetDefaultValue() for System.Type to get the default value for that type. - Added eCoreDispatcherPriority.Layout as an ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found