Привязка Xamarin XAML к коду за свойствами (ListView) - PullRequest
0 голосов
/ 24 августа 2018

Я хочу связать свое свойство code-behind, которое является списком, с XAML ListView. Однако мой ListView никогда не заполняется, и я понятия не имею, почему.

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="BLEPl.EvaluationPage"
             Title="Evaluation">
    <ContentPage.Content>
        <StackLayout Padding="0,20,0,20">      
            <ListView HasUnevenRows="True" IsVisible="True" x:Name="TableLayout" ItemsSource="{Binding Path=SensorValues}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid>
                                <Label Style="{StaticResource TitleLabel}" Grid.Column="0" Grid.Row="0" Text="Zeitstempel: " />
                                <Label Style="{StaticResource DataLabel}" Grid.Column="1" Grid.Row="0" Text="{Binding TimeStamp}" />

                                <Label Style="{StaticResource TitleLabel}" Grid.Column="0" Grid.Row="1" Text="Wert: " />
                                <Label Style="{StaticResource DataLabel}" Grid.Column="1" Grid.Row="1" Text="{Binding Value}" />

                                <Label Style="{StaticResource TitleLabel}" Grid.Column="0" Grid.Row="2" Text="Sensor: " />
                                <Label Style="{StaticResource DataLabel}" Grid.Column="1" Grid.Row="2" Text="{Binding Sensor.Name}" />
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class EvaluationPage : ContentPage
{
    private ObservableCollection<SensorValue> SensorValues = new ObservableCollection<SensorValue>();

    public EvaluationPage ()
    {
        InitializeComponent ();

        using (var context = new BlepContext(true))
        {
            var repo = new BLEPRepository(context);
            SensorValues = new ObservableCollection<SensorValue>(repo.GetAllSensorValues());
        }
    }
}

Когда я запускаю это, мой ListView не содержит никаких элементов. Я не получаю ошибок, и мое приложение компилируется нормально. Я что-то здесь не так делаю?

1 Ответ

0 голосов
/ 24 августа 2018
<ListView HasUnevenRows="True" IsVisible="True" x:Name="TableLayout" ItemsSource="{Binding SensorValues}">

вы можете привязать только к публичным свойствам, и вам нужно установить BindingContext для страницы

// needs to be a PUBLIC PROPERTY
public ObservableCollection<SensorValue> SensorValues { get; set; }

public EvaluationPage ()
{
    InitializeComponent ();

    // set the BindingContext
    this.BindingContext = this;

    using (var context = new BlepContext(true))
    {
        var repo = new BLEPRepository(context);
        SensorValues = new ObservableCollection<SensorValue>(repo.GetAllSensorValues());
    }
}
...