c#
  • xaml
  • xamarin
  • binding
  • dynamicresource
  • 2017-09-06 76 views 2 likes 
    2

    对于Xamarin.Forms - XAML文件:XAML标签文本:绑定+ DynamicResource(字符串格式?)

    有绑定的方式文本属性的标签(在XAML)的绑定+ DynamicResource?也许与字符串格式?

    例如我想是这样的:

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

    但如果想它反之亦然,如果绑定已设置一个不能宣布一个绑定,如果动态资源设置,同样的问题(例如,没有dynamicresource。 )

    • 或使用值转换器将绑定字符串返回到“绑定字符串+动态资源”? (创建valueconverter这似乎过于庞大)
    • 代码,这可能与的String.format(...)工作
    +0

    看起来像转换器('IValueConverter')的工作给我... – Milen

    +0

    然后你在执行它吗? :) – NullReference

    回答

    2

    看起来像Xamarin.Forms应用程序不支持MultiBinding。

    这是一个很好的解决方法一种实现对Xamarin全multibinding支持:

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

    下面是一个简单的实现就可以使用:

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

    和讨论关于这个问题:

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

    +0

    非常感谢, http://intellitect.com/multibinding-in-xamarin-forms/看起来像一个有希望的方式去。如果我得到一个不错的结果,我会调查这个并更新这篇文章。 – Csharpest

    +1

    我添加了一个新的链接到我的答案与一个简单的准备使用类;) – Isma

    +0

    非常感谢,我现在将如何处理这个问题?选择正确的答案,或等待我更新它,只要我上传我的工作解决方案,然后勾选我的上传呢? – Csharpest

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

    +0

    这是关于Xamarin.Forms,我无法找到任何MultiBinding Xamarin.Forms。你在你的例子中是否以WPF为目标? – Csharpest

    +1

    对不起,没有注意到Xamarin的标签。是的,我的回答与WPF相关 –

    相关问题