繁体   English   中英

C#用户控件未使用DispatcherTimer Tick事件更新

[英]C# usercontrol not updating with DispatcherTimer Tick Event

我在使用DispatcherTimer发出信号以从某些温度传感器获取新数据的方式在主页上更新UserControl时遇到问题。

Sensors.xaml.cs

public sealed partial class Sensors : UserControl
{
    public Sensors()
    {
        this.InitializeComponent();
    }

    public string Sensorlbl
    {
        get { return (string)GetValue(SensorlblProperty); }
        set { SetValue(SensorlblProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Sensorlbl.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SensorlblProperty =
        DependencyProperty.Register("SensorLbl", typeof(string), typeof(Sensors), null);

Sensors.xaml

<Grid>
    <TextBlock Name="SensorLbl" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,5,0,0" Text="{Binding Sensorlbl, ElementName=userControl, Mode=TwoWay}"/>
</Grid>

MainPage.xaml.cs

    public MainPage()
    {
        tempTimer = new DispatcherTimer();
        tempTimer.Tick += GetTemp;
        tempTimer.Interval = TimeSpan.FromMilliseconds(1000);
        tempTimer.Start();

        Sensor1.Sensorlbl = "test";
    }
    public void GetTemp(object sender, object e)
    {
        Sensor1.Sensorlbl = "Test Working";

        Debug.WriteLine(Sensor1.Sensorlbl);
    }

MainPage.xaml

<local:Sensors x:Name="Sensor1" Margin="0,0,0,0" Width="107" Height="155" />

在上面的示例中,我只是用它来证明更新MainPage中使用的UserControl。 当我运行程序时,输出为“ Test Working”,但屏幕仍仅显示“ test”。

感谢您的任何建议。

DependencyProperty的注册区分大小写。 它必须与属性名称精确匹配。 您的情况不匹配:SensorLbl与Sensorlbl。

暂无
暂无

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

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