Question

I am developing a Windows phone application in c# xaml.
I am using MVVM architecture for that.

I have two View Model classes

BaseViewModel:ViewModelBaseX
{
  public override void OnNavigatedTo(NavigationMode mode, Uri uri, IDictionary<string, string> queryString)
  {
     // a condition is checking here & if the condition is true ,  
        i don't want to run the remaining codes in the AboutViewModel .

     TOTO: have to stop the execution here & prevent execution
           of remaining codes in the derived classes.
  }
}

AboutViewModel:BaseViewModel
{
  public override void OnNavigatedTo(NavigationMode mode, Uri uri, IDictionary<string, string> queryString)
  {
      base.OnNavigatedTo(mode, uri, queryString);                

      // here some code to be execute 

  }
}

I know a way by setting a property flag in baseviewmodel & checking it in derived view models.
But it will make code duplication & have to check it in all derived classes.

Is there any other way to that it from Base class? Please help !

Était-ce utile?

La solution 2

The easiest way is to:

1/ Introduce a new virtual method in BaseViewModel (called something along the lines of "HandleNavigatedTo")

2/ Have BaseViewModel call "HandleNavigatedTo" based on your criteria

3/ Have BaseViewModel descendants override "HandleNavigatedTo" and do their stuff in that method

4/ Have BaseViewModel descendants NOT override "OnNavigatedTo"

public class BaseViewModel : ViewModelBaseX
{


       protected virtual void HandleNavigatedTo(NavigationMode mode, Uri uri, IDictionary<string, string> queryString)
        {
        }

        public override void OnNavigatedTo(NavigationMode mode, Uri uri, IDictionary<string, string> queryString)
        {
            if (condition)
            {
                HandleNavigatedTo(mode, uri, queryString);
             }
        }
    }

    public class AboutViewModel:BaseViewModel
    {
          public override void HandleNavigatedTo(NavigationMode mode, Uri uri, IDictionary<string, string> queryString)
          {
              // here some code to be execute 
          }
     }

Autres conseils

One option would be to put all your actual OnNavigatedTo code in a separate method that is called by the base class OnNavigateTo. Create this as a protected method on the base class (that does nothing probably) and override it on the derived class. The base class then only calls it if it wants to.

BaseViewModel:ViewModelBaseX
{
  public override void OnNavigatedTo(NavigationMode mode, Uri uri, IDictionary<string, string> queryString)
  {
     // a condition is checking here & if the condition is true ,  
        i don't want to run the remaining codes in the AboutViewModel .
    if (someCondition)
    {
        NavigateToFunctionality();
    }
    else
    {
      // do nothing
     }
  }

  protected void NavigateToFunctionality()
  {
  }

}

AboutViewModel:BaseViewModel
{
  public override void OnNavigatedTo(NavigationMode mode, Uri uri, IDictionary<string, string> queryString)
  {
      base.OnNavigatedTo(mode, uri, queryString);                

  }

  protected override void NavigateToFunctionality()
  {
    // your code goes here
  }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top