为什么我的Xamarin.Forms标签不能包装文本?

时间:2016-11-23 08:45:40

标签: label uilabel xamarin.forms multiline

我正在尝试在MasterDetail页面的Master页面内进行标签包装。我认为这是包装标签的默认行为,但以防我甚至制作了自定义渲染器。我已经确认自定义渲染器工作(通过调试并向标签添加背景),所以我假设有一些我很遗憾的东西。任何人都可以看看我的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"
             x:Class="MyApp.Pages.NavigationDrawer.NavigationMenuMasterPage"
             xmlns:statics="clr-namespace:MyApp.Statics;assembly=MyApp"
             xmlns:multi="clr-namespace:MyApp.Pages.NavigationDrawer;assembly=MyApp"
             Title="Navigation drawer"
             Icon="hamburger.png"
             BackgroundColor="White">
    <ContentPage.Content>
        <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="Center" Margin="0, 25, 0, 0">
            <StackLayout Orientation="Horizontal" HorizontalOptions="Center">
                <Image x:Name="connectionStatusImage" />
                <Label x:Name="connectionStatusText"  Margin="16, 0, 16, 0" YAlign="Center" FontSize="Medium"/>
            </StackLayout>
            <Label x:Name="changeConnectionStatusButton" TextColor="{x:Static statics:Palette.Orange}" XAlign="Center" FontAttributes="Bold" >
                <Label.GestureRecognizers>
                    <TapGestureRecognizer Tapped="ChangeConnectionStatusClicked" NumberOfTapsRequired="1" />
                </Label.GestureRecognizers>
            </Label>
            <StackLayout x:Name="SyncStatusStack" Orientation="Horizontal" Margin="0, 16, 16, 0" VerticalOptions="Center" >
                <multi:MultiLineLabel x:Name="syncStatusText" MinimumWidthRequest="0" Margin="16, 0, 0, 0" Text="I want to write a really long string to see if it wraps" />
                <Label x:Name="syncButton" MinimumWidthRequest="40" Text="SYNC" FontAttributes="Bold" YAlign="Center" HorizontalOptions="End" >
                    <Label.GestureRecognizers>
                        <TapGestureRecognizer x:Name="SyncGestureRecognizer" NumberOfTapsRequired="1" />
                    </Label.GestureRecognizers>
                </Label>
            </StackLayout>
            <StackLayout x:Name="SyncStack" Orientation="Vertical" HorizontalOptions="Center" Margin="0, 16, 0, 0">
                <ActivityIndicator x:Name="syncingIndicator"  Margin="16, 0, 32, 0"/>
                <Label x:Name="syncingText" />
            </StackLayout>
            <ListView x:Name="menuListView" VerticalOptions="FillAndExpand" SeparatorVisibility="None" Margin="0, 16, 0, 0">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Text="{Binding Title}" TextColor="{x:Static statics:Palette.PrimaryText}" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

我的(很可能是不必要的)自定义标签渲染器:

using MyApp.iOS.Renderers;
using MyApp.Pages.NavigationDrawer;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(MultiLineLabel), typeof(MultiLineLabeliOSRenderer))]
namespace MyApp.iOS.Renderers
{
    public class MultiLineLabeliOSRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            base.OnElementChanged(e);

            if (Control == null) return;

            Control.LineBreakMode = UIKit.UILineBreakMode.WordWrap;
            Control.Lines = 0;            
        }
    }
}

screenshot of the issue

1 个答案:

答案 0 :(得分:0)

嗯,不知道为什么,但你是正确的设置行为0并使用自动换行应该做的伎俩(Multiple lines of text in UILabel

我最近也使用自定义渲染器(http://depblog.weblogs.us/2016/06/27/xamarin-forms-multi-line-label-custom-renderer-gotcha/)实现了此功能,并使用数字来指示我想要的确切行数。 这被设置为Lines属性,然后包装工作。