То, что вы, вероятно, хотите, это конвертер значений, который обрабатывает поиск StaticResource для вас. Полная документация Microsoft здесь .
На вашем элементе XAML вы бы сделали что-то вроде этого:
<Entry Style="{Binding foo, Converter={StaticResource FooToStyleConverter}}"/>
Ваш конвертер будет работать примерно так:
public class FooToStyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var someValue = (string)value; // Convert 'object' to whatever type you are expecting
// evaluate the converted value
if (someValue != null && someValue == "bar")
return (Style)App.Current.Resources["StyleOne"]; // return the desired style
return (Style)App.Current.Resources["StyleTwo"];
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// Usually unused, but inverse the above logic if needed
throw new NotImplementedException();
}
}
Наконец, настройте конвертер как статический ресурс в App.xaml (или как локальный ресурс на странице), чтобы ваша страница могла правильно ссылаться на него
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:DataBindingDemos">
<ContentPage.Resources>
<ResourceDictionary>
<local:FooToStyleConverter x:Key="FooToStyleConverter" />
....