String.Format没有在Xamarin.Forms中提供正确的字符串

时间:2018-11-12 12:48:07

标签: c# xamarin xamarin.forms

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

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

String value in debug mode

enter image description here

用于格式化字符串的代码

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是我为其设置此值的标签。

Update2:

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

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

1 个答案:

答案 0 :(得分:1)

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

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

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

简短的摘要是:

  1. 通过适当的构建操作(UWP-内容,iOS-捆绑资源,Android-Android资产)将字体添加到每个平台项目中
  2. 使用OnPlatform语法设置字体(理想情况下,创建静态资源以便可以重复使用):

资源:

<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}" />
相关问题