【问题标题】:Xamarin Binding fuction as ImageSource inside a Listview with DatabindingXamarin 绑定功能作为具有数据绑定的 Listview 内的图像源
【发布时间】:2019-10-19 00:40:12
【问题描述】:

我正在尝试在下面的 ListView 中可视化一个名为 MinRepresentation 的对象的状态。

我想绑定函数ImageSource stateImage(MinRepresentationState state),在这里我切换状态并取回Imagesource。

我的问题是,访问函数并通过 xaml 传递参数。

Visible OrdersMinRepresentation 的集合,每个都包含 State

<ListView HasUnevenRows="True" SelectionMode="Single" ItemsSource="{Binding VisibleOrders}" ItemSelected="OnListViewItemSelected" ItemTapped="OnListViewItemTapped">

***OTHER STUFF***

<StackLayout Orientation="Horizontal" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" Padding= "0,5,0,5" BackgroundColor="#CC3F6E3F">

<Image Source="{Binding stateImage(State)" Margin="10,0,0,0" />

<Label Text="{Binding Id, StringFormat='ID: [{0}]'}" FontSize="Small" Margin="5,0,0,0" FontAttributes="Bold" TextColor="#FFFFFF"/>

</StackLayout>

***OTHER STUFF***

</ListView>
public ImageSource stateImage(MinRepresentationState state)
        {

            switch (state)
            {
                case MinRepresentationState.Assigned:
                    return ImageSource.FromResource("state_assigned.png");
            }
        }

【问题讨论】:

  • 只能绑定公共属性,不能绑定方法
  • 知道如何解决这个问题吗?

标签: image listview xamarin binding


【解决方案1】:

您只能绑定到公共属性。在模型上创建 StateImage 属性

public string StateImage 
{
  get {
    switch (State)
    {
      case MinRepresentationState.Assigned:
        return "state_assigned.png";
    }
  }
}

【讨论】:

    【解决方案2】:

    您还可以使用 DataTrigger 在 XAML 中设置图像

    <Image Source="state_default.png" Margin="10,0,0,0">
        <Image.Triggers>
            <DataTrigger TargetType="Image" Binding="{Binding State}" Value="Assigned">
                <Setter Property="Source" Value="state_assigned.png" />
            </DataTrigger>
        </Image.Triggers>
    </Image>
    

    参考:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/triggers#data

    【讨论】:

      【解决方案3】:

      您可以使用IValueConverter 来实现这一点。创建一个IValueConverter 类并将您的逻辑代码放在那里:

      public class StateToImageConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
              MinRepresentationState representationState = (MinRepresentationState)value;
              if (representationState == MinRepresentationState.Assigned)
              {
                  return "state_assigned.png";
              }
              return "otherImage.png";
          }
      
          public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      }
      

      然后像这样在 XAML 中使用它:

      <!--Define it in resources-->
      <ContentPage.Resources>
          <local:StateToImageConverter x:Key="StateToImageConverter"/>
      </ContentPage.Resources>
      
      <!--Use converter-->
      <Image Source="{Binding State, Converter={StaticResource StateToImageConverter}}" Margin="10,0,0,0"/>
      

      【讨论】:

      • 这个工作到目前为止,我现在面临的唯一问题是,图片不加载。提前感谢所有帮助:) 错误:[0:] Could not load image named: {0}: state_new.png [0:] FileImageSourceHandler: Could not find image or image file was invalid: File: state_new.png
      • @Erstingo 很高兴它帮助了你:)。
      • 我试过了:C# return ImageSource.FromResource("state_new.png"); return "state_new.png"; return "Images/state_new.png";
      • @Erstingo 无需返回图片源,使用字符串即可:docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/…
      • [0:] Could not load image named: {0}: state_new.png 可能是因为StateToImageConverter 类与图像不在同一个命名空间中?
      猜你喜欢
      • 1970-01-01
      • 2017-08-15
      • 2018-08-14
      • 2018-10-19
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      • 2019-03-25
      • 1970-01-01
      相关资源
      最近更新 更多