Thursday, November 28, 2019

Xamarin.Forms Making Navigation object available in viewmodel

using App5.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace San.Ash.Mobile.Vic.School.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SchoolFilterSortListView : ContentPage
{
public SchoolFilterSortListView()
{
InitializeComponent();
BindingContext = new SchoolViewModel(Navigation);
}
}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Input;
using App5.Models;
using App5.Views;
using San.Ash.Mobile.Vic.School.Models;
using Xamarin.Forms;
using System.ComponentModel;
namespace App5.ViewModels
{
public class SchoolViewModel : INotifyPropertyChanged
{
readonly IList<School> source;
School selectedSchool;
int selectionCount = 1;
public ObservableCollection<School> Schools { get; private set; }
public IList<School> EmptySchools { get; private set; }
public string SelectedSchoolMessage { get; private set; }
public ICommand FilterCommand => new Command<string>(FilterItems);
public ICommand SchoolSelectionChangedCommand => new Command(SchoolSelectionChanged);
public INavigation Navigation { get; set; }
async void SchoolSelectionChanged()
{
//SelectedSchoolMessage = $"Selection {selectionCount}: {SelectedSchool.Name}";
//OnPropertyChanged("SelectedSchoolMessage");
//selectionCount++;
//var item = args.SelectedItem as Item;
//if (item == null)
// return;
var item = new Item();
item.Id = selectionCount.ToString();
item.Text = SelectedSchool.Name;
await Navigation.PushAsync(new ItemDetailPage(new ItemDetailViewModel(item)));
// Manually deselect item.
//ItemsListView.SelectedItem = null;
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}

No comments :