Search code examples
loggingtextmaui

.net MAUI log window with multiple text output


I'm looking for a MAUI control that functions similar WinForms rich text box. My use case is desktop application used for test automation, that has a "log" output window. The log shows MQTT packet traces and communications between the app and an external hardware device in real-time.

I've attempted using the editor control, but every time I update the text, it overwrites the previous line. In fact, I can only write one line of text, instead of it appending and displaying on the next line.

Any help would be much appreciated. PS: I'm using MAUI, for cross platform support - I need to use this app on a mac with Appium to control an app on the phone.

sample Code snippet:

Editor log = new Editor { Placeholder = "Comms log", HeightRequest = 250 };

//On a button click to connect to the MQTT broker, print some log messages
   private void OnConnectClicked(object sender, EventArgs e)
   {
       log.Text = MqttUrlConnectBtn.Text + " " + "Url: " + mqttUrl + "\n";
       log.Text = "Connecting to MQTT broker..." + "\n";
   }

Solution

  • As Jason said, you may append text using +=, which works fine. And if you want the Editor to scroll to the last string, you may try set the CursorPosition Property of the Editor, e.g.

    private void OnConnectClicked(object sender, EventArgs e)
    {
        log.Text += MqttUrlConnectBtn.Text + " " + "Url: " + mqttUrl + "\n";
        log.Text += "Connecting to MQTT broker..." + "\n";
    
        log.CursorPosition = log.Text.Length - 1;
        log.Focus();
    }
    

    For more info, you may refer to Editor.