为了账号安全,请及时绑定邮箱和手机立即绑定

C# uwp 客户端服务器显示组件

C# uwp 客户端服务器显示组件

C#
猛跑小猪 2021-10-09 16:06:10
我的服务器 uwp 端有问题。我尝试取消隐藏这样的一些组件,但我的应用程序挂起:private async void StreamSocketListener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args){    string request;    using(var streamReader = new StreamReader(args.Socket.InputStream.AsStreamForRead()))    {        request = await streamReader.ReadLineAsync();    }    await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => this.txtBlk_Events.Text = request);    if (request.Length > 0)    {        btnSend.Visibility = Visibility.Visible;    }}有可能这样做吗?
查看完整描述

1 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

Visibility是一个 UI 属性,必须在 UI 线程上设置。在您的情况下,它将在ConnectionReceived事件处理程序运行的同一线程上运行(感谢async/await),这可能不是 UI 线程。您应该改为Visibility在Dispatcher.RunAsync调用中设置 以确保它在 UI 线程上运行。


private async void StreamSocketListener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)

{

    string request;

    using(var streamReader = new StreamReader(args.Socket.InputStream.AsStreamForRead()))

    {

        request = await streamReader.ReadLineAsync();

    }


    var setVisibility = request.Length > 0;


    await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => 

    {

       this.txtBlk_Events.Text = request;

       if ( setVisibility )

       {

          btnSend.Visibility = Visibility.Visible;

       }

    });              

}


查看完整回答
反对 回复 2021-10-09
  • 1 回答
  • 0 关注
  • 153 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信