【问题标题】:Binding a list of string to a Xamarin Xaml with a converter使用转换器将字符串列表绑定到 Xamarin Xaml
【发布时间】:2017-07-13 13:34:39
【问题描述】:

我有一个名为 TransportModeType 的枚举列表绑定到一个 Listview,数据模板中有一个转换器。 ViewModel 看起来像这样

public class TransportTypeViewMode:ViewModelBase{
public TransportTypeViewMode()
{
    TransportTypes= new List<TransportTypeEnum>();
    TransportTypes.add(TransportTypeEnum.Car);
    TransportTypes.add(TransportTypeEnum.Bus);
    TransportTypes.add(TransportTypeEnum.Plane);
}

List<TransportTypeEnum> TransportTypes{get;set;}}

ListView 是这样的

  <ListView ItemsSource="{Binding TransportModeTypes}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ContentView>
                                <Image Source="{Binding Converter={StaticResource ListToImageConverter}}" Aspect="AspectFit"/>
                            </ContentView>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

我的名为 ListToImageConverter 的转换器看起来像这样

public class TransportModeEnumToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var transporttModeType = (TransportModeTypeEnum) value;
        string imagePath = String.Empty;    

        switch (transporttModeType)
        {
            case TransportModeTypeEnum.Bus:
                imagePath = "bus.png";
                break;
            case TransportModeTypeEnum.Train:
                imagePath = "train.png";
                break;
            case TransportModeTypeEnum.Car:
                imagePath = "car.png";
                break;
            case TransportModeTypeEnum.Plane:
                imagePath = "plane.png";
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }


        imagePath = "Common.Images." + imagePath;
        var imgResc= ImageSource.FromResource(imagePath);
        return imgResc;
    }


    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

转换器抛出一个异常值是空的..我的绑定中缺少什么

【问题讨论】:

  • 只需添加一个空检查并返回一个空字符串或其他东西。绑定完成后,它有时会被初始化为我认为的空值。您可能可以放心地忽略它
  • 我认为您需要在绑定表达式中指定绑定路径
  • 伟大的收获@Jason!完全忽略了那个
  • @GeraldVersluis 是对的,非常感谢
  • 将其更新为答案。

标签: c# visual-studio xaml xamarin xamarin.forms


【解决方案1】:

编辑:

您首先需要将枚举转换为字符串,因为枚举不是列表视图的有效项目源。所以你也需要使用转换器。

视图模型:

public class TransportTypeViewMode : ViewModelBase
{
    public TransportTypeViewMode()
    {
        TransportTypes= new List<TransportTypeEnum>();
        TransportTypes.add(TransportTypeEnum.Car);
        TransportTypes.add(TransportTypeEnum.Bus);
        TransportTypes.add(TransportTypeEnum.Plane);
    }      

    Public List<TransportTypeEnum> TransportTypes{get;set;}}
}

您的枚举到字符串转换器:

public class TransportModeEnumToString : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var transporttModeType = (TransportModeTypeEnum) value;
        string transportName = String.Empty;    

        switch (transporttModeType)
        {
            case TransportModeTypeEnum.Bus:
                transportName = "bus";
                break;
            case TransportModeTypeEnum.Train:
                transportName = "train";
                break;
            case TransportModeTypeEnum.Car:
                transportName = "car";
                break;
            case TransportModeTypeEnum.Plane:
                transportName = "plane";
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
        return transportName;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

你的字符串到图像源转换器:

public class StringToImagesourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string transporttModeType = (string)value;
        string imagePath = String.Empty;    

        switch (transporttModeType)
        {
            case "bus":
                break;
            case TransportModeTypeEnum.Train:
                imagePath = "train.png";
                break;
            case "car":
                imagePath = "car.png";
                break;
            case "plane":
                imagePath = "plane.png";
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }

        imagePath = "Common.Images." + imagePath;
        var imgResc= ImageSource.FromResource(imagePath);
        return imgResc;
    }
}

你的 xaml 应该是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:c="location of your converter classes"
             >
<ContentPage.Resources>
  <c:EnumToStringConverter x:Key="enumtostring" />
  <c:StringToImagesourceConverter x:Key="stringtoimagesource" />
</ContentPage.Resources>
<ContentPage.Content>
    <ListView ItemsSource="{Binding TransportModeTypes Converter={StaticResource EnumToStringConverter}}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ContentView>
                            <Image Source="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource StringToImagesourceConverter}}" Aspect="AspectFit"/>
                        </ContentView>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
    </ListView>
</ContentPage.Content>

【讨论】:

  • 嗨,我的问题是 datatemplate 中的绑定。我在图像控件中使用的转换器为空。
  • 虽然巧合的是,这一切都在 Xamarin.Forms 中有效,但您似乎在发布 WPF XAML,因为 Window 在 Xamarin 中不是一个东西。
  • 另外,解决方案似乎是一个窗口解决方案,需要一个用于 xamarin 的解决方案
  • @GeraldVersluis 是的,很抱歉 WPF 是我可以根除的第一个与转换器有这种绑定的,但你说得对,它确实在如此不同的 Xamarin 中正确编译。表单 xaml。
  • @Dan 让我为你重新编写答案,请耐心等待。
【解决方案2】:

只需添加一个空检查并返回一个空字符串或适当的东西。

绑定完成后,有时会使用空值进行初始化。您可能可以放心地忽略它。所以编辑你的代码是这样的:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
        if (value == null)
          return String.Empty;

        var transporttModeType = (TransportModeTypeEnum) value;
        string imagePath = String.Empty;   
...

此外,Jason 在 cmets 中指出,您似乎错过了您在此处绑定的路径:

<Image Source="{Binding YourProperty, Converter={StaticResource ListToImageConverter}}" Aspect="AspectFit"/>

注意我是如何添加YourProperty 的,它似乎不见了。当然,将其替换为对象中实际属性的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2010-11-23
    • 2013-10-17
    • 1970-01-01
    • 2016-10-05
    相关资源
    最近更新 更多