繁体   English   中英

将由单独的线程更改的静态对象属性绑定到WPF中的标签

[英]Binding a static objects property which is changed by a separate thread to a label in WPF

我目前正在尝试更新WPF项目中的标签以显示TCP服务器的状态。 文本框将在启动时绑定到属性,并正确显示在标签中,但是我正努力寻找一种方法来通知该属性已从另一个线程更改并更新标签。

private void startServerMultiThread() 
    {
        try
        {
            Thread t = new Thread(() => MultiThreadedListener.startListening());
            t.Start();
        }
        catch (Exception e)
        {
            showErrors(e);
        }
    }

在MultiThreadedListener.startListening()中,启动新的TCP侦听器后,Status.ServerStatus设置为true。

public class Status
{
    public static EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

    private static bool _ServerStatus = false;
    public static bool ServerStatus
    {
        get { return _ServerStatus; }

        set
        {
            if (value != _ServerStatus)
            {
                _ServerStatus = value;

                NotifyStaticPropertyChanged("ServerStatus");
            }
        }
    }
    private static void NotifyStaticPropertyChanged(string propertyName)
    {
        if (StaticPropertyChanged != null)
            StaticPropertyChanged(null, new PropertyChangedEventArgs(propertyName));
    }
}

我经过逐步检查,发现ServerStatus更新时我的EventHandler始终为null,并且还收到绑定错误:

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=TabStripPlacement; DataItem=null; target element is 'TabItem' (Name='serverMainTab'); target property is 'NoTarget' (type 'Object')

我的XAML标签:

<Window x:Class="serverGUI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:serverGUI"
    xmlns:server="clr-namespace:server;assembly=server"

    Title="MainWindow" Height="386" Width="418">
<Grid>
    <TabControl x:Name="serverTabControl">
        <TabItem x:Name="serverMainTab" Header="Server"  Margin="-2,-2,2,0">
            <Grid>
                <!-- SERVER STATUS -->
                <Label Content="Status:" Margin="10,10,0,0" Grid.Row="1" VerticalAlignment="Top" Height="26" HorizontalAlignment="Left" Width="94"/>
                <Label x:Name="server_status" Content="{Binding Path=(server:Status.ServerStatus)}" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="10,14,0,0" Grid.Row="1" VerticalAlignment="Top" Width="155" Grid.RowSpan="2"/>
            </Grid>
    </TabControl>
</Grid>

我将如何确保WPF应用程序正在侦听备用线程上的事件? 这是否是解决此问题的正确方法,还是我需要寻找另一种方法? 谢谢你的帮助。

您在StaticPropertyChanged事件声明中缺少event关键字。

它应该是

public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

代替

public static EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM