jeudi 15 mars 2018

ReactiveUI RaiseAndSetIfChanged reflection issue

I'm using reactiveui and all works good until I try to update a public property of my viewmodel using reflection. I've verified that the value does change but the UI is never made aware of it and never updates. Is there a way I can force the UI to update from the viewmodel? Here is how I'm updating a public property using reflection:

One of the trackitem's I'm setting, there are 20 of them

private TrackItem _TrackItem12;
public TrackItem TrackItem12
    {
        get
        {
            return _TrackItem12;
        }
        set { this.RaiseAndSetIfChanged(ref _TrackItem12, value); }
    }

Here is the method in the viewmodel I call after I get data into "Completed":

private void SetTrackBoxData()
    {            
        for(var x = 1; x < 21; x++)
        {
            var publicPropertyName = "TrackItem" + x;
            // Completed has property names Field1, Field2, Field3, etc...
            object sourceValue = typeof(CompletedResult).GetProperty("Field" +  x).GetValue(Completed, null);
            TrackItem targetProperty =  (TrackItem)typeof(ActivityCompletedAppointmentPageEditViewModel).GetProperty(publicPropertyName).GetValue(this, null);
            // This method should really end with the next line
            targetProperty.Total = sourceValue.ToString();
            // I tried going even further by deleting/recreating object
            var cacheProps = new TrackItem()
            {
                Total = targetProperty.Total,
                Lines = targetProperty.Lines,
                Heading = targetProperty.Heading,
                Paragraph = targetProperty.Paragraph
            };
            targetProperty = null;
            targetProperty = cacheProps;
        }
    }

Here is one of the grid's I'm using to display each trackitem:

<Grid Padding="10" Grid.Column="0" Grid.Row="26" Grid.ColumnSpan="2" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                    </Grid.RowDefinitions>
                    <Label Grid.Row="0" Text="Track 12"></Label>
                    <Entry Grid.Row="1"  Placeholder="Enter #" Text="{Binding TrackItem12.Total}"></Entry>
                    <Label Grid.Row="2" Text="{Binding TrackItem12.Heading}"></Label>
                    <Label Grid.Row="3" LineBreakMode="WordWrap" Text="{Binding TrackItem12.Paragraph}"></Label>
                </Grid>





Aucun commentaire:

Enregistrer un commentaire