3

对于 Xamarin.Forms - XAML 文件:

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

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

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

但是如果设置了动态资源,则无法声明绑定,反之亦然(例如,如果绑定已设置,则没有动态资源)

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

2 回答 2

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

于 2017-09-06T13:22:32.150 回答
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 中的常规属性。

于 2017-09-06T13:09:45.953 回答