コード例 #1
0
        public ItemsViewModel()
        {
            Title            = "Browse";
            Notes            = new ObservableCollection <Note>();
            LoadNotesCommand = new Command(async() => await ExeculeLoadNotesCommand());

            // <Sender, ArgType>
            MessagingCenter.Subscribe <ItemDetailPage, Note>(this, "SaveNote", async(sender, note) =>
            {
                Notes.Add(note);
                await PluralSightDataStore.AddNoteAsync(note);
            });
        }
コード例 #2
0
ファイル: ItemsViewModel.cs プロジェクト: walkesh/xamarin
        public ItemsViewModel()
        {
            Title            = "Browse";
            Notes            = new ObservableCollection <Note>();
            LoadItemsCommand = new Command(async() => await ExecuteLoadItemsCommand());

            //MessagingCenter.Subscribe<type of the sender, type of the data you expect with the message>(reference to the class calling the subscribe method,
            // string identifying the message,
            // async lambda expression (reference to message sender, information sent with that message) => { work we want to get done when the message is sent});
            MessagingCenter.Subscribe <ItemDetailPage, Note>(this, "SaveNote",
                                                             async(sender, note) => {
                // note appear in list view - add note to note collection
                Notes.Add(note);     // note collection is an observable selection - autommatically updates list view

                //save this into the data store
                await PluralSightDataStore.AddNoteAsync(note);
            });

            //Handle Note Updates
            MessagingCenter.Subscribe <ItemDetailPage, Note>(this, "UpdateNote",
                                                             async(sender, note) =>
            {
                await PluralSightDataStore.UpdateNoteAsync(note);

                // modifying a memeber within an ObservableCollection does not autoatically refresh data binding
                // we must explicity repopulate the collection
                await ExecuteLoadItemsCommand();
            });

/*            MessagingCenter.Subscribe<NewItemPage, Item>(this, "AddItem", async (obj, item) =>
 *          {
 *              var newItem = item as Item;
 *              Items.Add(newItem);
 *              await DataStore.AddItemAsync(newItem);
 *          });*/
        }