Solved

C# - SOLID Question

Posted on 2016-09-24
6
51 Views
Last Modified: 2016-09-29
I'm working on a Xamarin App.  I'm using the MVVM pattern.  I have two view models.  I'm setting the title and both titles are exactly the same as displayed below.  My question is how would you separate this so the title is set once so it is not in two different classes?

    public class MainPageViewModel : BaseViewModel
    {
        public string MainTitle { get; set; }
        public ICommand ButtonCommand { get; set; }

        public MainPageViewModel()
        {
            this.MainTitle = "My Title";
        }
    }

Open in new window

0
Comment
Question by:CipherIS
6 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 41814120
The question is: Why do you have two view models?

Without further information: Setting the title should be done in your base view model class.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41814224
I'm creating a viewmodel for each View as they will both have different functionality.
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 41814229
You could create a base class for your common view model code:
public class MyAppViewModel : BaseViewModel
{
    public string MainTitle { get; set; }

    public MainPageViewModel()
    {
        this.MainTitle = "My Title";
    }
}

Open in new window


and then use It in your view model:
public class MainPageViewModel : MyAppViewModel
{
    public ICommand ButtonCommand { get; set; }

    public MainPageViewModel() : base()
    {
    }
}

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 34

Expert Comment

by:sarabande
ID: 41814783
Setting the title should be done in your base view model class.

ste5an is right. a common base class for both the views is the right place to set the title if it is the same for both views.

i don't see a reason to creating a further baseclass.

Sara
0
 
LVL 1

Author Comment

by:CipherIS
ID: 41814898
This is the code in my BaseViewModel.  You would still add the Text in the BaseViewModel?

public abstract class BaseViewModel : INotifyPropertyChanged
    {
        public INavigation Navigation { get; set; }

        protected BaseViewModel(INavigation navigation = null)
        {
            Navigation = navigation;
        }

Open in new window

0
 
LVL 33

Expert Comment

by:ste5an
ID: 41814900
Yup.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question