.NET Maui Label text does not change its value

I am having issue that I've been struggling since I was programming apps in Xamarin. The problem is when I want change a label's text value outside from the ContentPage's Main method it does not update on the user interface.

public partial class MainPage : ContentPage
{
int command = 0;
SimpleTcpServer server1 = null;
SimpleTcpServer server2 = null;
System.Timers.Timer timer = null;
string iPPort = null;
public string Data { get; set; } = "getting data";

public MainPage()
{
    InitializeComponent();
    NetworkAccess accessType = Connectivity.Current.NetworkAccess;
    if (accessType == NetworkAccess.Internet)
    {
        server1 = new SimpleTcpServer("10.0.0.9:10000");
        server2 = new SimpleTcpServer("10.0.0.9:11000");
        timer = new System.Timers.Timer(150);
        timer.Elapsed += Tick;
        timer.AutoReset = true;
        server1.Events.ClientConnected += ClientConnected;
        server1.Events.ClientDisconnected += ClientDisconnected;
        server2.Events.ClientConnected += ClientConnected2;
        server2.Events.ClientDisconnected += ClientDisconnected2;
        server2.Events.DataReceived += DataReceived2;
        label.Text = Data;
        server1.Start();
        server2.Start();
        
    }
}

public void DataReceived2(object sender, SuperSimpleTcp.DataReceivedEventArgs e)
{
    ArraySegment<byte> buffer = e.Data;
    Data = Encoding.Default.GetString(buffer);
    label.Text = Data;
}

private void ClientDisconnected2(object sender, ConnectionEventArgs e)
{
    throw new NotImplementedException();
}

private void ClientConnected2(object sender, ConnectionEventArgs e)
{
}

private void Tick(object sender, ElapsedEventArgs e)
{
    server1.Send(iPPort, command.ToString());
}




private void ClientDisconnected(object sender, ConnectionEventArgs e)
{
    throw new NotImplementedException();
}

private void ClientConnected(object sender, ConnectionEventArgs e)
{
    iPPort = e.IpPort;
    timer.Start();
}

private void Forward(object sender, EventArgs e)
{
    command = 1;
}

private void Backward(object sender, EventArgs e)
{
    command = 2;
}

private void Left(object sender, EventArgs e)
{
    command = 3;
}

private void Right(object sender, EventArgs e)
{
    command = 4;
}

private void Released(object sender, EventArgs e)
{
    command = 0;
}

}

This is my .NET Maui C# program which basically creates two Tcp listeners, which listen to two ports-one for sending and one for receiving (It is important to be on two different ports because of the second part of the project). When a data is received from the second port (the receiving port) a method DataReceived2 is raised and it gets the data and changes the label text value with it. When I debug the program I see that the value of label is changed with what it should be but it does not change on the ContentPage. I tried Data biding too, but the result is the same.

    <StackLayout>
    <Grid x:Name="grid">
        <StackLayout VerticalOptions="CenterAndExpand" Margin="10,290,0,0">
            <StackLayout Orientation="Horizontal">
                <StackLayout Margin="0,120,0,60">
                    <Button VerticalOptions="CenterAndExpand" BackgroundColor="Green"  Pressed="Forward" Released="Released" CornerRadius="50" Margin="0,0,0,-20" HeightRequest="100" WidthRequest="100"></Button>
                    <Button HeightRequest="100" Pressed="Backward" BackgroundColor="Green" Released="Released" WidthRequest="100" CornerRadius="50"></Button>
                </StackLayout>
                <StackLayout Margin="20,200,0,120" Orientation="Horizontal">
                    <Button CornerRadius="100" Pressed="Left" BackgroundColor="Green" Released="Released" HeightRequest="100" WidthRequest="100"></Button>
                    <Button HeightRequest="100" Pressed="Right" BackgroundColor="Green" Released="Released" Margin="10,0,0,0" WidthRequest="100" CornerRadius="60"></Button>
                </StackLayout>
            </StackLayout>
        </StackLayout>
        <StackLayout x:Name="stack">
            <Label x:Name="label" Text="" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
        </StackLayout>
    </Grid>

</StackLayout>

And this is my xaml code.Can somebody help with this problem.

Upvotes: 3

Views: 5909

Answers (1)

Michael Haumann
Michael Haumann

Reputation: 51

Thanks @Oliver, working for me: in MAUI showing a progress label-text in webClient.DownloadAsync() like this:

XAML

<Label x:Name="progressBarText"
           Text="..." />

C#

private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
  try
  {
    progressBar.Progress = (double)(e.ProgressPercentage / 100.0);
    Application.Current.MainPage.Dispatcher.Dispatch(() => progressBarText.Text = String.Format( "Downloading {0}%", e.ProgressPercentage ));
  }
  catch (Exception error)
  {
    throw error;
  }
}

Upvotes: 4

Related Questions