Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9141845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:41:57+00:00 2026-06-17T09:41:57+00:00

I´ve got 2 ViewModels ( ConfigurationViewModel and EditConfigurationViewModel ). In the ConfigurationViewModel I’ve got

  • 0

I´ve got 2 ViewModels (ConfigurationViewModel and EditConfigurationViewModel). In the ConfigurationViewModel I’ve got the following code:

    public ConfigurationViewModel()
    {
        NewConfigCommand = new MvxRelayCommand(DoNewConfig);
        EditConfigCommand = new MvxRelayCommand<ConfigurationSet>(DoEditConfig);
    }

    private void DoNewConfig()
    {
        this.RequestNavigate<EditConfigurationViewModel>();
    }

    private void DoEditConfig(ConfigurationSet config)
    {
        this.RequestNavigate<EditConfigurationViewModel>(new { id = config.Id.ToString() });
    }

In the EditConfigurationViewModel I’ve got the following code:

    public EditConfigurationViewModel()
    {
        Configuration = new ConfigurationSet();
    }

    public EditConfigurationViewModel(string id)
    {
        Configuration = ConfigDataStore.GetConfiguration(Guid.Parse(id));
    }

What I want to achieve is something very simple… In the ConfigurationViewModel when the NewConfigCommand is fired, I want to navigate to the EditConfigurationViewModel, and use the parameterless constructor. When the EditConfigCommand is fired I want to use the constructor that receives a string.

The problem with this code is that no matter what command is fired, the parameterless constructor is allways used and the code never reaches the other constructor.

I did some experiments, by removing the parameterless constructor, and the result was that the other constructor is called and I get the expected result for the EditConfigurationCommand, but if I try to fire the NewConfigurationCommand an exception is throw due too the inesxistence of a parameterless constructor (so far so good).

Unfortunately, at this moment I don’t have VS2010 installed, so I’m not able to debug through PCL code… I’ve done some “eye debug” and found this class MvxViewModelLocator. I think the problem is somewhere here. Maybe in the DoLoad method when it tries to get the MethodInfo…

At this point I just wanted to know if I’m doing something wrong or if this is the expected result. Meanwhile I think I’ll take a chance on installing VS2010 and pray that it won´t break anything…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:41:58+00:00Added an answer on June 17, 2026 at 9:41 am

    On the PCL debugging issue, why not just add a Win8 or WP7/8 UI – then you can debug through the PCL code…


    On the main question – about how to use multiple constructors… I’d suggest you don’t.

    For me, edit and new are two different views and two different viewmodels – they may share common properties and common layout – but this can be achieved using inheritance, using UserControls, using include axml, etc.

    For an example of what I generally use for new and edit see https://github.com/slodge/MvvmCross/tree/vnext/Sample%20-%20CustomerManagement/CustomerManagement/CustomerManagement/ViewModels


    If you do insist on carry on using one viewmodel, then you could consider using a ‘magic value’ for New – e.g. if Guid.Empty is passed then that means new?


    Alternatively, you could just drop your parameterless constructor and could add a default value to the second one:

    public EditConfigurationViewModel(string id = null)
    {
        Guid value;
        if (id == null || !Guid.TryParse(id, out value))
        {
            Configuration = new ConfigurationSet();
        }
        else
        {
            Configuration = ConfigDataStore.GetConfiguration(value);
        }
    }
    

    I think that would work?


    Finally, if none of that seems suitable to you, then you could consider overriding the ViewModel construction mechanism.

    To help with this, there’s a fairly detailed recent post on how to write your own default ViewModelLocator for MvvmCross – see http://slodge.blogspot.co.uk/2013/01/navigating-between-viewmodels-by-more.html

    Using this approach, you could create a much more custom navigation model – or if this is the only special view model, then I suspect you could create a default viewModelLocator like:

    public class MyViewModelLocator
        : MvxDefaultViewModelLocator
    {
        public override bool TryLoad(Type viewModelType, IDictionary<string, string> parameterValueLookup,
                                     out IMvxViewModel model)
        {
            if (viewModelType == typeof(EditConfigurationViewModel))
            {
                string id;
                if (parameterValueLookup.TryGetValue("id", out id))
                {
                    model = new EditConfigurationViewModel(id);
                }
                else
                {
                    model = new EditConfigurationViewModel();
                }
                return true;
            }
            return base.TryLoad(viewModelType, parameterValueLookup, IMvxViewModel model);
        }
    }
    

    and register that locator in App.cs using:

    protected override IMvxViewModelLocator CreateDefaultViewModelLocator()
    {
         return new MyViewModelLocator();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a View.xaml with the following set in Resources-section: <DataTemplate DataType={x:Type ViewModels:MyFirstViewModel}> <Views:MyFirstView
I have got the following Models: public class Car : BindableBase { private string
I've got a set of ViewModels that I'm binding to the ItemsSource property of
I've got a collection of ViewModels and want to bind a ListBox to them.
I've got an ItemsControl bound to a collection of extremely basic ViewModels, that each
I've currently got all of this mess at the top of my ViewModels which
Ok, i got a viewmodel as follows: public class PageViewModel { public Item Item
I've got view: @using (Html.BeginForm(ProcessRoute, Calculator)) { @Html.TextBox(CityArrival, null, new { @style = width:
I have got a collection of viewModels(InputViewModel) in an other viewModel(ScenarioManager). each InputviewModel has
I have a controller HomeController with the following action method : [HttpPost] public ActionResult

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.