Cześć chciałbym użyć commond do usuwania i wzorca MVVM
using System;
using System.Collections.Generic;
namespace TestApp
{
public class GuidModel
{
public Guid Guid { get; set; }
public string Text
{
get
{
return Guid.ToString();
}
set
{
if (Guid.ToString() != value)
{
Guid = Guid.Parse(value);
}
}
}
public GuidModel(Guid guid)
{
Guid = guid;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<d:ExtendedContentPage xmlns:d="clr-namespace:TestApp;assembly=TestApp"
xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestApp.GuidPage" BackgroundColor="Gray" >
<ContentPage.Content>
<Grid>
<ListView x:Name="GuidsView" ItemsSource="{Binding GuidItems}" SelectedItem="{Binding GuidItem, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Text}">
<TextCell.ContextActions>
<MenuItem Clicked="OnDelete" CommandParameter="{Binding .}" Text="Delete" />
</TextCell.ContextActions>
</TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</ContentPage.Content>
</d:ExtendedContentPage>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Xamarin.Forms;
namespace TestApp
{
public partial class GuidPage : ExtendedContentPage
{
private readonly GuidViewModel _viewModel = null;
public GuidPage()
{
InitializeComponent();
if (_viewModel == null)
{
_viewModel = new GuidViewModel(Navigation);
}
BindingContext = _viewModel;
}
private void OnDelete(object sender, EventArgs e)
{
var mi = ((MenuItem)sender);
GuidModel guidToRemove = (GuidModel)mi.CommandParameter;
_viewModel.GuidItemDelete(guidToRemove);
}
}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using Xamarin.Forms;
namespace TestApp
{
public class GuidViewModel : BaseViewModel
{
private ObservableCollection<GuidModel> _guidItems = null;
public ObservableCollection<GuidModel> GuidItems
{
get
{
return _guidItems;
}
set
{
if (_guidItems != value)
{
_guidItems = value;
OnPropertyChanged();
}
}
}
private GuidModel _guidItem = null;
public GuidModel GuidItem
{
get
{
return _guidItem;
}
set
{
if (_guidItem != value)
{
_guidItem = value;
OnPropertyChanged();
}
}
}
public GuidViewModel(INavigation navigation) : base(navigation)
{
GuidItems = new ObservableCollection<GuidModel>();
PrepareGuidList();
}
private void PrepareGuidList()
{
for (int i = 0; i < 100; i++)
{
_guidItems.Add(new GuidModel(Guid.NewGuid()));
}
}
public void GuidItemDelete(GuidModel guidToRemove)
{
if (guidToRemove != null)
{
_guidItems.Remove(guidToRemove);
}
}
public override void RemoveHandlers()
{
throw new NotImplementedException();
}
}
}
public abstract class BaseViewModel : BaseNotifyPropertyChanged
{
#region Events
public delegate void NavigateFromDetailPageHandler(ContentPage page);
public event NavigateFromDetailPageHandler NavigateFromDetailPageEvent;
private void OnNavigateFromDetailPage(ContentPage page)
{
if (NavigateFromDetailPageEvent != null) NavigateFromDetailPageEvent(page);
}
#endregion
#region Fields
private INavigation _navigation;
#endregion
#region Properties
private PageManager PageManagerInstance
{
get
{
return SingletonsManager.Instance.PageManagerInstance;
}
}
#endregion
#region Methods
protected virtual void OnNavigatedFrom() { }
public BaseViewModel(INavigation navigation)
{
_navigation = navigation;
}
public async Task NavigateToPage(ContentPage page, bool detailPage = false, bool removePrevious = false)
{
PageManagerInstance.CurrentPage = page;
if (detailPage)
{
OnNavigateFromDetailPage(page);
}
else
{
await _navigation.PushAsync(page);
}
if (removePrevious)
{
int navigationPageCount = _navigation.NavigationStack.Count;
if (navigationPageCount - 2 >= 0)
{
_navigation.RemovePage(_navigation.NavigationStack[navigationPageCount - 2]);
}
}
}
public abstract void RemoveHandlers();
#endregion
}