Permalink
Browse files

adding items

  • Loading branch information...
saraford committed Dec 24, 2017
1 parent 4d1ecd4 commit 9ca3ffe18d5204eb8ca53ab0655e1b7c354f625c
@@ -11,17 +11,33 @@ public class ItemDetailsViewModel : BaseViewModel
public Fabric Fabric { get; private set; }
private INavigation Navigation;
public ICommand DoneEditingCommand { get; private set; }
private bool isNew = false;
public ItemDetailsViewModel(INavigation Navigation, Fabric fabric)
{
Title = "Hello from Item " + fabric.Name;
isNew = fabric == null;
if (isNew)
{
this.Fabric = new Fabric(Name: "", Seconds: 60);
Title = "Hello from new item";
}
else
{
this.Fabric = fabric;
Title = "Hello from Item " + Fabric.Name;
}
this.Navigation = Navigation;
this.Fabric = fabric;
DoneEditingCommand = new Command(DoneEditing);
}
private async void DoneEditing()
{
if (isNew) {
MessagingCenter.Send<ItemDetailsViewModel, Fabric>(this, "added", this.Fabric);
}
await Navigation.PopAsync();
}
}
@@ -4,6 +4,8 @@
using Xamarin.Forms;
using ListViewInTabbedControl.Models;
using System.Collections.Generic;
using System.Windows.Input;
using ListViewInTabbedControl.Views;
namespace ListViewInTabbedControl.ViewModels
{
@@ -12,6 +14,7 @@ public class PageOneViewModel : BaseViewModel
// this ObservableCollection is *only* for the View
// do *not* use this as the "source of truth" for fabrics
public ObservableCollection<Fabric> OCFabrics { get; set; }
public ICommand AddFabricCommand { get; private set; }
private INavigation Navigation;
@@ -25,7 +28,21 @@ public PageOneViewModel(INavigation Navigation)
OCFabrics.Add(new Fabric("fabric3", 5));
Title = "Page One from VM";
this.Navigation = Navigation;
this.Navigation = Navigation;
AddFabricCommand = new Command(AddFabric);
MessagingCenter.Subscribe<ItemDetailsViewModel, Fabric>(this, "added", (sender, arg) =>
{
OCFabrics.Add(arg);
});
}
private async void AddFabric()
{
Fabric fabric = null;
await this.Navigation.PushAsync(new ItemDetailsPage(fabric));
}
}
}
@@ -23,6 +23,11 @@
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Footer>
<ContentView>
<Button Text="Add Fabric" FontSize="Large" Command="{Binding AddFabricCommand}" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
</ContentView>
</ListView.Footer>
</ListView>
</StackLayout>
</ContentPage.Content>

0 comments on commit 9ca3ffe

Please sign in to comment.