2016-11-23 61 views
0

我試圖在MasterDetail頁面的Master頁面內做一個標籤包裝。我認爲這是包裝標籤的默認行爲,但以防萬一我做了自定義渲染器。我已經確認了自定義渲染器的工作原理(通過調試並向標籤添加背景),所以我認爲有些東西很容易丟失。任何人都可以看看我的xaml,並告訴我爲什麼這個標籤不包裝?爲什麼我的Xamarin.Forms標籤不能包裝文本?

<?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

回答

0

嗯不知道爲什麼,但你是正確的設置線爲0,並使用自動換行應該做的訣竅(Multiple lines of text in UILabel

我最近還實現了這個自定義渲染器(http://depblog.weblogs.us/2016/06/27/xamarin-forms-multi-line-label-custom-renderer-gotcha/)並使用了一個數字ind請提供我想要的確切數量的行數。 這被設置到Lines屬性中,然後包裝工作。

+1

我已經做了一些實驗,將標籤放在不同的地方,並在不同的地方將水平的StackLayouts放置在不同的地方(甚至在一個基本的ListView的新頁面上)。看起來水平StackLayout中的標籤不會換行iOS如果在同一頁面上有一個ListView。如果沒有一個ListView,一切都按預期工作。這一切都在Android上正常工作。所以我認爲這一定是一個錯誤。我與Grid有相同的行爲。所以我只是改變了我的佈局,使StackLayout垂直。不完全是我想要的,但它的工作原理:) –

+1

最好的發佈在BugZilla的回購,它將被跟蹤! – Depechie

+0

@JasonToms是對的 - 我只是打這個...多麼浪費時間。如果你想保持你的佈局,你可以修改LineBreakMode。 – thinice

相關問題