ʞᴉɯ*_*ʞᴉɯ 1 xaml xamarin.forms
是否可以在 Xamarin Forms 中对静态资源进行数据绑定?就像是
Style="{StaticResource {Binding foo, StringFormat='SomeStyle{0}'}}"
Run Code Online (Sandbox Code Playgroud)
谢谢
您可能想要的是一个值转换器,它可以为您定位 StaticResource。完整的 Microsoft 文档在这里。
在您的 XAML 元素上,您将执行以下操作:
<Entry Style="{Binding foo, Converter={StaticResource FooToStyleConverter}}"/>
Run Code Online (Sandbox Code Playgroud)
你的转换器会像这样工作:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
最后,将转换器设置为 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" />
....
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2407 次 |
| 最近记录: |