Resolve resources in binding conversion #2447
-
Good day all, My attempt is pretty straight forward, yet I am unable to resolve the theme resource I attempted to work around this using different Thanks in advance for any advice. More generally, I am attempting to setup a new TabBar().DataContext(new BindableNavigatorModel(), (bar, vm) =>
[...]
new TabBarItem
.Resources(r => r.Add(icon))
.Children(
new PathIcon()
.Data(icon)
.Foreground(
() => model.SelectedPage,
selectedPage => TODO_THEME_BRUSH_FROM_KEY(
selectedPage == typeof(MyPage) ? Theme.Brushes.Primary.Default : Theme.Brushes.Primary.Disabled
)
)
); public partial record NavigatorModel
{
public NavigatorModel()
{
App = (App)Application.Current;
ArgumentNullException.ThrowIfNull(App.Frame?.CurrentSourcePageType);
SelectedPage = State.Value(this, () => App.Frame.CurrentSourcePageType);
App.Frame.Navigated += (_, _) => SelectedPage.Update(_ => App.Frame.CurrentSourcePageType, default);
}
public App App { get; }
public IState<Type> SelectedPage { get; }
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @ProphetLamb, you should be able to use .Background(x => x.Binding(() => vm.IsToggleOn).Convert(ConvertIsOnToBrush)) private Brush ConvertIsOnToBrush(bool isOn)
{
return isOn ? GetBrush("PrimaryBrush") : GetBrush("TertiaryBrush");
}
private Brush GetBrush(string key)
{
if (Application.Current.Resources.TryGetValue(key, out var brush))
{
return brush as SolidColorBrush;
}
throw new KeyNotFoundException($"The resource '{key}' was not found.");
} |
Beta Was this translation helpful? Give feedback.
Hey @ProphetLamb,
you should be able to use
TryGetValue
and apply the value to the Foreground directly. I tested this with a little bit different sample, so you will need to adjust it for your case. But for the general idea, you can do something like this: