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>
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>
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 badges1 Answer
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
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: 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(); }
}
版权声明:本文标题:maui - How do I make a line break in a display alert made with CommunityToolkit ShowPopup()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744535795a2611307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论