自从我在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;
}
}
这是我的. NETMaui 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代码。有人能帮我解决这个问题吗?
1条答案
按热度按时间mfuanj7w1#
谢谢你@奥利弗,为我工作:在MAUI中显示进度标签-webClient.DownloadAsync()中的文本,如下所示:
XAML格式
C语言#