1

我怎樣才能讓標籤文本下劃線使用Xamarin窗體WinPhoneXamarin形式WinPhone - 如何使標籤文本下劃線WinPhone?

+1

創建'Label'渲染器,並使用原生** ** WinPhone方法來創建帶下劃線的文本。 –

+0

你見過[this](https://github.com/XLabs/Xamarin-Forms-Labs/wiki/ExtendedLabel)嗎? –

+0

是@EgorGromadskiy但我不能找到** ** ExtendedLabelRenderer爲WinPhone在這[GitHub庫(https://github.com/XLabs/Xamarin-Forms-Labs/wiki/ExtendedLabel#platform-code) –

回答

0

嘗試使用以下xaml;

<StackLayout Orientation="Vertical"> 
    <Label Text="SomeText"/> 
    <BoxView HeightRequest="1" HorizontalOptions="FillAndExpand" BackgroundColor="Black"/> 
</StackLayout> 

這應該爲所有3個平臺。 :)

+1

這只是繪製一個高度爲1的盒子視圖,與下劃線無關。當標籤文本較短或者指定對齊方式以及進入兩行時,它會中斷。 –

+0

我對文本屬性長度上的數據觸發器具有相同的實現,這爲我提供了谷歌材質設計條目。直到現在我都很滿意。 :) –

+0

正如@RohitVipinMathews所說,這實際上只是一個拙劣的解決方法,可能會有問題。 – jbyrd

0

我認爲您需要爲此創建一個自定義視圖,使其具有一個標籤和一個帶有小標籤heightRequest的BoxView以充當一條線。

+0

是的,我可以找不到另一個解決方案 –

1

你有最好的創建一個新的控件在你的PCL /共享項目繼承自標籤。

public class Exlabel : Label 
{ 
} 

在Windows Phone項目爲它創建一個Custom Renderer如下,並使用TextBlock.TextDecorations屬性設置下劃線。 標籤在Windows中呈現爲TextBlock

樣品(未經測試):

[assembly: ExportRenderer(typeof(Exlabel), typeof(ExlabelRenderer))] 
namespace CustomRenderer.WinPhone81 
{ 
    public class ExlabelRenderer : LabelRenderer 
    { 
     protected override void OnElementChanged(ElementChangedEventArgs<Label> e) 
     { 
      base.OnElementChanged(e); 

      if (Control != null) 
      { 
       Control.TextDecorations = TextDecorations.UnderLine; 
      } 
     } 
    } 
} 

如果您使用的是Windows手機看看這個樣本 - How to format texts of TextBlock using xaml in Windows Phone

對於WinRT,你可以使用這個 - TextBlock underline in WinRT

在SilverLight WinPhone(舊的和不太支持的模板)中,您還可以使用保證金來實現您所需的功能,類似於How to make an underlined input text field in Windows Phone?

+0

我找不到此屬性!我有這樣的錯誤: 錯誤\t CS1061 \t「TextBlock的」不包含「TextDecorations」的定義,並沒有擴展方法「TextDecorations」接受式「的TextBlock」的第一個參數可以找到(是否缺少using指令或程序組裝參考?) –

+0

您在Windows phone項目中獲得Control的類型是什麼? –

+0

'TextBlock',你可以在我的錯誤信息中看到這個 –

0

在WinPhone項目中創建一個標籤渲染:

using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Documents; 
[assembly: ExportRenderer(typeof(ExtendedLabel), typeof(ExtendedLabelRenderer))] 

namespace SampleProject.WinPhone 
{ 
public class ExtendedLabelRenderer: LabelRenderer 
{ 
    ExtendedLabel element; 
    TextBlock control; 
    protected override void OnElementChanged(ElementChangedEventArgs<Label> e) 
    { 
     base.OnElementChanged(e); 

     if((ExtendedLabel)Element == null || Control == null) 
      return; 

     element = (ExtendedLabel)Element; 
     control = Control; 
     UnderlineText(); 
    } 
    void UnderlineText() 
    { 
     control.Text = string.Empty; 
     Underline ul = new Underline(); 
     Run run = new Run(); 
     run.Text = element.Text; 
     ul.Inlines.Add(run); 
     control.Inlines.Add(ul); 
    } 
    } 
}