public override void Load(Uri targetUri, Uri currentUri) { //tamper protection Boolean isMatchRequired = targetUri.OriginalString.Contains(Constants.ADDNEW) && targetUri.OriginalString.Contains(Constants.TRACKING_KEY); try { PersitableScreen viewmodel; _conductor.ViewModels.TryGetValue(targetUri.OriginalString, out viewmodel); if (isMatchRequired && viewmodel == null) { throw new InvalidOperationException("Uri missing from cache"); } if (viewmodel != null) { Object result = viewmodel.GetView(); if (result is Uri) base.Complete((Uri)result); else base.Complete(result); } else { _result = this.Loader.BeginLoad(targetUri, currentUri, (res) => { try { LoadResult loadResult = this.Loader.EndLoad(res); if (loadResult.RedirectUri != null) base.Complete(loadResult.RedirectUri); else { DependencyObject content = loadResult.LoadedContent as DependencyObject; if (content != null) { String currentOriginalString = null; if (currentUri == null || String.IsNullOrWhiteSpace(currentUri.OriginalString)) { currentOriginalString = Constants.HOME_URI_STRING; } else { currentOriginalString = currentUri.OriginalString; } String targetUriString = targetUri.OriginalString; String trackingKey = String.Empty; if (targetUri.OriginalString.EndsWith(Constants.ADD_NEW_QUERYSTRING)) { trackingKey = Guid.NewGuid().ToString(); targetUriString = String.Concat(targetUriString.Replace(Constants.ADD_NEW_QUERYSTRING, String.Empty), Constants.TRACKING_KEY_QUERYSTRING, trackingKey); } ViewLocator.InitializeComponent(content); var vm = ViewModelLocator.LocateForView(content); if (vm == null) { // to avoid shellviewmodel to be set on cild screens datacontext property vm = new PersitableScreen(); } viewmodel = vm as PersitableScreen; if (viewmodel != null) { viewmodel.ViewType = content.GetType(); viewmodel.UriString = targetUriString; viewmodel.ParentUriOriginalString = currentOriginalString; viewmodel.TrackingKey = trackingKey; // TODO: Add query string Uri auri = new Uri("dummy://" + targetUriString, UriKind.Absolute); string query = auri.Query; // _conductor.NavigationService.TryInjectQueryString(viewmodel,content); } // dont rebind when you're bound to yourself if (vm != content) ViewModelBinder.Bind(vm, content, null); base.Complete(content); } else { throw new InvalidOperationException("LoadedContent was null or not a DependencyObject"); } return; } } catch (Exception e) { base.Error(e); return; } }, null); } } catch (Exception e) { base.Error(e); return; } }
public override void Load(Uri targetUri, Uri currentUri) { //tamper protection Boolean isMatchRequired = targetUri.OriginalString.Contains(Constants.ADDNEW) && targetUri.OriginalString.Contains(Constants.TRACKING_KEY); try { PersitableScreen viewmodel; _conductor.ViewModels.TryGetValue(targetUri.OriginalString, out viewmodel); if (isMatchRequired && viewmodel == null) { throw new InvalidOperationException("Uri missing from cache"); } if (viewmodel != null) { Object result = viewmodel.GetView(); if (result is Uri) { base.Complete((Uri)result); } else { base.Complete(result); } } else { _result = this.Loader.BeginLoad(targetUri, currentUri, (res) => { try { LoadResult loadResult = this.Loader.EndLoad(res); if (loadResult.RedirectUri != null) { base.Complete(loadResult.RedirectUri); } else { DependencyObject content = loadResult.LoadedContent as DependencyObject; if (content != null) { String currentOriginalString = null; if (currentUri == null || String.IsNullOrWhiteSpace(currentUri.OriginalString)) { currentOriginalString = Constants.HOME_URI_STRING; } else { currentOriginalString = currentUri.OriginalString; } String targetUriString = targetUri.OriginalString; String trackingKey = String.Empty; if (targetUri.OriginalString.EndsWith(Constants.ADD_NEW_QUERYSTRING)) { trackingKey = Guid.NewGuid().ToString(); targetUriString = String.Concat(targetUriString.Replace(Constants.ADD_NEW_QUERYSTRING, String.Empty), Constants.TRACKING_KEY_QUERYSTRING, trackingKey); } ViewLocator.InitializeComponent(content); var vm = ViewModelLocator.LocateForView(content); if (vm == null) { // to avoid shellviewmodel to be set on cild screens datacontext property vm = new PersitableScreen(); } viewmodel = vm as PersitableScreen; if (viewmodel != null) { viewmodel.ViewType = content.GetType(); viewmodel.UriString = targetUriString; viewmodel.ParentUriOriginalString = currentOriginalString; viewmodel.TrackingKey = trackingKey; // TODO: Add query string Uri auri = new Uri("dummy://" + targetUriString, UriKind.Absolute); string query = auri.Query; // _conductor.NavigationService.TryInjectQueryString(viewmodel,content); } // dont rebind when you're bound to yourself if (vm != content) { ViewModelBinder.Bind(vm, content, null); } base.Complete(content); } else { throw new InvalidOperationException("LoadedContent was null or not a DependencyObject"); } return; } } catch (Exception e) { base.Error(e); return; } }, null); } } catch (Exception e) { base.Error(e); return; } }