Xamarin样式静态资源绑定 [英] Xamarin Style staticresource binding
本文介绍了Xamarin样式静态资源绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在Xamarin Forms中将静态资源进行数据绑定? 像
would it be possible to databind a static resource in Xamarin Forms? Something like
Style="{StaticResource {Binding foo, StringFormat='SomeStyle{0}'}}"
谢谢
推荐答案
您可能想要的是一个Value Converter,它可以为您处理StaticResource的位置.完整的Microsoft文档此处
What you probably want is a Value Converter that handles locating the StaticResource for you. Full Microsoft Documentation here.
在XAML元素上,您将执行以下操作:
On your XAML element, you'd do something like this:
<Entry Style="{Binding foo, Converter={StaticResource FooToStyleConverter}}"/>
您的转换器将像这样工作:
Your converter would work something like this:
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中的静态资源(或页面上的本地资源),以便您的页面可以正确地引用它.
Lastly, set up your converter as a Static Resource in App.xaml (or as a local resource on the page) so your page can properly reference it
<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" />
....
这篇关于Xamarin样式静态资源绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文