首页 文章

XAML标签文本:绑定DynamicResource(字符串格式?)

提问于
浏览
2

对于Xamarin.Forms - XAML文件:

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

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

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

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

  • 或使用值转换器将绑定字符串返回到"binding string + dynamic resource"? (为此创建一个valueconverter似乎太过于压倒性)

  • 在代码中这可能适用于string.Format(...)

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

  • 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中的常规属性 .

相关问题