C#usercontrol不使用DispatcherTimer Tick事件更新

时间:2017-06-07 03:06:30

标签: c# xaml user-controls uwp windows-10-iot-core

我遇到问题,主页中有UserControl更新,使用DispatcherTimer发出信号,从某些温度传感器获取新数据。

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。当我运行程序时,我得到“测试工作”的输出,但屏幕仍然只显示“测试”。

感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

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

相关问题