Labe text capacity behaviour issue

Haviv Elbsz 2,071 Reputation points
2022-11-15T16:57:23.07+00:00

Hi all

my maui android app when running
write text to a label.

currently I use s1+= s2 and then
label.text = s1

when the s1 growth to some huge size
the app freeze.

Is there any way to add text to a label
something like textbox.append of winforms

mainly every cycle add small chunk to
the label text.

Thank you very much

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,578 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,996 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 43,926 Reputation points Microsoft Vendor
    2022-11-16T02:40:59.477+00:00

    You need to use StringBuilder to solve the efficiency problem of strings.

    The String object is immutable. Every time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly. The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop.

    For your issue, you could use a StringBuilder object to append your string.

       StringBuilder stringBuilder = new StringBuilder();  
       stringBuilder.Append(test_label.Text);  
       var s2 = "test";  
       stringBuilder.Append(s2);  
       test_label.Text = stringBuilder.ToString();  
    

    Update:

    This is because when a string is too long, it causes significant memory and computational overhead.

    The best solution to this problem is to segment or paginate the content after it exceeds a certain range or load its fragments as you scroll.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.