首页 > 解决方案 > UWP 数据绑定:事件处理程序中的属性更改不会更新 UI

问题描述

我正在尝试使用 TextBlock 绑定通过蓝牙 LE 接收的数据(单向模式),但我无法使其工作。

我有一个包含另一个辅助页面的主页。此辅助页面将 TextBlock 用作 Grid 列内的目标:

<Grid x:Name="DataPanel" 
              RelativePanel.Below="ControlPanel"
              RelativePanel.AlignLeftWithPanel="True"
              RelativePanel.AlignRightWithPanel="True">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Rectangle x:Name="ax_down0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Fill="Transparent" Grid.Column="0" />
            <StackPanel Orientation="Vertical" Margin="10" Grid.Column="0" HorizontalAlignment="Center"
                        DataContext="">
                <TextBlock Text="Accelerometr X" TextAlignment="Center" FontWeight="Bold"/>
                <TextBlock Name="ADataX" Text="{x:Bind Path=SData.Acceleration_x, Mode=OneWay}" TextAlignment="Center"/>
            </StackPanel>
            <Rectangle x:Name="ax_up0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Fill="Transparent" Grid.Column="0"/>
            .
            .
            .
</Grid>

用作源的类如下所示:

public class SensorData : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private UInt16 acceleration_x;
    public UInt16 Acceleration_x
    {
        get
        {
            return acceleration_x;
        }
        set
        {
            acceleration_x = value;
            OnPropertyChanged("Acceleration_x");
        }
    }
    .
    .
    .

    public SensorData()
    {
        Acceleration_x = 0;
        Acceleration_y = 0;
        Acceleration_z = 0;
    }

    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        // Raise the PropertyChanged event, passing the name of the property whose value has changed.
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

接下来,在 C# 文件中,对于辅助页面,注册了从蓝牙设备接收通知并将其存储到 SData 属性中的处理程序(属性 Accel_x 具有 GattCharacteristic 类型并在 MainPage 中定义,其实例可通过 rootPage 变量访问)

public sealed partial class Page2 : Page
{
    private MainPage rootPage;
    public SensorData SData { get; set; }

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
     rootPage.Accel_x.ValueChanged += delegate (GattCharacteristic sender, GattValueChangedEventArgs args)
            {
                var reader = DataReader.FromBuffer(args.CharacteristicValue);
                SData.Acceleration_x = reader.ReadByte();
            };
}

即使 SData 中的数据发生更改,UI 也不会更新。有谁知道为什么?我已经花了很多时间试图解决它。

但是,当 PointerPressed 事件处理程序更新 SData 时,由矩形元素触发它可以正常工作。

 ax_up0.PointerPressed += new PointerEventHandler(delegate (object sender, PointerRoutedEventArgs e)
            {
                e.Handled = true;
                ax_down0.Fill = new SolidColorBrush(Windows.UI.Colors.DimGray);
                UInt16 i = SData.Acceleration_x;
                i++;
                SData.Acceleration_x = i;
            });

知道为什么一个 hadler 有效而另一个不更新吗?这可能是由于在不同页面中定义 Accel_x 而不是目标引起的吗?

标签: c#xamldata-bindinguwpbluetooth-lowenergy

解决方案


所以,最后我想通了。将 Dispatcher 添加到 ValueChanged 处理程序后,它开始工作:

rootPage.Accel_x.ValueChanged += async delegate (GattCharacteristic sender, GattValueChangedEventArgs args)
            {
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => {
                    var reader = DataReader.FromBuffer(args.CharacteristicValue);
                    SData.Acceleration_x = reader.ReadByte();
                });
            };

推荐阅读