String.Format没有在Xamarin.Forms中提供正确的字符串 [英] String.Format is not giving the correct string in Xamarin.Forms

查看:80
本文介绍了String.Format没有在Xamarin.Forms中提供正确的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以标签中的格式显示文本,但是显示的值不正确.

I want to display text in a format in label, but it's displaying an incorrect value.

它在调试模式下显示正确的值.但是它在屏幕上显示错误.理想情况下,屏幕应将总计和小计显示为图像一.

It's showing the correct value in debug mode. But its displayed wrong on the screen. Ideally, the screen should display total and subtotal as image one.

用于格式化字符串的代码

string paymentFormat = "{0,-25} {1,8}\n";
string paymentMode = "Total"; // Or Subtotal
string paymentAmount = "604.00";

string test = string.Format(paymentFormat, paymentMode, paymentAmount);

更新

public class AlertPopupViewItem : ContentView
{
    Label HeaderLabel,MessageLabel;
    public Button OKButton, CancelButton;
    AbsoluteLayout _overlay;
    LoggerService logservice;
    public bool ButtonValue = false;
    StackLayout CancelStackLayout, OKStackLayout;
    string PageSource = string.Empty;

    public AlertPopupViewItem()
    {
        logservice = new LoggerService();
        logservice.WriteData(Constants.DEBUG_LOGGING, "Alert Message Popup ctor.. Start");

        _overlay = new AbsoluteLayout
        {
            BackgroundColor = Color.Black.MultiplyAlpha(0.5),
            HorizontalOptions = LayoutOptions.Fill,
            VerticalOptions = LayoutOptions.Fill,
        };

        Grid mainGrid = new Grid
        {
            HeightRequest = 40,
            BackgroundColor = Color.White,
            Padding = 20,
            RowDefinitions =
            {

                new RowDefinition { Height = new GridLength(15, GridUnitType.Star) },//0 Title
                new RowDefinition { Height = new GridLength(3, GridUnitType.Star) },//1 Line
                new RowDefinition { Height = new GridLength(80, GridUnitType.Star) },//2 Message
                new RowDefinition { Height = new GridLength(12, GridUnitType.Star) },//3 OK-Cancel
            }
        };

        HeaderLabel = new Label
        {
            FontAttributes = FontAttributes.Bold,
            FontSize = 22,
            TextColor = Color.Black,
            HorizontalTextAlignment= TextAlignment.Center,
            VerticalTextAlignment = TextAlignment.Center,
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            VerticalOptions =LayoutOptions.CenterAndExpand
        };


        BoxView divider = new BoxView
        {
            HeightRequest = 1,
            Color = Color.Gray,
            VerticalOptions = LayoutOptions.End,
        };



        MessageLabel = new Label
        {
            FontAttributes = FontAttributes.None,
            FontSize = 13,
            HorizontalTextAlignment = TextAlignment.Start,
            VerticalTextAlignment = TextAlignment.Center,
            HorizontalOptions = LayoutOptions.StartAndExpand,
            TextColor = Color.Black
        };

        ScrollView scroll = new ScrollView()
        {
            Orientation = ScrollOrientation.Vertical
        };

        scroll.Content = MessageLabel;

        Grid ButtonGrid = new Grid
        {

            HeightRequest = 35,
            ColumnDefinitions =
            {
                new ColumnDefinition {Width=new GridLength(58,GridUnitType.Star) },
                new ColumnDefinition {Width=new GridLength(20,GridUnitType.Star) },
                new ColumnDefinition {Width=new GridLength(2,GridUnitType.Star) },
                new ColumnDefinition {Width=new GridLength(20,GridUnitType.Star) }
            }
        };


        CancelStackLayout = new StackLayout
        {
            Padding = new Thickness(-6, -6, -6, -6),
            //VerticalOptions = LayoutOptions.Center,
            BackgroundColor = Color.FromHex("#ff9500")
        };

        CancelButton = new Button
        {
            TextColor = Color.White,
            FontSize = 15,
            BorderRadius = 0,
            Text = Localizer.Localize("CancelSmall"),
            BackgroundColor = Color.FromHex("#01458e"),
            HorizontalOptions =LayoutOptions.FillAndExpand,
            VerticalOptions=LayoutOptions.FillAndExpand,
            BorderColor = Color.Transparent
        };


        CancelButton.Clicked += CancelButtonClicked;

        CancelStackLayout.Children.Add(CancelButton);
        ButtonGrid.Children.Add(CancelStackLayout, 1, 0);

        OKStackLayout = new StackLayout
        {
            Padding = new Thickness(-6, -6, -6, -6),
            BackgroundColor = Color.FromHex("#ff9500")
        };


        OKButton = new Button
        {
            TextColor = Color.White,
            FontSize = 15,
            BorderRadius = 0,
            Text = Localizer.Localize("OK"),
            BackgroundColor = Color.FromHex("#01458e"),
            HorizontalOptions = LayoutOptions.FillAndExpand,
            VerticalOptions = LayoutOptions.FillAndExpand,
            BorderColor = Color.Transparent
        };
        OKButton.Clicked += OKButtonClicked;

        OKStackLayout.Children.Add(OKButton);
        ButtonGrid.Children.Add(OKStackLayout, 3, 0);


        mainGrid.Children.Add(HeaderLabel, 0, 0);

        mainGrid.Children.Add(divider, 0, 1);

        mainGrid.Children.Add(scroll, 0, 2);

        mainGrid.Children.Add(ButtonGrid, 0, 3);



        AbsoluteLayout.SetLayoutFlags(mainGrid, AbsoluteLayoutFlags.All);

        AbsoluteLayout.SetLayoutBounds(mainGrid, Findlayoutbounds(new Rectangle(0.20, 0.25, 0.5, 0.50)));


        _overlay.Children.Add(mainGrid);


        Content = _overlay;
        logservice.WriteData(Constants.DEBUG_LOGGING, "Alert Message Popup ctor.. End");
    }

   // ThreadHandle thread = new ThreadHandle();
    private void CancelButtonClicked(object sender, EventArgs e)
    {
        ButtonValue = false;
        this.IsVisible = false;
       // thread.WorkMethod();
    }

    private void OKButtonClicked(object sender, EventArgs e)
    {

        ButtonValue = true;
        if (PageSource == "StarterPage") ;
        //MessagingCenter.Send(this, "ModifyValBooleanForAlert");
        this.IsVisible = false;

       // thread.WorkMethod();
    }

    Rectangle Findlayoutbounds(Rectangle fractionalRect)
    {
        if (fractionalRect.Width - 1 == 0)
            fractionalRect.Width = 0.99;
        if (fractionalRect.Height - 1 == 0)
            fractionalRect.Height = 0.99;
        Rectangle layoutbounds = new Rectangle
        {
            X = fractionalRect.X / (1 - fractionalRect.Width),
            Y = fractionalRect.Y / (1 - fractionalRect.Height),
            Width = fractionalRect.Width,
            Height = fractionalRect.Height
        };

        return layoutbounds;
    }

    public void DisplayAlertPopup(string alertBoxTitle, string alertBoxContent,bool CancelDisplay)
    {
        HeaderLabel.IsVisible = false;
        CancelStackLayout.IsVisible = CancelDisplay;
        CancelButton.IsVisible = CancelDisplay;
        HeaderLabel.Text = alertBoxTitle;
        MessageLabel.Text = alertBoxContent;
        OKButton.Text = Localizer.Localize("OK");
        CancelButton.Text = Localizer.Localize("CancelSmall");
        HeaderLabel.IsVisible = true;
    }

    public void DisplayAlertPopup(string alertBoxTitle, string alertBoxContent, string ButtonText)
    {
        CancelStackLayout.IsVisible = false;
        CancelButton.IsVisible = false;
        HeaderLabel.Text = alertBoxTitle;
        MessageLabel.Text = alertBoxContent;
        OKButton.Text = ButtonText;
    }

    public void DisplayAlertConditionalPopup(string alertBoxTitle, string alertBoxContent, bool CancelDisplay)
    {
        CancelStackLayout.IsVisible = CancelDisplay;
        CancelButton.IsVisible = CancelDisplay;
        HeaderLabel.Text = alertBoxTitle;
        MessageLabel.Text = alertBoxContent;
        this.IsVisible = true;

    }

    public void SetButtonText(string OKText, string CancelText)
    {
        if (OKText != null)
            OKButton.Text = OKText;
        if (CancelText != null)
            CancelButton.Text = CancelText;
    }

}

我从其他类获取格式化的字符串,然后调用DisplayAlertPopup方法. MessageLabel是为其设置此值的标签.

I get the formatted string from some other class and call the DisplayAlertPopup method. MessageLabel is the label for which I am setting this value.

Update2:

正如答案中所建议的那样,我尝试了以下代码来设置Android的字体.但是它也不会以所需的格式显示文本.

As suggested in answer, I have tried the below code to set the font for Android. But its also not displying the text in the required format.

MessageLabel = new Label
    {
        FontAttributes = FontAttributes.None,
        FontSize = 13,
        HorizontalTextAlignment = TextAlignment.Start,
        VerticalTextAlignment = TextAlignment.Center,
        HorizontalOptions = LayoutOptions.StartAndExpand,
        TextColor = Color.Black,
        FontFamily = "Droid Sans Mono"
    };

推荐答案

您将需要使用固定宽度的字体.不幸的是,所有平台都没有内置任何组件,但是每个平台都有其自己的:

You will need to use a fixed-width font. There is unfortunately none that is built-in across all platforms, but each platform has its own:

  • iOS-Courier New
  • Android-Droid Sans Mono
  • UWP-Consolas

如果没有适合您的内置字体,或者您希望在所有平台上都具有相同的体验,则还可以在Xamarin.Forms中使用自定义字体.这要求您在 Google字体之类的服务上找到所需的固定宽度字体.然后,您可以按照 Xamarin帮助上的教程进行操作. ,介绍了如何在每个平台中包含TTF文件以及如何从XAML中使用它.

If no built-in font suits you or you want to have the same experience on all platforms, you can also use custom fonts in Xamarin.Forms. This requires you to find a fixed-width font you like on a service like Google Fonts. Then you can follow the tutorial here on Xamarin Help, that describes how to include the TTF file in each platform and use it from XAML.

简短的摘要是:

  1. 通过适当的构建操作(UWP-内容,iOS-捆绑资源,Android-Android资产)将字体添加到每个平台项目中
  2. 使用OnPlatform语法设置字体(理想情况下,创建静态资源以便可以重复使用):
  1. Add the font to each platform project with appropriate build action (UWP - Content, iOS - Bundle Resource, Android - Android Asset)
  2. Use the OnPlatform syntax to set the font (ideally creating a static resource so that it can be reused):

资源:

<OnPlatform x:TypeArguments="x:String" x:Key="MyFontFamily">
    <On Platform="Android" Value="MyFont.ttf#Open Sans" />
    <On Platform="UWP" Value="/Assets/MyFont.ttf#Open Sans" />
    <On Platform="iOS" Value="MyFont" />
</OnPlatform>

并像这样使用:

<Label FontFamily="{StaticResource MyFontFamily}" />

这篇关于String.Format没有在Xamarin.Forms中提供正确的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆