[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 69
  • Last Modified:

C# - SOLID Question

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
CipherIS
Asked:
CipherIS
1 Solution
 
ste5anSenior DeveloperCommented:
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
 
CipherISAuthor Commented:
I'm creating a viewmodel for each View as they will both have different functionality.
0
 
Miguel OzSoftware EngineerCommented:
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
sarabandeCommented:
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
 
CipherISAuthor Commented:
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
 
ste5anSenior DeveloperCommented:
Yup.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now