1

我正在使用 VS 2019 最新版本创建一个 Xamarin Forms 移动应用程序。Xamarin Forms 和 Essentials 包也更新到最新版本。我有以下视图模型,但未通过调用 LoadHouses() 方法

24. LoadHousesCommand = new AsyncCommand(LoadHouses); 

在此处输入图像描述

知道为什么吗?我还收到“当前不会命中断点”警告。谢谢

编辑: 我的 xaml 页面如下,

<?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>

当我在构造函数中手动添加房屋时,它工作正常,

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);
        }
4

2 回答 2

2

您可以将命令与按钮绑定,或者可以执行某些操作来调用该方法,例如:

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

这是视图模型:

  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");
            });
        }

结果:

在此处输入图像描述

于 2021-08-09T07:54:29.253 回答
1

如果您不想将命令绑定到按钮或任何东西。您可以简单地从您的 xaml.cs 文件中执行您的命令,如下所示。

在页面加载之前从 OnAppearing 开始。这是 web api 的最佳方式。

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

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

或者,您可以简单地在 xaml.cs 构造函数中运行它

Task.Run(() => 
        {
            var vm = BindingContext as HouseViewModel;
            vm.LoadHousesCommand.Execute(null);
        });
于 2021-08-09T20:52:14.370 回答