【问题标题】:How to write Iconverter for multiple converts如何为多个转换编写 Iconverter
【发布时间】:2020-11-11 15:03:09
【问题描述】:

下面是我用来在绑定到列表视图之前转换值的代码。但是这里只有前 2 个转换工作,convert3 和 convert4 的结果没有显示。请帮助我

 <ContentPage.Resources>
        <local:Class1 x:Key="_converter"/>
    </ContentPage.Resources>

    <ContentPage.Content>
        <ListView x:Name="Models">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label  Text="{Binding from_time,Converter={StaticResource _converter}}"/>
                            <Label  Text="{Binding to_time,Converter={StaticResource _converter}}"/>
                            <Label Text="{Binding from_time_tuesday,Converter={StaticResource _converter}}" TextColor="Brown"/>
                            <Label Text="{Binding to_time_tuesday,Converter={StaticResource _converter}}" TextColor="Brown"/>

                        </StackLayout>
                        
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>



public class Class1: IValueConverter
    {
        public  object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var str = (string)value; // value is the binding data
            if (str== "00:00:00.0000000")
                return "";
            return value;
        }
       
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

【问题讨论】:

  • 由于每个转换中的逻辑相似,您可以在同一个转换中处理它们。 class1 中的格式是非法的。
  • @LucasZhang-MSFT 先生,“已经更改了 class1 代码,但标签 3 和 4 的值也没有显示出来。请帮助我,谢谢
  • 你可以分享一个样本,这样我就可以直接在我这边测试了。
  • @LucasZhang-MSFT,先生,我已经更新了代码,请帮助我。谢谢

标签: c# xamarin cross-platform


【解决方案1】:

由于它适用于第一个和第二个标签,我认为问题是由布局引起的。在您的情况下,StackLayout 在运行时不会影响其子元素的大小。

您可以使用网格

<Grid>
        <Grid.RowDefinitions>

            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />

        </Grid.RowDefinitions>

        <Label Grid.Row="0" HeightRequest="30"  Text="{Binding from_time,Converter={StaticResource _converter}}"/>
        <Label Grid.Row="1" HeightRequest="30"  Text="{Binding to_time,Converter={StaticResource _converter}}"/>
        <Label Grid.Row="2" HeightRequest="30" Text="{Binding from_time_tuesday,Converter={StaticResource _converter}}" TextColor="Brown"/>
        <Label Grid.Row="3" HeightRequest="30" Text="{Binding to_time_tuesday,Converter={StaticResource _converter}}" TextColor="Brown"/>

</Grid>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-31
    • 2014-04-09
    • 1970-01-01
    • 2018-07-15
    • 2011-04-06
    • 1970-01-01
    • 2017-12-30
    • 2019-10-09
    相关资源
    最近更新 更多