XAML标签文本:绑定+动态资源(字符串格式?)

时间:2017-09-06 11:50:37

标签: c# xaml xamarin binding dynamicresource

对于Xamarin.Forms - XAML文件:

有没有办法将Label的文本属性(在XAML中)绑定到 Binding + DynamicResource ?也许用字符串格式?

例如我尝试过这样的事情:

<Label Text="{DynamicResource resource, Binding binding, StringFormat='Resource: {0} and Binding: {1}"} />

但是如果设置了动态资源,则无法声明绑定,如果尝试相反则会出现相同的问题(例如,如果绑定已经设置则没有动态资源)

  • 或使用值转换器将绑定字符串返回到&#34;绑定字符串+动态资源&#34;? (为此创建一个valueconverter似乎太过分了)
  • 在代码中,这可能适用于string.Format(...)

2 个答案:

答案 0 :(得分:2)

在Xamarin.Forms应用程序中不支持MultiBinding。

这是一个很好的解决方法,可以实现对Xamarin的完全多绑定支持:

http://intellitect.com/multibinding-in-xamarin-forms/

这是一个可以使用的更简单的实现:

https://gist.github.com/Keboo/0d6e42028ea9e4256715

关于这个问题的讨论:

https://forums.xamarin.com/discussion/21034/multibinding-support

答案 1 :(得分:0)

我认为你需要的是MultiBinding。

尝试创建这样的转换器类:

public class MultiBindingConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return values[0].ToString() + " " + values[1].ToString();
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

在App.xaml或其他资源词典中引用它

<local:MultiBindingConverter x:Key="MultiBindingConverter" />

然后在您的视图中执行以下操作:

<Label>
    <Label.Content>
        <MultiBinding Converter="{StaticResource MultiBindingConverter}">
            <Binding Path="FirstProperty" />
            <Binding Path="SecondProperty" />
        </MultiBinding>
    </Label.Content>
</Label>

FirstProperty和SecondProperty只是ViewModel中的常规属性。

相关问题