private void navigationHelper_LoadState(object sender, LoadStateEventArgs e) { // TODO: 将可绑定项集合分配到 this.DefaultViewModel["Items"] card = e.NavigationParameter as MenuCard; if(card != null) { this.DefaultViewModel["Items"] = card.MenuItems; } }
public MenuCardData(MenuCard menuCard) { this.Title = menuCard.Title; this.Description = menuCard.Description; this.ImagePath = menuCard.ImagePath; MenuItems = new List<MenuItemData>(); foreach (var item in menuCard.MenuItems) { MenuItems.Add(new MenuItemData(item)); } }
// 写数据到文件 public async Task WriteMenuCardAsync(MenuCard menuCard) { StorageFolder folder = ApplicationData.Current.RoamingFolder; if (menuCard.IsDirty) { StorageFile storageFile = await folder.CreateFileAsync( string.Format("MenuCards{0}.xml", menuCard.Title), CreationCollisionOption.ReplaceExisting); await WriteMenuCardToFileAsync(menuCard, storageFile); menuCard.ClearDirty(); } }
public MenuCard ToMenuCard() { var menuCard = new MenuCard { Title = this.Title, Description = this.Description, ImagePath = this.ImagePath }; foreach (MenuItemData item in this.MenuItems) { menuCard.MenuItems.Add(item.ToMenuItem()); } menuCard.ClearDirty(); return menuCard; }
public async Task WriteMenuCardToFileAsync(MenuCard menuCard, StorageFile storageFile) { var menuCardData = new MenuCardData(menuCard); var knownTypes = new Type[] { typeof(MenuItemData) }; var cardStream = new MemoryStream(); var serializer = new DataContractSerializer(typeof(MenuCardData), knownTypes); serializer.WriteObject(cardStream, menuCardData); using (Stream fileStream = await storageFile.OpenStreamForWriteAsync()) { cardStream.Seek(0, SeekOrigin.Begin); await cardStream.CopyToAsync(fileStream); await fileStream.FlushAsync(); } }
public static ObservableCollection<MenuCard> GetSampleMenuCards() { Uri baseUri = new Uri("ms-appx:///"); var cards = new ObservableCollection<MenuCard>(); var card1 = new MenuCard { Title = "Breakfast" }; card1.MenuItems.Add(new MenuItem { Text = "Spezialfrühstück", Price = 5.4, MenuCard = card1 }); card1.MenuItems.Add(new MenuItem { Text = "Wiener Frühstück", Price = 4.4, MenuCard = card1 }); card1.MenuItems.Add(new MenuItem { Text = "Schinken mit 3 Eiern", Price = 4.4, MenuCard = card1 }); card1.ImagePath = string.Format("{0}{1}", baseUri, "Assets/Breakfast.jpg"); cards.Add(card1); var card2 = new MenuCard { Title = "Salad" }; card2.MenuItems.Add(new MenuItem { Text = "Mariniertes Gemüse mit Schafkäsebällchen und Rohschinken", Price = 8.60, MenuCard = card2 }); card2.MenuItems.Add(new MenuItem { Text = "Gebratener Schafkäse im Speckhemd auf knackigem Salat", Price = 7.80, MenuCard = card2 }); card2.MenuItems.Add(new MenuItem { Text = "Backhendlsalat", Price = 7.20, MenuCard = card2 }); card2.ImagePath = string.Format("{0}{1}", baseUri, "Assets/Salads.jpg"); cards.Add(card2); var card3 = new MenuCard { Title = "Sweets" }; card3.MenuItems.Add(new MenuItem { Text = "Marillenknödel mit Zimtbrösel (Stück)", Price = 2.30, MenuCard = card3 }); card3.MenuItems.Add(new MenuItem { Text = "Apfelstrudel", Price = 2.60, MenuCard = card3 }); card3.MenuItems.Add(new MenuItem { Text = "Marmelade-Palatschinken (2 Stück)", Price = 3.40, MenuCard = card3 }); card3.ImagePath = string.Format("{0}{1}", baseUri, "Assets/Sweets.png"); cards.Add(card3); return cards; }
/// <summary> /// 保留与此页关联的状态,以防挂起应用程序或 /// 从导航缓存中放弃此页。 值必须符合 /// <see cref="SuspensionManager.SessionState"/> 的序列化要求。 /// </summary> ///<param name="sender">事件的来源;通常为 <see cref="NavigationHelper"/></param> ///<param name="e">提供要使用可序列化状态填充的空字典 ///的事件数据。</param> /// // 退出页面时会调用 保存添加的Card private async void navigationHelper_SaveState(object sender, SaveStateEventArgs e) { var mc = new MenuCard { Title = info.Title, Description = info.Description, Image = info.Image, ImagePath = info.ImageFileName }; MenuCardRepository.Instance.Cards.Add(mc); var storage = new MenuCardStorage(); await storage.WriteMenuCardsAsync(MenuCardRepository.Instance.Cards.ToList()); }