Xamarin xaml使用成语来绑定图像

时间:2016-03-27 15:18:23

标签: xaml xamarin.forms

您好我想在listview中显示不同的图片,具体取决于设备类型,如下所示

    <ContentPage.Resources>
    <ResourceDictionary>
      <local:Base64ToImageConverter x:Key="btoi"></local:Base64ToImageConverter>
    </ResourceDictionary>
  </ContentPage.Resources>
 <ListView x:Name="lvImages"
              VerticalOptions="FillAndExpand"
              SeparatorVisibility="Default"
              ItemsSource="{Binding Images}"
              ItemSelected="lvImages_Selected"
              HasUnevenRows="True"
              SelectedItem="{Binding SelectedImage}"
              IsPullToRefreshEnabled="True"
              CachingStrategy="RecycleElement"
              RefreshCommand="{Binding RefreshCommand}" 
              BackgroundColor="#009688"
              SeparatorColor="#FFFFFF"  >
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <!--<Image Source="{Binding PhoneImageData,Converter={StaticResource btoi}}" />-->
            <Image>
            <Image.Source>
              <Binding Converter="{StaticResource btoi}">
                <Path>
                  <OnIdiom x:TypeArguments="String">
                    <OnIdiom.Phone>
                      PhoneImageData
                    </OnIdiom.Phone>
                    <OnIdiom.Tablet>
                      TabletImageData
                    </OnIdiom.Tablet>
                  </OnIdiom>
                </Path>
              </Binding>
            </Image.Source>
          </Image>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

但是我收到了xamarin.forms.xaml中找不到的错误字符串,请帮助..

3 个答案:

答案 0 :(得分:0)

尝试一下(虽然我不确定您要对Binding属性做什么:

<OnIdiom x:TypeArguments="ImageSource"
         Phone="PhoneImageData.png"
         Tablet="TabletImageData.png"/>

编辑:为了更好地凝胶化你的例子:

<ContentPage.Resources>
    <ResourceDictionary>
        <OnIdiom x:Key="image" 
                 x:TypeArguments="ImageSource"
                 Phone="PhoneImageData.png"
                 Tablet="TabletImageData.png"/>
   </ResourceDictionary>
</ContentPage.Resources>

<Image Source="{StaticResource image}"/>

编辑#2: 好吧,这更有意义。然后我建议使用BindingContextChanged(),然后在该事件订阅中使用Device.Idiom检查。您可以使用this帖子查看如何执行此操作。你可以将你的图像设置为永远,然后在BindingContextChanged()中你可以将它重置为你想要的base64转换图像。

如果遇到问题请告诉我!

答案 1 :(得分:0)

尝试使用x:TypeArguments="x:String"代替x:TypeArguments="String"

除非它存在于XML文件的默认命名空间上下文中,否则您需要为每个类型引用指定命名空间。由于Xamarin.Forms.Xaml的根XML节点上的声明,默认上下文几乎总是xmlns="http://xamarin.com/schemas/2014/forms",但String位于System命名空间中。默认情况下,基本.NET数据类型(包括String和其他)在xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"中有别名,这就是您需要x:前缀的原因。

答案 2 :(得分:0)

使用设备品牌设置图像源并在xmal文件中键入:

<Image 
  VerticalOptions="FillAndExpand"
  BackgroundColor="White"
  Aspect="AspectFit">

    <Image.Source>      
      <OnIdiom x:TypeArguments="ImageSource">
          <OnIdiom.Phone>
            <OnPlatform x:TypeArguments="ImageSource"
                          iOS="logo.png"
                          Android="logo.png"
                          WinPhone="logo.png" />
          </OnIdiom.Phone>
          <OnIdiom.Tablet>
            <OnPlatform x:TypeArguments="ImageSource"
                          iOS="logo_iPad.png"
                          Android="logo.png"
                          WinPhone="logo.png" />
          </OnIdiom.Tablet>
      </OnIdiom>
    </Image.Source>