Solved

C# - SOLID Question

Posted on 2016-09-24
6
44 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

947 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

21 Experts available now in Live!

Get 1:1 Help Now