我试图通过Event SwipeEnded从刷过的行中获取数据 但SwipeEnded事件返回没有数据的选择项
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"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
xmlns:sfgrid="clr-namespace:Syncfusion.SfDataGrid.XForms;assembly=Syncfusion.SfDataGrid.XForms"
xmlns:b="clr-namespace:Xamarin.Behaviors;assembly=Xamarin.Behaviors"
x:Class="PrismUnityApp2.Views.Hovedside"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="test">
<ContentPage.Content>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="50"/>
<RowDefinition Height="*" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<DatePicker Date="{Binding SelectedDate,Mode=TwoWay}" MinimumDate="{Binding MinimumSelectedDate,Mode=TwoWay}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" HorizontalOptions="Center">
<DatePicker.Format>dd-MM-yyyy</DatePicker.Format>
</DatePicker>
<sfgrid:SfDataGrid x:Name="dataGrid"
AutoGenerateColumns="False"
AllowSwiping="true"
ColumnSizer="Star"
SelectedIndex="1"
SelectionMode="Single"
ItemsSource="{Binding ToemmeListe}"
AllowSorting="True"
Grid.Row="2"
Grid.Column="0" Grid.ColumnSpan="3"
>
<b:Interaction.Behaviors>
<b:BehaviorCollection>
<b:EventToCommand Command="{Binding SelectionCommand}"
CommandParameter="{x:Reference Name=dataGrid}"
EventName="SelectionChanged" />
<b:EventToCommand Command="{Binding SwipeCommand}"
CommandParameter="{x:Reference Name=dataGrid}"
EventName="SwipeEnded" />
</b:BehaviorCollection>
</b:Interaction.Behaviors>
<sfgrid:SfDataGrid.Columns x:TypeArguments="syncfusion:Columns">
<sfgrid:GridTextColumn HeaderText="Fraktion"
MappingName="Fraktion" />
<!--Fraktion-->
<sfgrid:GridTextColumn HeaderText="Type"
MappingName="Type" />
<!--Type-->
<sfgrid:GridTextColumn HeaderText="Vaegt"
MappingName="Vaegt" />
<!--vægt-->
</sfgrid:SfDataGrid.Columns>
<sfgrid:SfDataGrid.LeftSwipeTemplate>
<DataTemplate>
<ContentView BackgroundColor="#1AAA87" >
<Grid IsVisible="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width ="20" />
<ColumnDefinition Width ="*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="1"
Text ="Slet"
HorizontalTextAlignment="Start"
VerticalTextAlignment="Center"
LineBreakMode ="NoWrap"
BackgroundColor="Red"
TextColor ="White"/>
</Grid>
</ContentView>
</DataTemplate>
</sfgrid:SfDataGrid.LeftSwipeTemplate>
</sfgrid:SfDataGrid>
<Button Text="Menu" FontSize="24" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3" HorizontalOptions="Center" Command="{Binding popup}"/>
</Grid>
</ContentPage.Content>
</ContentPage>
主视图模型
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using Syncfusion.SfDataGrid.XForms;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace PrismUnityApp2.ViewModels
{
public class HovedsideViewModel : BindableBase, INotifyPropertyChanged, INavigationAware
{
MobileService.VaksServiceClient ws = new MobileService.VaksServiceClient();
INavigationService _navigationService;
public event PropertyChangedEventHandler PropertyChanged;
#region Properties
private DateTime selectedDate;
public DateTime SelectedDate
{
get { return selectedDate; }
set
{
if (selectedDate != value)
{
selectedDate = value;
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs("SelectedDate"));
}
}
}
}
private DateTime minimumSelectedDate;
public DateTime MinimumSelectedDate
{
get { return minimumSelectedDate; }
set
{
if (minimumSelectedDate != value)
{
minimumSelectedDate = value;
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs("MinimumSelectedDate"));
}
}
}
}
public DelegateCommand Navigatetest { get; private set; }
public DelegateCommand NavigateToBestilling { get; private set; }
public DelegateCommand popup { get; private set; }
public string latitude { get; set; }
private string _longitude;
public string longitude
{
get { return _longitude; }
set { _longitude = value; }
}
public String PladsID { get; set; }
private ObservableCollection<ToemningData> toemmeListe;
public ObservableCollection<ToemningData> ToemmeListe
{
get { return toemmeListe; }
set { this.toemmeListe = value; }
}
#endregion
public HovedsideViewModel(INavigationService navigationService)
{
SetRowData(); // initialize the grid
SelectedDate = DateTime.Today.AddDays(1);
_navigationService = navigationService;
Navigatetest = new DelegateCommand(Navigate);
NavigateToBestilling = new DelegateCommand(_NavigateToBestilling);
popup = new DelegateCommand(_popup);
selectionCommand = new Command<SfDataGrid>(onSelectionChanged);
swipeCommand = new Command<SfDataGrid>(onSwipeChanged);
selectedItems = new ObservableCollection<object>();
}
private Command<SfDataGrid> selectionCommand;
public Command<SfDataGrid> SelectionCommand
{
get { return selectionCommand; }
set { selectionCommand = value; }
}
private Command<SfDataGrid> swipeCommand;
public Command<SfDataGrid> SwipeCommand
{
get { return swipeCommand; }
set { swipeCommand = value; }
}
private ObservableCollection<object> selectedItems;
public ObservableCollection<object> SelectedItems
{
get { return selectedItems; }
set { selectedItems = value; }
}
private void onSelectionChanged(SfDataGrid obj)
{
//gets the selected items in the datagrid
selectedItems = obj.SelectedItems;
}
private void onSwipeChanged(SfDataGrid obj)
{
selectedItems = obj.SelectedItems; // returns null on all properties
}
async void _popup()
{
Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
{
var action = await App.Current.MainPage.DisplayActionSheet("ActionSheet: SavePhoto?", null, null, "Manuel bestilling", "Scan", "Send", "Fortryd");
switch (action)
{
case "Manuel bestilling":
_NavigateToBestilling();
break;
case "Scan":
Navigate();
break;
case "Send":
// TODO make webservice that sends recipe
break;
case "Fortryd":
// Nothing
break;
default:
break;
}
});
}
MobileService.Toemning ToWCFServiceTodoItem(MobileService.Toemning item)
{
return new MobileService.Toemning
{
Fraktion = item.Fraktion,
ToemmeId = item.ToemmeId,
Type = item.Type,
date = item.date,
Latitude = item.Latitude,
Longitude = item.Longitude,
Weight = item.Weight,
PladsId = item.PladsId
};
}
public async Task<ObservableCollection<MobileService.Toemning>> CallService(MobileService.Toemning item)
{
var todoItem = ToWCFServiceTodoItem(item);
MobileService.VaksServiceClient client = new MobileService.VaksServiceClient(new BasicHttpBinding(), new EndpointAddress("http://kss.vestfor.dk/Vaks2Svc/VaksService.svc?singleWsdl"));
var t = Task<ObservableCollection<MobileService.Toemning>>.Factory.FromAsync(
((MobileService.IVaksService)client.InnerChannel).BeginGetToemmeDatasByPladsId,
((MobileService.IVaksService)client.InnerChannel).EndGetToemmeDatasByPladsId,
todoItem, TaskCreationOptions.None);
return await t;
}
public void SetRowData()
{
try
{
toemmeListe = new ObservableCollection<ToemningData>();
}
catch (Exception ex)
{
throw;
}
ToemningData _ToemningData = new ToemningData();
_ToemningData.Parent = this;
ToemmeListe.Add(_ToemningData);
}
#region Navigation
private void _NavigateToBestilling()
{
var p = new NavigationParameters();
p.Add("pladsid", PladsID);
var ttttt = DateTime.Today;
if (SelectedDate.Date > DateTime.Now.Date)
{
p.Add("selecteddate", SelectedDate);
}
_navigationService.NavigateAsync("Bestilling", p);
}
private void Navigate()
{
var p = new NavigationParameters();
p.Add("pladsid", PladsID);
p.Add("selecteddate", SelectedDate);
_navigationService.NavigateAsync("Scanner", p);
}
public void OnNavigatedFrom(NavigationParameters parameters)
{
}
public async void OnNavigatedTo(NavigationParameters parameters)
{
PladsID = parameters["pladsid"].ToString();
if (parameters.ContainsKey("selecteddate"))
{
SelectedDate = DateTime.Parse(parameters["selecteddate"].ToString());
}
GridUpdate();
}
#endregion
public void GridUpdate()
{
Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
{
// if the phone inst near latitude "55,7070631" and longitude "12,4235393" then remove the part where it gets data from parameters
// and use the two lines under this
//latitude = "55,7070631";
//longitude = "12,4235393";
var ggg = await CallService(new MobileService.Toemning { PladsId = Convert.ToInt32(PladsID), date = new DateTime(2016, 11, 1) }); // the date is set to a day where there is data // TODO set date = DateTime.Today.AddDays(1);
ToemmeListe.Clear();
foreach (var toemming in ggg)
{
ToemningData _ToemningData = new ToemningData();
_ToemningData.Parent = this;
_ToemningData.Fraktion = toemming.Fraktion;
_ToemningData.ToemmeID = toemming.ToemmeId;
_ToemningData.Type = toemming.Type;
_ToemningData.Vaegt = toemming.Weight;
ToemmeListe.Add(_ToemningData);
}
});
}
public void GridDelete()
{
}
}
}
行使用的ViewModel会被发送到主ViewModel
using Prism.Commands;
using System;
using System.ComponentModel;
using Xamarin.Forms;
namespace PrismUnityApp2.ViewModels
{
public class ToemningData : INotifyPropertyChanged
{
public DelegateCommand DeleteRow { get; private set; }
public ToemningData()
{
DeleteRow = new DelegateCommand(_DeleteRow);
}
private void _DeleteRow() // Do not delete anything from yesterday or before
{
// TODO get the ID of the selected(swiped) ROW
// TODO sendt it to GridDelete() where it will make a WS call and remove that row of data
//Parent.GridDelete();
Parent.GridUpdate();
}
private int toemmeID { get; set; }
public int ToemmeID
{
get
{
return toemmeID;
}
set
{
this.toemmeID = value;
RaisePropertyChanged("ToemmeID");
}
}
private string fraktion { get; set; }
public string Fraktion
{
get
{
return fraktion;
}
set
{
this.fraktion = value;
RaisePropertyChanged("Fraktion");
}
}
private string type { get; set; }
public string Type
{
get
{
return type;
}
set
{
this.type = value;
RaisePropertyChanged("Type");
}
}
private string vaegt { get; set; }
public string Vaegt
{
get
{
return vaegt;
}
set
{
this.vaegt = value;
RaisePropertyChanged("Vaegt");
}
}
private string transportType { get; set; }
public string TransportType
{
get
{
return transportType;
}
set
{
this.transportType = value;
RaisePropertyChanged("TransportType");
}
}
private int transportTypeID { get; set; }
public int TransportTypeID
{
get
{
return transportTypeID;
}
set
{
this.transportTypeID = value;
RaisePropertyChanged("TransportTypeID");
}
}
private string pickerfraktion { get; set; }
public string PickerFraktion
{
get
{
return pickerfraktion;
}
set
{
this.pickerfraktion = value;
RaisePropertyChanged("PickerFraktion");
}
}
private int pickerfraktionID { get; set; }
public int PickerFraktionID
{
get
{
return pickerfraktionID;
}
set
{
this.pickerfraktionID = value;
RaisePropertyChanged("PickerFraktionID");
}
}
public HovedsideViewModel Parent { get; internal set; }
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(String Name)
{
if (PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(Name));
}
#endregion
}
}
有什么方法可以通过滑动而不是从行中获取数据 使用SelectionChanged事件?
即时通讯使用visual studio 2015社区,xamarin.Forms 2.3.3.168,prism v6.2.0 Syncfusion 14.4.0.15。主要是在Android版6.0.1和使用API 23的sony xperia z3上运行它我也在虚拟iphone 6s和IOS 10.0上使用它进行测试
非常感谢任何帮助
答案 0 :(得分:0)
您可以通过处理RowData
事件来获取SwipeEnded
。 SwipeEndedEventArgs
有RowData
。请参考下面的代码示例。
private void dataGrid_SwipeEnded(object sender, SwipeEndedEventArgs e)
{
var rowdata = e.RowData;
}
答案 1 :(得分:0)
问题是您需要创建一个转换器,以将事件args放入视图模型。如果您查看here的源代码,您会发现您需要一个转换器来获取值。我将SwipeEnded事件的EventToCommand行为更改为此:
<b:EventToCommand
EventName="SwipeEnded"
Command="{Binding SwipeCommand}"
EventArgsConverter="{StaticResource swipeEndedEventArgsConverter}" />
然后使用这样的转换器:
public class SwipeEndedEventArgsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var itemSwipeEndedEventArgs = value as SwipeEndedEventArgs;
if (itemSwipeEndedEventArgs == null)
{
throw new ArgumentException($"Expected value to be of type {nameof(SwipeEndedEventArgs)}", nameof(value));
}
return itemSwipeEndedEventArgs;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
请记住将其添加到资源字典中,以便您可以在XAML中使用它,例如,从内容页面资源中使用它:
<ContentView.Resources>
<ResourceDictionary>
<converters:SwipeEndedEventArgsConverter x:Key="swipeEndedEventArgsConverter" />
</ResourceDictionary>
</ContentView.Resources>
并将命名空间也添加到xaml:
xmlns:converters="clr-namespace:Project.Converters;assembly=Project"