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

我的服务器 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;

    }

}

有可能这样做吗?


猛跑小猪
浏览 154回答 1
1回答

幕布斯7119047

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;       }    });              }
打开App,查看更多内容
随时随地看视频慕课网APP