【问题标题】:String.Format is not giving the correct string in Xamarin.FormsString.Format 未在 Xamarin.Forms 中提供正确的字符串
【发布时间】:2018-11-12 12:48:06
【问题描述】:

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

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

格式化字符串的代码

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

更新2:

正如回答中所建议的,我已尝试使用以下代码为 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"
    };

【问题讨论】:

  • 可以分享一下xaml页面吗?
  • 是在谈论两个金额不一致的事实吗?
  • @Kzrystof 是的,两个金额都不一致。
  • @PaulKaram 我已经更新了问题
  • @viveknuna 你不能把字体改成类似 Consolas 的字体吗?

标签: c# xamarin xamarin.forms


【解决方案1】:

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

  • iOS - 新快递
  • Android - Droid Sans Mono
  • UWP - 康索拉斯

如果没有适合您的内置字体,或者您想在所有平台上获得相同的体验,您还可以在 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}" />

【讨论】:

  • 我无法解决我的问题,您能帮忙吗?
  • 哪个问题?我发布的解决方案不起作用?
  • 我尝试了适用于 Android 的 Droid Sans Mono。但它显示相同的旧错误格式
  • 我需要在代码后面(C#)而不是 xaml 中设置字体
  • 字体是否真的改变了(输出看起来不同吗?)。也许更改不成功,所以您仍然看到默认字体
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
  • 2020-07-25
  • 1970-01-01
相关资源
最近更新 更多