简体   繁体   English

Xamarin.Forms Listview有2行标签?

[英]Xamarin.Forms Listview with 2 line label?

I'm converting a MonoTouch.Dialog app to Xamarin.Forms . 我正在将MonoTouch.Dialog应用程序转换为Xamarin.Forms

I have a Cell in a ListView and that has a "Detail" line that should be 2 lines long, and should truncate the tail after that. 我在ListView中有一个Cell,并且有一个“ Detail”行,该行应该长2行,并在此之后截断尾部。

        var lblDetail = new Label
        {
            LineBreakMode = LineBreakMode.TailTruncation,
            Text = "a really long string that should show 2 lines then ..."
        };

How do I set something like, "Lines = 2" 如何设置“线= 2”

I solved this with a custom renderer 我用自定义渲染器解决了这个问题

This is my xaml (in my pcl project) 这是我的xaml(在我的pcl项目中)

<ListView ItemsSource="{Binding Cards}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
          <Grid Padding="10">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="60"/>
              <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Image Source="{Binding Icon}"/>
            <customRenderers:MultiLineLabel Text="{Binding Summary}"
                                            Grid.Column="1"/>
          </Grid>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>

</ListView>

This is my MultiLineLabel class 这是我的MultiLineLabel类别

public class MultiLineLabel : Label
    {
    }

This is the renderer for iOS: 这是iOS的渲染器:

[assembly: ExportRenderer(typeof(MultiLineLabel), typeof(MultiLineLabelRenderer))]
namespace NameSpace.iOS.Renderers
{
    public class MultiLineLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
            {
                Control.LineBreakMode = UILineBreakMode.TailTruncation;
                Control.Lines = 3;
            }
        }
    }
}

the following code working fine for me :) 以下代码对我来说很好:)

 UserNotesListView = new ListView() {
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand,
                HasUnevenRows=true,
                ItemsSource=//upon your bussiess
            };

            DataTemplate dt=new DataTemplate(()=>
                {
                    var LabelText = new Label();
                    LabelText.SetBinding(Label.TextProperty, new Binding("note"));
                    return new ViewCell { View = LabelText };
            });
            UserNotesListView.ItemTemplate = dt;
            Content = new StackLayout
            {
                HorizontalOptions=LayoutOptions.FillAndExpand,
                Children=
                {
                    UserNotesListView
                }
            };

You might want to customize ListView: 您可能要自定义ListView:

    <ListView x:Name="ListMenu" ItemsSource="{Binding _YourItems_}" >
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <ViewCell.View>
              <StackLayout Orientation="Horizontal">
                <Label Text="{Binding _YourText_}" TextColor="Black" />
              </StackLayout>
            </ViewCell.View>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

even if text not fit one line, it will wrap automatically. 即使文本不适合一行,它也会自动换行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM