【问题标题】:.NET Maui Label text does not change its value.NET Maui Label 文本不会更改其值
【发布时间】:2022-10-20 18:26:10
【问题描述】:

自从我在 Xamarin 中编写应用程序以来,我一直在苦苦挣扎。问题是当我想在 ContentPage 的 Main 方法之外更改标签的文本值时,它不会在用户界面上更新。

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;
}

}

这是我的 .NET Maui C# 程序,它基本上创建了两个 Tcp 侦听器,它们侦听两个端口——一个用于发送,一个用于接收(由于项目的第二部分,位于两个不同的端口上很重要)。当从第二个端口(接收端口)接收到数据时,会引发一个 DataReceived2 方法,它会获取数据并使用它更改标签文本值。当我调试程序时,我看到标签的值改变了它应该是什么,但它在 ContentPage 上没有改变。我也尝试过数据竞价,但结果是一样的。

    <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>

这是我的 xaml 代码。有人可以帮助解决这个问题。

【问题讨论】:

  • 从未使用过 MAUI,但我认为您必须将调用分派给 UI 线程。如果我搜索正确,您必须拨打Application.Current.MainPage.Dispatcher.Dispatch(() =&gt; label.Text = Data) 之类的电话。
  • 它确实解决了问题。 @Oliver 非常感谢!请将其作为解决方案发布。
  • 只需删除问题。 SO上已经有几十个这样的问题和答案。

标签: c# .net xamarin xamarin.forms maui


【解决方案1】:

谢谢@Oliver,为我工作: 在 MAUI 中显示 webClient.DownloadAsync() 中的进度标签文本,如下所示:

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

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

【讨论】:

    猜你喜欢
    • 2023-01-17
    • 1970-01-01
    • 2020-11-12
    • 2012-03-25
    • 1970-01-01
    • 2023-01-14
    • 2023-01-24
    • 1970-01-01
    • 2022-11-15
    相关资源
    最近更新 更多