admin管理员组

文章数量:1384238

I'm trying to show a message with a line break using CommunityToolkit.ShowPopup.

Consider

Application.Current.MainPage.ShowPopup(new ErrorPopup(message, buttonMessage, onButtonClicked));

Where message comes from a string resource

AppResources.ResourceManager.GetString("Error_MyMessage")

I would like to show a line break. In my string resource I have used

\n
<br>
&#10;

All of them were displayed as is, and none resulted in an actual line break in the popup.

What's the best way to show a line break in the pop up?

I'm trying to show a message with a line break using CommunityToolkit.ShowPopup.

Consider

Application.Current.MainPage.ShowPopup(new ErrorPopup(message, buttonMessage, onButtonClicked));

Where message comes from a string resource

AppResources.ResourceManager.GetString("Error_MyMessage")

I would like to show a line break. In my string resource I have used

\n
<br>
&#10;

All of them were displayed as is, and none resulted in an actual line break in the popup.

What's the best way to show a line break in the pop up?

Share Improve this question asked Mar 17 at 20:51 AbsolutelyFreeWebAbsolutelyFreeWeb 4194 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

===========Update===========

Are you using a xaml resource file because line breaks are not possible using resx string resource files ?

If you want to add line breaks in the resx file, you need to right click your AppResources.resx file. choose Open with, select the Source code(Text) editor, then find your Error_MyMessage key, add a line break by typing "error" followed by Enter and "Messages" like following format.

  <data name="Error_Message" xml:space="preserve">
<value>error  
this message</value>
</data>

Then when you showing the text with line break between error and this message like this screenshot.

Or concatenating strings like Application.Current.MainPage.ShowPopup(new ErrorPopup((string)AppResources.Error_Message+" \n "+"test", buttonMessage));

I use the Unicode line feed character, which is &#10;

Firstly, I create a .NET MAUI ResouceDictionary file called Strings.xaml in the Resources\Styles folder.

Then I add error message strings like following code.

<ResourceDictionary xmlns="http://schemas.microsoft/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft/winfx/2009/xaml"
             x:Class="MauiApp15.Resources.Styles.Strings">

     <x:String x:Key="Error_MyMessage">Error:&#10;this is message</x:String>
 
</ResourceDictionary>

Next. I add this Strings.xaml reference in the App.xaml

<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft/winfx/2009/xaml"
             xmlns:local="clr-namespace:MauiApp15"
             x:Class="MauiApp15.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
 
  <ResourceDictionary Source="Resources/Styles/Strings.xaml" />


</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

After that, we can get the value of Error message string by App.Current.Resources.TryGetValue. About get the strings from the resources dictionary, you can refer to this document:Access resources by key from code

I test add one parameter for testing in ErrorPopup's constructor.

var hasValue = App.Current.Resources.TryGetValue("Error_MyMessage", out object message);
 
  if (hasValue) {
      Application.Current.MainPage.ShowPopup(new ErrorPopup((string)message));
 
  }

In the end. I use background code to set this error Message to Label

public partial class ErrorPopup : Popup
{

    public ErrorPopup(string message)
    {
        Message = message;
       
        StackLayout sl = new StackLayout();
        sl.Children.Add(new Label() { Text = Message });
        Content = sl;
 
 
    }
 
    private string message;
 
    public string Message
    {
        get { return message; }
        set { message = value;OnPropertyChanged(); }
    }

本文标签: mauiHow do I make a line break in a display alert made with CommunityToolkit ShowPopup()Stack Overflow