//TODO For some reason, even though it shows up in the debugger as having a BindingContext, the line, "(MenuItem)sender.BindingContext" fails //to compile. Because of this, even though both a menuitem and a button have a BindingContext, I can't reference it with a single line //of code. So, I have these two methods that do exactly the same thing. Is there a better way? public void OnToggleCartMenuClicked(object sender, EventArgs args) { MenuItem m = (MenuItem)sender; MyPlant p = (MyPlant)m.BindingContext; AppData appData = (AppData)BindingContext; appData.MasterViewModel.PlantList.ToggleStatus(p); }
public void OnToggleCartButtonClicked(object sender, EventArgs args) { Button b = (Button)sender; MyPlant p = (MyPlant)b.BindingContext; AppData appData = (AppData)BindingContext; appData.MasterViewModel.PlantList.ToggleStatus(p); }
//Attempt to toggle the cart status of the item passed in public bool ToggleStatus(MyPlant p) { //First, find the plant that matches the one passed in (this assumes each plant's name is spelled consistently, that //the name is unique, and that it exists exactly once). And then, toggle the status of that plant. bool value = allPlants.Find(x => x.Plant.Name == p.Plant.Name).TogglePlantCartStatus(); //Now, regenerate the list so that it reflects the right set of plants and everybody knows it has changed RefreshShoppingListPlants(); return(value); }
// When the "More Info" link is tapped, then open a new pop-over window and load the URL of the selected plant into this async void OnMoreInfoTapped(object sender, EventArgs e) { Label label = (Label)sender; MyPlant thePlant = (MyPlant)label.BindingContext; if (thePlant.Plant.URL != null) { await Navigation.PushAsync(new MoreInfoPage(thePlant.Plant.URL)); } else { //TODO: what to do if there is no URL for the plant } }
public App() { InitializeComponent(); // The Application ResourceDictionary is available in Xamarin.Forms 1.3 and later if (Application.Current.Resources == null) { Application.Current.Resources = new ResourceDictionary(); } //In theory, this should instantiate our view model and make it accessible to the other pages AppData = new AppData(); // Load previous AppData if it exists. if (Properties.ContainsKey("appData")) { //Load the list of the scientific names of the plants the user wants List <CartItem> shoppingList = AppData.Deserialize((string)Properties["appData"]); if (shoppingList != null) { //rehydrate the list foreach (CartItem aPlant in shoppingList) { //Check that the plant exists before adding it to the cart. Store this in a variable so we don't do the query twice. The query //returns a reference to the actual object, not a copy of it, so this works as expected. MyPlant p = this.AppData.MasterViewModel.PlantList.AllPlants.Find(x => x.Plant.ScientificName == aPlant.Name); if (p != null) { p.InCart = true; p.Count = aPlant.Count; } } this.AppData.MasterViewModel.PlantList.RefreshShoppingListPlants(); } }//end if Properties MainPage = new NavigationPage(new TopLevelPage()); }