Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

C# - SOLID Question

Posted on 2016-09-24
6
49 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 35

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 33

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

860 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