【问题标题】:Xamarin Forms / XAML: Converter not calledXamarin Forms / XAML:未调用转换器
【发布时间】:2017-01-07 22:45:19
【问题描述】:

有人知道为什么只有第二个isvisible 转换器被调用吗?
如果我更改了顺序,则只会调用新的第二个转换器。
Converter1 是DiaryTypeNahrungsaufnahmeToBoolConverter,converter2 是DiaryTypeAuswirkungToBoolConverter

<ListView>
  <ListView.ItemTemplate>
    <DataTemplate>      
        <ViewCell>
            <RelativeLayout IsVisible="{Binding Type, Converter={StaticResource converter1}}"></RelativeLayout>
            <RelativeLayout IsVisible="{Binding Type, Converter={StaticResource converter2}}"></RelativeLayout>
        </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

转换器代码为:

public class DiaryTypeNahrungsaufnahmeToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            if (value is LibChemotherapie.DiaryType)
            {
                return ((LibChemotherapie.DiaryType)value) == LibChemotherapie.DiaryType.Food;
            }
            return false;
        }
        catch (Exception)
        {
            return false;
        }

    }

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

public class DiaryTypeAuswirkungToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            if (value is LibChemotherapie.DiaryType)
            {
                return ((LibChemotherapie.DiaryType)value) == LibChemotherapie.DiaryType.Effect;
            }
            return false;
        }
        catch (Exception)
        {
            return false;
        }
    }

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

感谢您的帮助。

【问题讨论】:

  • 您的转换器的代码是什么?正如现在所说的那样,它太宽泛了,所以有太多可能的答案。请添加更多详细信息,否则将很难为您提供帮助。
  • @Demitrian 好的添加了转换器代码
  • 您在哪里定义了converter1converter2?还是您只是使用这些名称来避免在代码中为您的问题输入转换器的名称?
  • 我认为问题在于您的 ViewCell 有两个孩子。一个 ViewCell 只有一个 View 属性,所以它只能有一个孩子。多个子项需要包含在一个布局中。
  • @Jason 是的,谢谢,就是这样!现在它正在工作! :)

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

根据 Jason 的提示,它现在可以工作了!我更改了 xaml,以便 ViewCell 仅包含一个视图。在那个视图中,我添加了我的两个具有绑定属性的相对布局。

<ListView>
  <ListView.ItemTemplate>
    <DataTemplate>      
        <ViewCell>
            <RelativeLayout>
                <RelativeLayout IsVisible="{Binding Type, Converter={StaticResource converter1}}"></RelativeLayout>
                <RelativeLayout IsVisible="{Binding Type, Converter={StaticResource converter2}}"></RelativeLayout>
            </RelativeLayout>
        </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 2018-10-22
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多