注意事项:
1、在ListView中无法直接绑定Command需要这样写
Command="{Binding BindingContext.ClaimAssignmentClickCommand,Source={x:Reference cpg}}"
其中cpg是界面名称
2、传递参数
ListView绑定的数据源:
<ListView x:Name="lv_QMSampList" RowHeight="-1" HasUnevenRows="True" BackgroundColor="#ECECEC" ItemsSource="{Binding QmSampListItemsSource}" SeparatorVisibility="None">
</ListView>
我想将ListView绑定的参数传递给Command
CommandParameter="{Binding .}"
3、ViewModel中接收
public QMSampAssignmentViewModel(BsUserList user, QMSampAssignmentPage page)
{
this.user = user;
this.page = page;
ClaimAssignmentClickCommand = new Command<QMSampingListItem>(async (item) => await BtnClaimAssignmentClick(item));
}
public async Task BtnClaimAssignmentClick(QMSampingListItem item) {
if (!XaApp.App.IsNetAvailable)
await page.DisplayAlert("错误", "无网络连接", "确定");
}
源码:
1、创建界面
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
xmlns:resources="clr-namespace:ErpApp.Resources"
mc:Ignorable="d"
Title="领取取样任务"
x:Name="cpg"
x:Class="ErpApp.Views.QMSamping.QMSampAssignmentPage" >
<ContentPage.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<resources:Colors />
<resources:Styles />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<StackLayout>
<!--<ScrollView>-->
<ListView x:Name="lv_QMSampList" RowHeight="-1" HasUnevenRows="True" BackgroundColor="#ECECEC" ItemsSource="{Binding QmSampListItemsSource}" SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ContentView Padding="5,5">
<Frame Padding="1" CornerRadius="10">
<StackLayout Padding="8,8,0,8">
<StackLayout Orientation="Horizontal">
<Label Text="库 位:" FontAttributes="Bold" VerticalOptions="CenterAndExpand" HorizontalOptions="Start"></Label>
<Label Text="{Binding STORELOCA_NAME}" VerticalOptions="CenterAndExpand" HorizontalOptions="StartAndExpand"></Label>
<Button Text="领取任务"
HorizontalOptions="Start"
VerticalOptions="CenterAndExpand"
Style="{StaticResource PrimaryLayuiButtonStyle}"
Command="{Binding BindingContext.ClaimAssignmentClickCommand,Source={x:Reference cpg}}"
CommandParameter="{Binding .}"
android:Button.UseDefaultShadow="True"
android:Button.UseDefaultPadding="True"/>
</StackLayout>
</StackLayout>
<Frame.Triggers>
<DataTrigger TargetType="Frame" Binding="{Binding ISURGQMINSP}" Value="1">
<Setter Property="BackgroundColor" Value="#FF8585"/>
</DataTrigger>
</Frame.Triggers>
</Frame>
</ContentView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
2、界面后端代码
namespace ErpApp.Views.QMSamping
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class QMSampAssignmentPage : ContentPage
{
public QMSampAssignmentPage(BsUserList bsUserList)
{
InitializeComponent ();
this.BindingContext = new QMSampAssignmentViewModel(bsUserList,this);
}
}
}
3、ViewModel
using AppUntils;
using EamApp.ViewModels;
using Newtonsoft.Json;
using StrongModel;
using StrongModel.Erp;
using StrongModel.Erp.InspectionModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace ErpApp.Views.QMSamping.ViewModel
{
public class QMSampAssignmentViewModel : BaseViewModel
{
#region 属性
public BsUserList user;
public QMSampAssignmentPage page;
/// <summary>
/// 原数据
/// </summary>
private List<QMSampingListItem> CopyData =new List<QMSampingListItem>();
private List<QMSampingListItem> _QmSampListItemsSource =new List<QMSampingListItem>();
public List<QMSampingListItem> QmSampListItemsSource
{
get { return _QmSampListItemsSource; }
set
{
_QmSampListItemsSource = value;
OnPropertyChanged();
}
}
#endregion
/// <summary>
/// 领取任务
/// </summary>
public ICommand ClaimAssignmentClickCommand { get; }
public QMSampAssignmentViewModel(BsUserList user, QMSampAssignmentPage page)
{
this.user = user;
this.page = page;
ClaimAssignmentClickCommand = new Command<QMSampingListItem>(async (item) => await BtnClaimAssignmentClick(item));
}
/// <summary>
/// 领取任务
/// </summary>
/// <returns></returns>
public async Task BtnClaimAssignmentClick(QMSampingListItem item) {
if (!XaApp.App.IsNetAvailable)
await page.DisplayAlert("错误", "无网络连接", "确定");
try
{
//var cell = ((sender as VisualElement).Parent.Parent.Parent.Parent.Parent as ViewCell).BindingContext as QMSampingListItem;
//if (cell.BASIC_BILLSTATUS_ID == "1003")
//{
// await this.DisplayAlert("温馨提示", "该单据已提交,禁止重复提交,请刷新数据!", "确定");
// return;
//}
QmSampingAssignmentModel model = new QmSampingAssignmentModel();
}
catch (Exception ex)
{
await page.DisplayAlert("任务领取失败", ex.Message, "确定");
}
}
}
}