【发布时间】: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