简体   繁体   English

在 Xamarin.Forms 中使用 xaml 以大写形式显示标签文本

[英]Display label text in uppercase using xaml in Xamarin.Forms

I have an username label and need to view this as uppercase but this should only relate to the UI.我有一个用户名标签,需要将其视为大写,但这应该只与 UI 相关。 The data (string) should be saved in the db as actual case whatever it is.数据(字符串)应该作为实际情况保存在数据库中,无论它是什么。 Could anyone tell me if there is anyway to convert it to uppercase without doing so through the code behind?谁能告诉我是否可以通过背后的代码将其转换为大写?

You can use Label.TextTransform with TextTransform.Uppercase .您可以将Label.TextTransformTextTransform.Uppercase Label.TextTransform使用。

XAML XAML

<Label TextTransform="Uppercase" />

C# C#

var label = new Label
{
    TextTransform = TextTransform.Uppercase
};

As you're aware you can do this from the code behind as such:如您所知,您可以从后面的代码中执行此操作:

string data = "my data";
UILabel myLabel = new UILabel();
myLabel.Text = data.ToUpper();

So bearing in mind that you don't want to do it this way you would need to derive from UILabel and create your own, then simply add the ToUpper() onto the end of the get;set;因此请记住,您不想这样做,您需要从 UILabel 派生并创建自己的,然后只需将 ToUpper() 添加到 get;set; 的末尾; values of the Text property. Text 属性的值。

using CoreGraphics;
using System;
using UIKit;

namespace MyApp.Controls
{
    partial class Control_UpperLabel : UILabel
    {
        public Control_UpperLabel IntPtr handle) : base(handle)
        {
               //
        }

        public Control_UpperLabel()
        {
               //
        }

        public override void Draw(CGRect rect)
        {
            base.Draw(rect);
        }

        public override string Text { get => base.Text.ToUpper(); set => base.Text = value.ToUpper(); }    
   }
}

EDIT: As per comments below, here is an alternative solution for Xamarin.Forms编辑:根据下面的评论,这里是 Xamarin.Forms 的替代解决方案

This uses a value converter as part of a binding solution.这使用值转换器作为绑定解决方案的一部分。 It's also been slightly amended to use the suggestion by clint in the comments below.它也被稍微修改以在下面的评论中使用 clint 的建议。 Thanks.谢谢。

public class StringCaseConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        switch ((parameter as string).ToUpper()[0]) 
        { 
        case 'U': 
            return ((string)value).ToUpper(); 
        case 'L': 
            return ((string)value).ToLower(); 
        default: 
            return ((string)value);
        };
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

It would be used in the XAML as such:它将在 XAML 中使用,如下所示:

Text="{Binding Text, Converter={StaticResource caseConverter}, ConverterParameter=u}}"

Or you can use Bindable property then format the text on the getter : eg:或者您可以使用 Bindable 属性然后格式化 getter 上的文本:例如:

  public static readonly BindableProperty ItemLabelProperty = 
  BindableProperty.Create(nameof(ItemLabel), typeof(string), 
  typeof(DetailsLineItemControl), default(string), BindingMode.OneWay);
    public string ItemLabel
    {
        get
        {

            var value = (string)GetValue(ItemLabelProperty);
            return !string.IsNullOrEmpty(value) ? value.ToUpper() : value;
        }
        set
        {
            SetValue(ItemLabelProperty, value);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM