1

I am creating a Xamarin Forms mobile app using VS 2019 latest version. Xamarin Forms and Essentials packages are also updated to latest. I have following viewmodel, but the LoadHouses() method is not called via

24. LoadHousesCommand = new AsyncCommand(LoadHouses); 

enter image description here

Any idea why? I also get "The breakpoint will not currently be hit" warning. Thanks

Edit: My xaml page as follows,

<?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="HomeInventory.Views.HousesPage"
             xmlns:viewmodels="clr-namespace:HomeInventory.ViewModels"
             xmlns:models="clr-namespace:Shared.Models;assembly=Shared"
             xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
             x:DataType="viewmodels:HouseViewModel"
             >
    <ContentPage.BindingContext>
        <viewmodels:HouseViewModel />
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <ListView ItemsSource="{Binding Houses}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="models:House">
                    <TextCell Text="{Binding Name}"></TextCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

When I add houses manually as follows in the constructor it works fine,

public HouseViewModel()
        {
            
            Houses = new ObservableRangeCollection<House>();
            LoadHousesCommand = new AsyncCommand(LoadHouses);
            House h1 = new House();
            h1.Name = "House 01";
            Houses.Add(h1);
            House h2 = new House();
            h2.Name = "House 02";
            Houses.Add(h2);
        }
David Clarke
  • 12,888
  • 9
  • 86
  • 116
Asela
  • 303
  • 1
  • 5
  • 16
  • 1
    This works for me. I also using VS 2019, Xamarin Forms updated version. Check your BindingContext properly and restart you VS, clean and rebuild your project. – Kaushik Aug 08 '21 at 12:47
  • @Kaushik thanks for looking into this. I have added my xaml page, have I done something wrong there? – Asela Aug 08 '21 at 14:27
  • 1
    In your xaml page, there is no binding for LoadHousesCommand and I think your xaml.cs file is not execute this command. That's why you got "The breakpoint will not currently be hit" warning. – Kaushik Aug 09 '21 at 06:33
  • 1
    May be you want to execute this LoadHousesCommand automatically when this page loads or OnAppearing ? – Kaushik Aug 09 '21 at 06:43
  • @Kaushik the code in the constructor of viewmodel including, 24. LoadHousesCommand = new AsyncCommand(LoadHouses); gets executed, but the problem is AsynCommand doesn't call LoadHouses() method. Thanks – Asela Aug 09 '21 at 12:27
  • AsynCommand doesn't execute like that. See my answer. – Kaushik Aug 09 '21 at 20:54

2 Answers2

2

You can bind the Command with a button or something can execute to call the method, like:

 <StackLayout>
        <Button Text="Load"
                Command="{Binding LoadHouseCommand}"/>
        <CollectionView x:Name="col">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding Name}"/>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
   </StackLayout>

Here is the ViewModel:

  public class HouseViewModel
    {public ObservableRangeCollection<House> Houses { set; get; }
        public IAsyncCommand LoadHouseCommand { set; get; }
        public HouseViewModel()
        {
            Houses = new ObservableRangeCollection<House>();
            LoadHouseCommand = new AsyncCommand(async()=> {
                House H1 = new House();
                H1.Name = "House 1";
                Houses.Add(H1);
                House H2 = new House();
                H2.Name = "House 2";
                Houses.Add(H2);
                Console.WriteLine("done");
            });
        }

result:

enter image description here

Adrain
  • 1,946
  • 1
  • 3
  • 7
1

If you don't want to bind your command to button or anything. You can simply execute your command from your xaml.cs file like below.

From OnAppearing before your page load. It is the best way for web api.

protected override void OnAppearing()
    {
        base.OnAppearing();

        var vm = BindingContext as HouseViewModel;
        vm.LoadHousesCommand.Execute(null);
    }

Or, You can simply run it inside your xaml.cs constructor

Task.Run(() => 
        {
            var vm = BindingContext as HouseViewModel;
            vm.LoadHousesCommand.Execute(null);
        });
Kaushik
  • 120
  • 1
  • 1
  • 10