56214

Passing Parameter From Main to Detail in MVVMCross

Question:

I am trying to pass the selected item from the list to the detail view, but myitem is null in the DetailViewmodel even though it is not in the MyViewModel.

<strong>MyViewModel.cs</strong>

public virtual ICommand ItemSelected { get { return new MvxCommand<MyViewModel>(item =>{SelectedItem = item;}); } } public MyViewModel SelectedItem { get { return _selectedItem; } set { _selectedItem = value; // myItem is NOT null here!!! ShowViewModel<MyDetailViewModel>(new { date = Date, myItem = _selectedItem }); RaisePropertyChanged(() => SelectedItem); } }

<strong>MyDetailViewModel.cs</strong>

public class MyDetailViewModel: MvxViewModel { private MyViewModel _myItem; public void Init(DateTime date, MyViewModel myItem = null) { // myItem is NULL here!!! _myItem = myItem; } }

Answer1:

You can use a parameter object, because you can only pass one parameter. I usually crate a nested class Parameter for this.

public class MyDetailViewModel: MvxViewModel { private MyViewModel _myItem; public class Parameter { public DateTime Date {get; set; } public string Name {get; set;} } public void Init(Parameter param) { Name = param.Name; } }

and show the viewmodel like:

ShowViewModel<MyDetailViewModel>(new MyDetailViewModel.Parameter { Date = Date, Name = _selectedItem.Name }); <h2>But be aware!</h2>

The paramters cannot be complex due certain platform issues. You might have to pass only the Id of your Item within the Parameter object and then load MyItem in your Init function. Or you pass only a string and use serialization: <a href="https://stackoverflow.com/a/19059938/1489968" rel="nofollow">https://stackoverflow.com/a/19059938/1489968</a>

Answer2:

myItem is null because if you pass typed parameter to Init it should be the only parameter you pass. According to <a href="https://github.com/MvvmCross/MvvmCross/wiki/ViewModel--to-ViewModel-navigation" rel="nofollow">MvvmCross ViewModel Creation documentation</a>:

<blockquote>

Init() can come in several flavors:.

<ul><li>individual simply-Typed parameters</li> <li>a single Typed parameter object with simply-Typed properties</li> <li>as InitFromBundle() with an IMvxBundle parameter - this last flavor is always supported via the IMvxViewModel interface.</li> </ul></blockquote>

Recommend

  • Setting binding in WPF control correctly
  • How can I get the checked checkbox values using MVVM?
  • How to Raise Property Changed in Derived Classes?
  • Why does binding setup behave differently in .NET 4 vs .NET 3.5
  • Why does binding setup behave differently in .NET 4 vs .NET 3.5
  • MVVM Creating the ViewModel
  • How generate Graphs using modern UI Charts according to user inputs
  • UNABLE to POST data to WCF from Android
  • User.Identity.Name - which assembly reference brings that into your project?
  • Using Route Attributes In ASP.NET MVC 5 With Areas
  • WPF8/C# - Binding Data To Grid
  • Saving image to sd with current date and time in name doesn't work
  • android google indoor map
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • Combining two different ActiveRecord collections into one
  • IE7 and TinyMCE with Plone
  • Implementation of State Monad
  • Stop Bash Script if Hive Fails
  • custom UITableViewCell with image for highlighting
  • JQuery Internet Explorer and ajaxstop
  • Why querying a date BC is changed to AD in Java?
  • Row Count Is Returning the incorrect number using RaptureXML
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • Q promise. Difference between .when and .then
  • Illegal mix of collations for operation for date/time comparison
  • jQuery show() function is not executed in Safari if submit handler returns true
  • Problems to linebreak with an int in JLabel
  • Sony Xperia Z Tablet not found by adb
  • Check if a string to interpolate provides expected placeholders
  • javascript inside java/jsp code
  • Sending data from AppleScript to FileMaker records
  • Release, debug version and Authorization Google?
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • Hits per day in Google Big Query
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory