【问题标题】:XAML Label Text: Binding + DynamicResource (String Format?)XAML 标签文本:绑定 + DynamicResource(字符串格式?)
【发布时间】:2018-02-14 21:37:21
【问题描述】:

对于 Xamarin.Forms - XAML 文件:

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

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

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

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

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

【问题讨论】:

  • 对我来说似乎是一个转换器 (IValueConverter) 的工作......
  • 那你实施了吗? :)

标签: c# xaml xamarin binding dynamicresource


【解决方案1】:

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

【讨论】:

  • 非常感谢,intellitect.com/multibinding-in-xamarin-forms 看起来很有前途。如果我得到一个不错的结果,我会对此进行调查并更新这篇文章。
  • 我在答案中添加了一个新链接,其中包含一个简单的即用型类;)
  • 非常感谢,我现在将如何处理这个问题?将您的答案勾选为正确,或者等我上传我的工作解决方案后立即更新它,然后勾选我的上传?
  • 好吧,如果您认为我提供了问题的答案:有没有办法将标签的文本属性(在 XAML 中)绑定到 Binding + DynamicResource?然后将其标记为答案,但这当然取决于您。
  • 你是对的。而且我仍然可以根据您的回答编辑我的帖子以显示我的解决方案!祝你有美好的一天
【解决方案2】:

我认为您需要的是 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 中的常规属性。

【讨论】:

  • 这是关于 Xamarin.Forms,我在 MultiBinding for Xamarin.Forms 上找不到任何内容。你在你的例子中是针对 WPF 的吗?
  • 抱歉,没有注意到 Xamarin 标签。是的,我的回答与 WPF 有关
猜你喜欢
  • 2011-04-27
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
相关资源
最近更新 更多