Monday, 15 June 2015

c# - WPF MVVM PropertyChanged notifications in the ViewModel triggered by Model Events -



c# - WPF MVVM PropertyChanged notifications in the ViewModel triggered by Model Events -

i having problem understanding how propagate property changed event in model class through viewmodel , view. trying conform mvvm pattern please maintain in mind.

i have model trying expose viewmodel. model class queries api phone call server status , exposes status in public properties. ex:

public class serverstatusrequest : apirequest { //exposable properties request public serverstatushelperclass status { get; set; }

where serverstatushelperclass wrapper combined results in query:

public class serverstatushelperclass { public bool serverstatus { get; set; } public int onlineplayers { get; set; }

the cool thing apirequest base of operations class checks cache time of particular api phone call , updates results using system.timers.timer. so, example, serverstatus api phone call cached 3 minutes on api, every 3 minutes serverstatusapirequest object have fresh info it. expose updatedresults event in apirequest classes notify when new info comes in.

now want viewmodel have instance of serverstatusapirequest , bind serverstatushelperclass status property , remain date changes every time info updated, view (for binding) can't know model, , thus, doesn't know updatedresults event in apirequest class. how can reflect out view through viewmodel? doing weird here?

here have semi-working sense hacky solution:

in viewmodel:

public const string eveserverstatuspropertyname = "eveserverstatus"; private serverstatusrequest _eveserverstatus = new serverstatusrequest(); public serverstatusrequest eveserverstatus { { homecoming _eveserverstatus; } set { //if (_eveserverstatus == value) //{ // return; //} //raisepropertychanging(eveserverstatuspropertyname); _eveserverstatus = value; raisepropertychanged(eveserverstatuspropertyname); } } public void updateeveserverstatus(object sender, eventargs e) { eveserverstatus = (serverstatusrequest)sender; }

and in viewmodels constructor subscribe model's event:

eveserverstatus.updatedresults += new updatedresultseventhandler(updateeveserverstatus);

as can see, seems extremely redundant. , ran problem had comment out check in setter eveserverstatus because @ point _eveserverstatus updated value without knowing , wanted fire event anyway.

i fell i'm missing key concept here link much more easily.

thanks input.

i have come across much improve way implement behavior looking for. here code in viewmodel:

private serverstatusrequest _eveserverstatus = new serverstatusrequest(); public serverstatusrequest eveserverstatus { { homecoming _eveserverstatus; } }

no setter viewmodel nor view should changing data. , within serverstatusrequest class have property exposing serverstatushelperclass object shown in question. have changed serverstatushelperclass , made implement inotifypropertychanged so:

public class serverstatushelperclass : observableobject { private bool _serverstatus; public bool serverstatus { { homecoming _serverstatus; } set { _serverstatus = value; raisepropertychanged("serverstatus"); } } ...

observableobject simple class implements inotifypropertychanged me mvvmlight.

by doing view automatically updated when apirequest class modifies it's serverstatushelperclass object.

input on solution welcome.

c# wpf data-binding mvvm mvvm-light

No comments:

Post a Comment