Search code examples
c#xamldata-bindinguwpbluetooth-lowenergy

UWP data binding: property change in event handler does not update UI


I'm trying to bind data (OneWay mode), recieved over Bluetooth LE, with TextBlock, but I can not make it work.

I have one Mainpage that contains another secondary Page. This secondary Page have TextBlock used as target inside Grid's column:

<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>

Class used as source looks like this:

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));
    }
}

Next, in C# file, for secondary Page, is registred handler that recieves notifications from Bluetooth device and stores them into SData property (Property Accel_x has type GattCharacteristic and is defined in MainPage, whose instance is accessible over rootPage variable)

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();
            };
}

Even though data in SData are changed, the UI does not update. Does anyone have any idea why? I have already spend many hours trying to solve it.

But, when SData are updated by PointerPressed event handler, triggred by rectangle element it works ok.

 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;
            });

Any idea why updatig by one hadler works and by the other not? Can this be caused by defining Accel_x inside different page then the target?


Solution

  • So, finaly I figured it out. After adding Dispatcher into ValueChanged handler it started to work:

    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();
                    });
                };