Labe text capacity behaviour issue

Haviv Elbsz 806 Reputation points

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

A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
1,424 questions
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.
8,198 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 16,621 Reputation points Microsoft Vendor

    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();  
       var s2 = "test";  
       test_label.Text = stringBuilder.ToString();  


    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.