Xamarin绑定功能作为具有数据绑定的Listview中的ImageSource

时间:2019-06-03 12:45:54

标签: image listview xamarin binding

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

我想绑定功能ImageSource stateImage(MinRepresentationState state),在这里我将状态移交并取回图像源。

我的问题是,通过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");
            }
        }

3 个答案:

答案 0 :(得分:0)

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

    Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
            at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
            at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
            at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
            at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:73)
            at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:48)
    Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
            at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
            at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
            at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
            ... 5 more

答案 1 :(得分:0)

您还可以使用&&在XAML中设置图片

DataTrigger

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

答案 2 :(得分:0)

您可以使用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"/>