c# - INotifyPropertyChanged in singleton and binding to the WPF element -


i'm bit experimenting mvvm once again , i'm experiencing 2 issues code.
first let me explain code structure:

i have class (simplified of course):

public abstract class navigationservicebase : notifypropertychangedbase, inavigationservice {    private iview _currentview;    public iview currentview    {        { return _currentview; }        protected set        {            _currentview = value;            onpropertychanged("currentview");        }    }     public virtual void dosomethingfancy()    {         currentview = ...; // here expect fire onpropertychanged , notify view    } } 

and singleton inheriting base class:

public class navigationservice : navigationservicebase {     private static readonly inavigationservice _instance = new navigationservice();     public static inavigationservice instance { { return _instance; } }      ... } 

viewmodel:

private inavigationservice _navigationservice;  public iview currentview { { return _navigationservice.currentview; } } public icommand navigatecommand { get; private set; }  public mainwindowviewmodel() {     _navigationservice = navigationservice.instance;     navigatecommand = new relaycommand(param => navigate(param)); }  private void navigate(object requestedpage) {     _navigationservice.navigate((string)requestedpage);     //onpropertychanged("currentview"); // works , but... } 

now issues:
1.) i'm editing xaml in visual studio 2012 express. seems works, warning message: unable load 1 or more of requested types. retrieve loaderexceptions more information. shows in part when declare resources binding viewmodel. mean? if rid of singleton, message goes away. project compiles , runs fine either way.

2.) seems onpropertychanged("currentview") isn't firing or something, because have manually call method within viewmodel itself. if try base class or inheriting singleton, doesn't work. (binding ignores new values). if manually while handing command, works. yes, it's line of code, wonder, there way make work without "cheating" this?

the problem you're binding property in viewmodel:

public iview currentview { { return _navigationservice.currentview; } } 

the natigationservice raising propertychanged, happens in _navigationservice, not in viewmodel itself, view never sees event.

there 2 common options -

you can listen propertychanged event on navigation service, , handle raising locally if needed:

_navigationservice = navigationservice.instance; _navigationservice.propertychanged += (o,e) =>  {    // when navigation raises prop changed property, raise    if (e.propertyname == "currentview")      onpropertychanged("currentview"); };  navigatecommand = new relaycommand(param => navigate(param)); 

the other alternative expose service, , bind directly it:

public inavigationservice navigation { { return _navigationservice; } } 

then, in view, bind content inside of service instead of local property:

<contentpresenter content="{binding navigation.currentview}" /> 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -