我正在尝试使标签在MasterDetail页的母版页内自动换行。我认为包装标签是默认行为,但为了以防万一,我甚至制作了一个自定义渲染器。我已经确认了自定义渲染器可以工作(通过调试和向标签添加背景),所以我假设我遗漏了一些简单的东西。谁能看一眼我的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;            
        }
    }
}

发布于 2016-11-23 20:01:44
嗯,不知道为什么,但是您正确地将行设置为0,并使用自动换行功能( Multiple lines of text in UILabel )
我最近还用一个自定义渲染器( http://depblog.weblogs.us/2016/06/27/xamarin-forms-multi-line-label-custom-renderer-gotcha/ )实现了这一点,并使用一个数字来表示我想要的线条的确切数量。它被设置到Lines属性中,然后包装就可以工作了。
https://stackoverflow.com/questions/40759635
复制相似问题