protected override void OnNavigatedTo(NavigationEventArgs e) { if (this.State != null && this.State.ContainsKey("PictureViewModelData")) { _pictureData = this.State["PictureViewModelData"] as IEnumerable <Tuple <string, string> >; if (_pictureData != null) { _pictureViewModel = new LinkedPictureViewModel { Pictures = _pictureData.Select(tpl => new LinkedPictureViewModel.LinkedPicture { Title = tpl.Item1, ImageSource = tpl.Item2 }) }; } } else if (this.NavigationContext.QueryString["data"] != null) { var unescapedData = HttpUtility.UrlDecode(this.NavigationContext.QueryString["data"]); var deserializedObject = JsonConvert.DeserializeObject <IEnumerable <Tuple <string, string> > >(unescapedData); if (deserializedObject != null) { _pictureViewModel = new LinkedPictureViewModel { Pictures = deserializedObject.Select(tpl => new LinkedPictureViewModel.LinkedPicture { Title = tpl.Item1, ImageSource = tpl.Item2 }) }; _pictureData = deserializedObject; } } if (DataContext == null) { DataContext = _pictureViewModel; } }
protected override void OnNavigatedTo(NavigationEventArgs e) { if (this.State != null && this.State.ContainsKey("PictureViewModelData")) { _pictureData = this.State["PictureViewModelData"] as string; if (_pictureData != null) { var deserializedObject = JsonConvert.DeserializeObject<Tuple<string, IEnumerable<Tuple<string, string>>, string>>(_pictureData); if (deserializedObject != null) { _pictureViewModel = new LinkedPictureViewModel { LinkTitle = deserializedObject.Item1.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace("'", "'").Trim(), LinkId = deserializedObject.Item3, Pictures = deserializedObject.Item2.Select(tpl => new LinkedPictureViewModel.LinkedPicture { Title = tpl.Item1.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace("'", "'").Trim(), ImageSource = tpl.Item2, Url = tpl.Item2 }) }; } } } else if (this.NavigationContext.QueryString["data"] != null) { var unescapedData = HttpUtility.UrlDecode(this.NavigationContext.QueryString["data"]); var deserializedObject = JsonConvert.DeserializeObject<Tuple<string, IEnumerable<Tuple<string, string>>, string>>(unescapedData); if (deserializedObject != null) { _pictureViewModel = new LinkedPictureViewModel { LinkTitle = deserializedObject.Item1.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace("'", "'").Trim(), LinkId = deserializedObject.Item3, Pictures = deserializedObject.Item2.Select(tpl => new LinkedPictureViewModel.LinkedPicture { Title = tpl.Item1.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace("'", "'").Trim(), ImageSource = tpl.Item2, Url = tpl.Item2 }) }; _pictureData = unescapedData; } } if (DataContext == null || e == null) { DataContext = _pictureViewModel; } _viewModelContextService.PushViewModelContext(DataContext as ViewModelBase); _smartOfflineService.NavigatedToView(typeof(LinkedPictureView), e == null ? true : e.NavigationMode == NavigationMode.New); }
/// <summary> /// Populates the page with content passed during navigation. Any saved state is also /// provided when recreating a page from a prior session. /// </summary> /// <param name="navigationParameter">The parameter value passed to /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested. /// </param> /// <param name="pageState">A dictionary of state preserved by this page during an earlier /// session. This will be null the first time a page is visited.</param> protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState) { var rawData = navigationParameter as Tuple<string, IEnumerable<Tuple<string, string>>, string>; var pictureData = rawData.Item2; if (pictureData == null && pageState != null && pageState.ContainsKey("NavagationData")) { var data = pageState["NavagationData"] as Tuple<string, IEnumerable<Tuple<string, string>>, string>; _navData = pictureData = data.Item2; } if (pictureData != null) { _navData = pictureData; var pictureTasks = pictureData.Select(async (tpl) => { var imageBytes = await ServiceLocator.Current.GetInstance<IImagesService>().ImageBytesFromUrl(tpl.Item2); if (imageBytes != null) { var renderer = GifRenderer.CreateGifRenderer(imageBytes); if (renderer != null) { renderer.Visible = true; return new LinkedPictureViewModel.LinkedPicture { Title = tpl.Item1.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace("'", "'").Trim(), ImageSource = renderer, Url = tpl.Item2 }; } else return new LinkedPictureViewModel.LinkedPicture { Title = tpl.Item1.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace("'", "'").Trim(), ImageSource = tpl.Item2, Url = tpl.Item2 }; } else return null; }) .Where(val => val != null) .ToArray(); _pictureViewModel = new LinkedPictureViewModel { Pictures = await Task.WhenAll(pictureTasks) }; } DataContext = _pictureViewModel; }
/// <summary> /// Populates the page with content passed during navigation. Any saved state is also /// provided when recreating a page from a prior session. /// </summary> /// <param name="navigationParameter">The parameter value passed to /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested. /// </param> /// <param name="pageState">A dictionary of state preserved by this page during an earlier /// session. This will be null the first time a page is visited.</param> protected override async void LoadState(Object navigationParameter, Dictionary <String, Object> pageState) { var pictureData = navigationParameter as IEnumerable <Tuple <string, string> >; if (pictureData == null && pageState != null && pageState.ContainsKey("NavagationData")) { _navData = pictureData = pageState["NavagationData"] as IEnumerable <Tuple <string, string> >; } if (pictureData != null) { _navData = pictureData; var pictureTasks = pictureData.Select(async(tpl) => { var renderer = GifRenderer.CreateGifRenderer(await DownloadImageFromWebsiteAsync(tpl.Item2)); if (renderer != null) { renderer.Visible = true; return(new LinkedPictureViewModel.LinkedPicture { Title = tpl.Item1, ImageSource = renderer }); } else { return new LinkedPictureViewModel.LinkedPicture { Title = tpl.Item1, ImageSource = tpl.Item2 } }; }) .ToArray(); _pictureViewModel = new LinkedPictureViewModel { Pictures = await Task.WhenAll(pictureTasks) }; } DataContext = _pictureViewModel; }
private static Tuple<string, IEnumerable<Tuple<string, string>>, string> MakeSerializable(LinkedPictureViewModel vm) { return Tuple.Create(vm.LinkTitle, vm.Pictures.Select(linkedPicture => Tuple.Create(linkedPicture.Title, linkedPicture.Url)), vm.LinkId); }