Solved

C# - SOLID Question

Posted on 2016-09-24
6
42 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 32

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 32

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 32

Expert Comment

by:ste5an
ID: 41814900
Yup.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now