【发布时间】:2016-11-23 08:45:40
【问题描述】:
我正在尝试在 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;
}
}
}
【问题讨论】:
标签: label uilabel xamarin.forms multiline