Replies: 3 comments
-
|
well, after 2,5 years, i too would like to know about this! |
Beta Was this translation helpful? Give feedback.
-
|
On Mar 7, 2023 @TheCodeTraveler writes CommunityToolkit/Maui#1056 (comment):
This explains why DynamicResource cannot be assigned to Behaviors, but it also suggests an interesting workaround. It works by assigning the DynamicResource to a property in a hidden VisualElement (e.g., Button.CommandParameter) and then extracting the StatusBarColor from it. For it to work, the VisualElement must be connected to see the merge dictionaries, hence the Parent line. <ContentPage x:Name="thisPage" ...>
<ContentPage.Behaviors>
<toolkit:StatusBarBehavior
StatusBarColor="{Binding CommandParameter,
x:DataType=Button,
Source={Button CommandParameter={DynamicResource Tertiary},
Parent={Reference thisPage}}}" />
</ContentPage.Behaviors>
</ContentPage>Another solution is to write an alternate DynamicResource that carries its own VisualElement with a settable Parent, e.g. <ContentPage.Behaviors>
<toolkit:StatusBarBehavior StatusBarColor="{local:DynamicResourceAlt Tertiary}" />
</ContentPage.Behaviors>[ContentProperty(nameof(Key))]
[RequireService([typeof(IReferenceProvider), typeof(IProvideValueTarget)])]
public partial class DynamicResourceAlt : Button, IMarkupExtension<BindingBase>
{
[BindableProperty] public partial string Key { get; set; } = string.Empty;
public DynamicResourceAlt()
{
this.Parent = Application.Current;
this.PropertyChanged += (s, e) =>
{
if (e.PropertyName == nameof(Key) && !string.IsNullOrEmpty(Key))
{
SetDynamicResource(Button.CommandParameterProperty, Key);
}
};
}
public object ProvideValue(IServiceProvider serviceProvider) => (this as IMarkupExtension<BindingBase>).ProvideValue(serviceProvider);
BindingBase IMarkupExtension<BindingBase>.ProvideValue(IServiceProvider serviceProvider)
{
if (serviceProvider.GetService(typeof(IProvideValueTarget)) is IProvideValueTarget provideValueTarget && provideValueTarget.TargetObject is BindableObject targetObject)
{
this.SetBinding(BindingContextProperty, static (BindableObject t) => t.BindingContext, BindingMode.OneWay, source: targetObject);
}
return BindingBase.Create(static (DynamicResourceAlt t) => t.CommandParameter, BindingMode.OneWay, source: this);
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Suggestion, if we create a new public partial class VisualElement : NavigableElement, IAnimatable, IVisualElementController, IResourcesProvider, IStyleElement, IFlowDirectionController, IPropertyPropagationController, IVisualController, IWindowController, IView, IControlsVisualElement, IConstrainedView
{
// public IList<Behavior> Behaviors
public IList<IBehavior> Behaviors
/* ... */Then someone can subclass VisualElement and an IBehavior interface as a new Behavior, and this may "help" support DynamicResource. How we may utilize this could be //public partial class IconTintColorBehavior : Behavior<View>
public partial class IconTintColorBehavior : VisualElement, IBehavior<View>
{
/* ... */
}<Image x:Name="globe" Source="globe_32x32.png">
<Image.Behaviors>
<toolkit:IconTintColorBehavior
TintColor="{DynamicResource TargetColor}",
Parent="{Reference globe}" />
</Image.Behaviors>
</Image>References:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I raised this Q&A and also a bug in the MCT repo:
CommunityToolkit/Maui#1054
It is a question related to doing a
DynamicResourcebinding against a bindable property on theStatusBarBehaior, if it is supported or a bug. If it is a bug I think it belongs here in the main MAUI repo.Here is a link to the bug in the MCT Repo:
CommunityToolkit/Maui#1056
So, is this a bug in the MAUI repo or a limitation? For me as a developer and consumer, since the XAML does not show any error I would expect and assume that I would be able to use a
DynamicResourcebinding for theStatusBarBehavior'sStatusBarColorbindable property.I also expressed in one of my comments that if I wanted to start investigating this, where would I start? I do not know who would own the
DynamicResourcebinding. When doing a normalBindI would expect it to be owned by the UI element itself, targeting a definedBindingContext. However,DynamicResourcebindings seem a bit more "magical" in that they work without aBindingContextbeing defined/assigned.There are several issues currently related to
DynamicResourcebindings not working in .NET 7 but in .NET 6. So I do not know if this problem is related toBehavors specifically or a core issue withDynamicResource.Update
I got information here CommunityToolkit/Maui#1056 that it can't use
DynamicResourcebecause it does not inherit from VisualElement or implement IResourcesProvider.I would argue that you should be able to use
DynamicResourceeven when not inheriting fromVisualElement. The reason being that you can put any type of value into aResourceDictionary, so in essence it is just a normal binding where the source is the parent or app'sResourceDictionary.Beta Was this translation helpful? Give feedback.
All reactions