UWP - 从TextBox中的另一个线程更新TCP连接状态

时间:2017-04-20 10:23:32

标签: c# textbox uwp task observable

我的TCP连接有以下代码。我不知道它是否异步正确运行,所以我添加了Task.Delay(1000)以便让方法接收结果,将其保存到"结果"然后将其提供给用户界面。

我的问题是,如何绑定字符串"结果"到我的文本框。我希望文本框尽快在UI-Thread中刷新"结果"从Connect(...) - 方法获得结果。

public static async Task Connect(string ip, int port) {

            result = string.Empty;

            DnsEndPoint hostEntry = new DnsEndPoint(ip, port);


            Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


            SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
            socketEventArg.RemoteEndPoint = hostEntry;


            socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate (object s, SocketAsyncEventArgs e)
            {

                result = e.SocketError.ToString();    
            });


           _socket.ConnectAsync(socketEventArg);
            await Task.Delay(1000); 

        }

XAML背后的代码

            await TCPConnection.Connect(_con.IpAddress,port);
            tbConnectionText.Text = "Connectionstatus: " + _con.IpAddress;
            tbConnectionStatus.Text =TCPConnection.result;
            tbConnectionText.Visibility = Visibility.Visible;
            tbConnectionStatus.Visibility=Visibility.Visible;

1 个答案:

答案 0 :(得分:0)

您可以编写绑定并使用NotifyProperty来更改TextBox。

在ViewModel中编写Result字符串,任务将在结束时更改它。

ViewModel应该继承NotifyProperty,属性在集合中使用OnPropertyChanged。

public string Result 
{
    set
    {
         _result=value;
         OnPropertyChanged(); 
    }
    get
    {
         return _result;
    }
}

  public static async Task Connect(string ip, int port)
  {

        result = string.Empty;

        DnsEndPoint hostEntry = new DnsEndPoint(ip, port);


        Socket _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


        SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
        socketEventArg.RemoteEndPoint = hostEntry;


        socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate (object s, SocketAsyncEventArgs e)
        {

            result = e.SocketError.ToString();    
        });


       _socket.ConnectAsync(socketEventArg);
        await Task.Delay(1000); 
        Result=TCPConnection.result;
    }

您可以绑定TextBox

        Binding bind=new Binding()
        {
             Path = new PropertyPath("Result"),
        };
        bind.Source = ViewModel;
        bind.Mode=BindingMode.OneWay;
        BindingOperations.SetBinding(TextBox,TextBox.TextProperty, bind);

如果您只使用Main.xaml.cs

您也可以在Main();

中使用bind
        Binding bind=new Binding("Result");
        bind.Source = this;
        bind.Mode=BindingMode.OneWay;
        BindingOperations.SetBinding(TextBox,TextBox.TextProperty, bind);

你应该让Main继承INotifyPropertyChanged

您应该在代码中写下以下内容

public event PropertyChangedEventHandler PropertyChanged;

    public void UpdateProper<T>(ref T properValue, T newValue, [CallerMemberName] string properName = "")
    {
        if (Equals(properValue, newValue))
        {
            return;
        }

        properValue = newValue;
        OnPropertyChanged(properName);
    }

    public async void OnPropertyChanged([CallerMemberName] string name = "")
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () => { handler?.Invoke(this, new PropertyChangedEventArgs(name)); });
    }