【问题标题】:Xamarin xaml using Idiom to bind imageXamarin xaml 使用 Idiom 绑定图像
【发布时间】:2019-03-14 00:55:48
【问题描述】:

您好,我想根据设备类型在列表视图中显示不同的图像,如下所示

    <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 中找不到的错误字符串,请帮助..

【问题讨论】:

    标签: xaml xamarin.forms


    【解决方案1】:

    使用设备品牌设置图像源并在 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>
    

    【讨论】:

      【解决方案2】:

      尝试使用x:TypeArguments="x:String" 而不是x:TypeArguments="String"。

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

      【讨论】:

        【解决方案3】:

        试一试(虽然我不确定你想用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转换图像。

        如果遇到问题请告诉我!

        【讨论】:

        • PhoneImageData 或 TabletImageData 是来自数据库的 base64 字符串,作为列表项中的属性。所以我必须在那里使用绑定。图片在列表中
        猜你喜欢
        • 2023-04-04
        • 2018-07-07
        • 2017-02-09
        • 1970-01-01
        • 1970-01-01
        • 2017-04-22
        • 2016-12-16
        • 2017-06-03
        • 1970-01-01
        相关资源
        最近更新 更多