Hello,
Welcome to our Microsoft Q&A platform!
ObservableRangeCollection
is a thrid-part nuget-packages, I do not use it, I use System.Collections.ObjectModel.ObservableCollection
to replace it, Here is running screenshot, I can get running normally without error.
Here is my ReadingsViewModel.cs
.
using System.Collections.ObjectModel;
using System.Text;
namespace App26
{
public class ReadingsViewModel
{
public ObservableCollection<MyReading> Readings { get; private set; }
public ReadingsViewModel()
{
Readings = new ObservableCollection<MyReading>();
Readings.Add(new MyReading() { Dia= 1, Sys=2, Weight=0.2f, Glocouse=0.3f, TimeOfDay=DateTime.Now });
Readings.Add(new MyReading() { Dia = 2, Sys = 2, Weight = 0.2f, Glocouse = 0.3f, TimeOfDay = DateTime.Now });
Readings.Add(new MyReading() { Dia = 3, Sys = 2, Weight = 0.2f, Glocouse = 0.3f, TimeOfDay = DateTime.Now });
Readings.Add(new MyReading() { Dia = 4, Sys = 2, Weight = 0.2f, Glocouse = 0.3f, TimeOfDay = DateTime.Now });
Readings.Add(new MyReading() { Dia = 5, Sys = 2, Weight = 0.2f, Glocouse = 0.3f, TimeOfDay = DateTime.Now });
}
}
public class MyReading
{
public DateTime TimeOfDay { get; set; }
public float Glocouse { get; set; }
public int Dia { get; set; }
public int Sys { get; set; }
public float Weight { get; set; }
public override string ToString()
{
return Glocouse.ToString();
}
}
}
I do not use x:DataType="viewmodel:ReadingsViewModel"
in your layout.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodel="clr-namespace:App26"
x:Class="App26.Page1">
<ContentPage.BindingContext>
<viewmodel:ReadingsViewModel />
</ContentPage.BindingContext>
<StackLayout>
<CollectionView ItemsSource="{Binding Readings}" SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid
Padding="10"
ColumnDefinitions="Auto,*"
RowDefinitions="Auto, *">
<Label
Grid.Column="1"
FontAttributes="Bold"
Text="{Binding TimeOfDay}" />
<Label
Grid.Row="1"
Grid.Column="1"
FontAttributes="Bold"
Text="{Binding Weight}" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ContentPage>
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.