This project is read-only.

StyleMVVM 3.1.5 Windows Phone 8 Navigation with object

May 14, 2014 at 11:04 PM
So maybe this is being improved with 4.0 but I don't see a clear way to pass an object during navigation like there is in Windows 8. The PageViewModel even has a NavigationParamater object but this doesn't seemed to be used in that the StyleNavigationEventArgs do no contain the object. Is there a better way to do this? Right now I'm thinking we are just going to need to use the container to get the target VM before navigation and set any data needed there, then navigate, or something along those lines. I just wondered if there is a cleaner way or a proper way to use the NavigationParamater.

May 15, 2014 at 12:41 AM
So if I remember correctly (it's been a bit since I wrote that code) Windows Phone navigation is different than the Windows 8 & WPF navigation. I will put it on my list of things to address for version 4.0 but I do remember it being different and not working the same.

You could possible share a data model and send data between the ViewModels that way. Ultimately if you have it working already using the container that may be your best option for the moment
May 15, 2014 at 6:05 PM
So went about solving this a little more conventionally.
         public static void Navigate<T>(this INavigationService navigationService, object paramater = null)
            var viewType = typeof(T);
            var viewModelName = viewType.FullName.Replace("Views", "ViewModels") + "VM";
            var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
            var viewModelTypeName = String.Format(CultureInfo.InvariantCulture, "{0}, {1}", viewModelName, viewAssemblyName);

            var viewModelType = Type.GetType(viewModelName);
            var viewModel = Bootstrapper.Instance.Container.LocateByType(new StyleMVVM.Data.TypeWrapper(viewModelType)) as BaseVM;
            if(viewModel != null)
                var targetURI = new Uri(viewModel.BaseURI + viewType.Name + ".xaml", UriKind.Relative);
                viewModel.NavigationParameter = paramater;
The one issue I'm having with this is [Singleton] does not appear to be working at least how I thought it would. On BaseVM class which all my VMs inhert from (it itself is PageViewModel) I have [Export(typeof(BaseVM))] and [Singleton] but this was not behaving as singleton, after navigation NavigationParamater would be null, adding the export/singleton to the child VM worked as expected. I assume this is because I'm using the container to locate by that type rather than BaseVM. So I guess I could return all BaseVMs and pull it form that if that is the case.
May 15, 2014 at 6:11 PM
Hi KJee85

I think I see what's happening and why you aren't getting the desired behavior you are looking for.

Try changing the export attribute to just [Export] rather than the [Export(typeof(BaseVM))]. I think what's happening is all the ViewModel types are being registered as BaseVM type. Then when you locate the real concrete type the container doesn't know how to find it so it constructs a transient on the fly for you (the container will automatically create concrete types for you even if they aren't registered).

Give that a whirl and let me know.

May 15, 2014 at 6:45 PM
Didn't seem to fix it, for now I'm just doing this:
            var baseVMs = Bootstrapper.Instance.Container.LocateAllByType(new StyleMVVM.Data.TypeWrapper(typeof(BaseVM)));
            BaseVM viewModel = null;
            foreach (BaseVM vm in baseVMs)
                if (vm.GetType().Equals(viewModelType))
                    viewModel = vm;
but it would be nice to not have to iterate all of them every navigation. Thanks. Let me know if you think of anything else.
May 15, 2014 at 6:53 PM
My mistake, it works changing to just export but then you need to just to a Locate(viewModelType.Name) I was still doing by type.

Works great!
May 15, 2014 at 8:59 PM
That's great to hear.