【问题标题】:Xamarin Community Toolkit AsynCommand not workingXamarin 社区工具包 AsynCommand 不工作
【发布时间】:2021-10-12 07:01:01
【问题描述】:

我正在使用 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);
        }

【问题讨论】:

  • 这对我有用。我还使用 VS 2019,Xamarin Forms 更新版本。正确检查您的 BindingContext 并重新启动 VS,清理并重建您的项目。
  • @Kaushik 感谢您对此进行调查。我添加了我的 xaml 页面,我在那里做错了吗?
  • 在您的 xaml 页面中,LoadHousesCommand 没有绑定,我认为您的 xaml.cs 文件没有执行此命令。这就是您收到“当前不会命中断点”警告的原因。
  • 您可能想在此页面加载时自动执行此 LoadHousesCommand 还是 OnAppearing ?
  • @Kaushik 视图模型构造函数中的代码,包括 24. LoadHousesCommand = new AsyncCommand(LoadHouses);被执行,但问题是 AsynCommand 没有调用 LoadHouses() 方法。谢谢

标签: xamarin.forms xamarin-community-toolkit


【解决方案1】:

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

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

结果:

【讨论】:

  • 是的,但我想从 web api 加载房屋详细信息。关于如何做到这一点的任何想法?谢谢
  • 正如 Kaushil 所说,覆盖 Onappearing 可以工作。
【解决方案2】:

如果您不想将命令绑定到按钮或任何东西。您可以简单地从您的 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-09-21
    • 2021-07-25
    • 2021-10-08
    • 2021-05-06
    • 1970-01-01
    • 2021-08-04
    • 2022-01-21
    • 2019-11-09
    • 2021-09-10
    相关资源
    最近更新 更多