【发布时间】:2018-07-01 12:03:13
【问题描述】:
我遇到了这个解决方案,它应该绑定到根页面而不是对我不起作用的列表视图(我正在尝试在列表视图内按下按钮并传递列表视图项目 ID 时执行命令)现在( Path=BindingContext.RequestAccepted) 无法解析对象类型数据上下文中的属性“RequestAccepted”。
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:XamarinApp.ViewModels;assembly=XamarinApp"
x:Name="RequestsPage"
x:Class="XamarinApp.ViewModels.Views.CustomerTransferRequestsPage">
<ContentPage.BindingContext>
<viewModels:CustomerTransferRequestsViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout >
<Label Text="لا يوجد لديك طلبات حالياً" IsVisible="{Binding EmptyLableVisible}" ></Label>
<ActivityIndicator IsRunning="{Binding IsLoading}" HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"/>
<ListView ItemsSource="{Binding RequestedItems}"
HasUnevenRows="True"
ItemTapped="ListView_OnItemTapped"
>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding RequestUserName}"></Label>
<Label Text="{Binding ItemsName}"></Label>
<Label Text="{Binding ItemsPrice}"></Label>
<StackLayout Orientation="Vertical">
<Button Text="قبول" Command="{Binding Source={x:Reference RequestsPage}, Path=BindingContext.RequestAccepted}"></Button>
<Button Text="رفض"></Button>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
视图模型
public class CustomerTransferRequestsViewModel : INotifyPropertyChanged
{
public CustomerTransferRequestsViewModel()
{
if (GetRequestedItems.CanExecute(null))
{
GetRequestedItems.Execute(null);
}
}
ApiServices _apiServices = new ApiServices();
private ObservableCollection<GetCustomerTransferOrderRespond> _requestedItems;
private bool _emptyLableVisible;
private bool _isLoading;
public ObservableCollection<GetCustomerTransferOrderRespond> RequestedItems
{
get => _requestedItems;
set
{
if (Equals(value, _requestedItems)) return;
_requestedItems = value;
OnPropertyChanged();
}
}
public bool EmptyLableVisible
{
get => _emptyLableVisible;
set
{
if (Equals(value, _emptyLableVisible)) return;
_emptyLableVisible = value;
OnPropertyChanged();
}
}
public bool IsLoading { get => _isLoading; set
{
if (Equals(value, _isLoading)) return;
_isLoading = value;
OnPropertyChanged();
}
}
public ICommand GetRequestedItems
{
get
{
return new Command(async () =>
{
IsLoading = true;
var accesstoken = Settings.AccessToken;
RequestedItems = await _apiServices.GetCustomerTranferOrdersAsync(accesstoken);
if (RequestedItems == null)
{
EmptyLableVisible = true;
IsLoading = false;
}
else
{
EmptyLableVisible = false;
IsLoading = false;
}
});
}
}
public ICommand RequestAccepted
{
get
{
return new Command(async () =>
{
//RequestAccepted Logic
});
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
【问题讨论】:
-
看起来应该可以了。试试“..Source={x:Reference Name=RequestsPage}..” Name= 部分是我看到的与过去的唯一区别。需要设置一个测试项目来验证。
-
不幸地尝试过......同样的事情。
标签: listview xamarin xamarin.forms binding icommand