我正在嘗試將一個ListView專案Command連接到我的VendorsViewModel,但不斷收到以下錯誤。
在 MyProject 上找不到 VendorSelectedCommand 屬性,目標屬性 Xamarin.Forms.TextCell.Command
這是我的ListView代碼:
<ListView
x:Name="VendorsList"
ItemsSource="{Binding Vendors}"
BackgroundColor="Transparent">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding FullName}" Command="{Binding Source={x:Reference Name=VendorsList}, Path=BindingContext.VendorSelectedCommand}" CommandParameter="{Binding .}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
這是我的VendorsViewModel代碼:
public class VendorsViewModel : BaseViewModel
{
public LayoutState _mainState;
public AsyncCommand VendorSelectedCommand;
public VendorsViewModel()
{
Title = string.Empty;
IsBusy = true;
MainState = LayoutState.Loading;
VendorSelectedCommand = new AsyncCommand(VendorSelected);
}
public LayoutState MainState
{
get => _mainState;
set => SetProperty(ref _mainState, value);
}
ObservableRangeCollection<Vendor> vendors = new ObservableRangeCollection<Vendor>();
public ObservableRangeCollection<Vendor> Vendors
{
get => vendors;
set
{
if (vendors == value)
return;
vendors = value;
OnPropertyChanged(nameof(Vendors));
}
}
async Task VendorSelected(Vendor vendor)
{
var route = $"{nameof(VendorProfile)}";
await Shell.Current.GoToAsync(route);
}
public async void Init()
{
IsBusy = true;
MainState = LayoutState.Loading;
// Call my service method to populate vendors
}
}
我在這里做錯了什么?
uj5u.com熱心網友回復:
主要問題在這個參考中。參考應該是頁面而不是串列。將 ax:Name 賦予 ContantPage,然后在 textCell 中參考它
<TextCell Text="{Binding FullName}" Command="{Binding BindingContext.VendorSelectedCommand, Source={x:Reference PageName}}" CommandParameter="{Binding .}" />
另外嘗試將您的 AsyncCommand 更改為 ICommand 。
public ICommand VendorSelectedCommand {get; set;}
(VendorSelectedCommand = new Command<object>(async (o) => await VendorSelected(o)));
async Task VendorSelected(object o)
{
// do stuff with received object
}
uj5u.com熱心網友回復:
將<ViewCell></ViewCell>標簽添加到您的 ListView。
像這樣的格式:
<ListView x:Name="listView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/437764.html
上一篇:有內容型別,但檔案已下載
