【问题标题】:Xamarin Forms Platform Specific Image Source Binding in xamlXamarin 在 xaml 中形成平台特定的图像源绑定
【发布时间】:2016-12-16 21:38:27
【问题描述】:

我正在尝试在 Xamarin 表单中绑定 xaml 中的图像源,但出现以下错误。

无法分配属性“iOS”:类型不匹配 “Xamarin.Forms.Binding”和“Xamarin.Forms.ImageSource”

不过,我可以对图像源进行硬编码,但我必须在很多地方进行此操作,因此我更愿意将其绑定到一个属性,如果更改该属性可以在整个项目中反映出来。任何人都可以对我遇到的错误有所了解吗?

我目前正在使用转换器类来检索源。以下是我当前产生错误的代码。此外,我不想转向基于代码的解决方案,而是希望看看这是否可以在 XAML 中解决。

<Image 
    Aspect="AspectFill"
    Grid.Row="0"
    Grid.Column="3"
    Grid.RowSpan="2"
    VerticalOptions="Center"
    HorizontalOptions="End">

    <Image.Source>
        <OnPlatform x:TypeArguments="ImageSource"
            iOS="{Binding .,Converter={StaticResource getGestureIcon},ConverterParameter=Swipe}"
            Android="{Binding .,Converter={StaticResource getGestureIcon},ConverterParameter=LongPress}" />
    </Image.Source>
</Image>

谢谢

【问题讨论】:

    标签: binding xamarin.forms imagesource


    【解决方案1】:

    @Rui Marinho:我从来没有这样想过。那可能行得通。但是,我以不同的方式实现它,即定义一个静态属性并直接引用它。使用来自How to bind class properties in XAML of xamarin forms的解决方案

    下面是代码。

    xmlns:Icon="clr-namespace:GetIcon"
    
     <Image 
      Aspect="AspectFill"
      Grid.Row="0"
      Grid.Column="2"
      Grid.RowSpan="2"
      VerticalOptions="Center"
      HorizontalOptions="End">
      <Image.Source>
      <OnPlatform 
        x:TypeArguments="ImageSource"
        iOS="{x:Static Icon:GetIcon.SwipeLeft}"
        Android="{x:Static Icon:GetIcon.LongTap}" />
      </Image.Source>
     </Image>
    

    【讨论】:

      【解决方案2】:

      您过于复杂并且使用 OnPlatform 没有绑定支持。在您的转换器中检查您所在的平台。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-25
        • 1970-01-01
        • 2017-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多