In a resource dictionary I am defining a DataTemplate with x:Key="AnimalsDataTemplate". The resource dictionary is registered under Application.Resources in App.xaml.
In my main page, I have a RefreshView containing a ListView named ListView1, and a class named Animals, and a list AnimalsList populated when new data is created from the Animals class. ListView1's ItemTemplate.DataTemplate is set to
x:Name="AnimalsDataTemplate".
I am trying to assign the Animals class to the ListView1's ItemSource; I can assign the variable x like this
<x:Array Type="{x:Type local:MainPage}">
</x:Array>
but I cannot assign it to MainPage.Animals.
I have tried coding all this just in the ListView tag itself, but it will not bind to the Animals class either.
This is my App.xaml file:
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:M_Android"
x:Class="M_Android.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/DataTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
And here's my DataTemplates.xaml file:
<?xml version="1.0" encoding="UTF-8" ?>
<?xaml-comp compile="true" ?>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<DataTemplate x:Key="AnimalsDataTemplate">
<ViewCell>
<Grid RowSpacing="6"
ColumnSpacing="6">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Text="{Binding Name}"
TextColor="Black"
FontAttributes="Bold" />
<Label Grid.Column="1"
Text="{Binding Location}"
TextColor="Black" />
<Label Grid.Column="2"
Text="{Binding Age}"
TextColor="Black"
HorizontalTextAlignment="End" />
</Grid>
</ViewCell>
</DataTemplate>
</ResourceDictionary>
My MainPage XAML file:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:M_Android"
x:Class="M_Android.MainPage">
<ScrollView>
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<RefreshView
IsRefreshing="{Binding IsRefreshing}"
Command="{Binding RefreshCommand}"
x:Name="Rfresh">
<ListView x:Name = "ListView1">
<ListView.ItemsSource>
<x:Array Type="{x:Type local:MainPage}">
</x:Array>
</ListView.ItemsSource>
<ListView.ItemTemplate>
<DataTemplate x:Name="AnimalsDataTemplate">
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</RefreshView>
</VerticalStackLayout>
</ScrollView>
</ContentPage>
MainPage.cs file:
public List<Animals> AnimalsList = new List<Animals>();
public class Animals
{
public string Name { get; set; }
public string Location { get; set; }
public string Age { get; set; }
public override string ToString()
{
return Name;
}
public Animals(string name, string location, string age)
{
Name = name;
Location = location;
Age = age;
}
}
public void ButtonClick(object sender, EventArgs args)
{
refreshListView();
}
public void refreshListView()
{
RefreshView refreshView = new RefreshView();
ICommand refreshCommand = new Command(() => {
// IsRefreshing is true
// Refresh data here
var S = new Animals("Jon", "22", "House");
AnimalsList.Add(S);
var S1 = new Animals("Jane", "20", "House");
AnimalsList.Add(S1);
var S2 = new Animals("Bill", "300", "House");
AnimalsList.Add(S2);
refreshView.IsRefreshing = false;
});
refreshView.Command = refreshCommand;
refreshCommand.Execute(Rfresh);
}