c# - Creating base loading view with prism -


my prism application has lot of async operations called view models. in cases, want view disabled , display kind of busy indicator until viewmodel gets result async operation.

i though of creating base view implement behavior (i.e have dependency property of isloading disable view , display busy indicator above it). problem is, i'm not sure how implement base view. appreciated, thanks.

edit: wrote loadingview job, think.

public class loadingview : usercontrol { private object content;

    public bool isloading     {                 {             return (bool)getvalue(isloadingproperty);         }         set         {             setvalue(isloadingproperty, value);         }     }      private progressring m_ringcontrol;      public loadingview()     {         m_ringcontrol = new progressring();         m_ringcontrol.isactive = false;     }      // using dependencyproperty backing store isloading.  enables animation, styling, binding, etc...     public static readonly dependencyproperty isloadingproperty =         dependencyproperty.register("isloading", typeof(bool), typeof(loadingview), new propertymetadata(false, isactivepropertychanged));      private static void isactivepropertychanged(dependencyobject d, dependencypropertychangedeventargs e)     {         loadingview view = d loadingview;         if (view != null)         {             // loading - show ring control             if (((bool)e.newvalue) == true)             {                 view.content = view.content;                 view.content = view.m_ringcontrol;                 view.m_ringcontrol.isactive = true;             }             else             {                 view.m_ringcontrol.isactive = false;                 view.content = view.content;             }         }     } } 

and put binding on loadingview.isloading isloading (or isbusy) in viewmodel

this subject can complex fast.

i advise small change in approach - instead of putting isbusy property in base viewmodel, instead make abstract each derived viewmodel has implement own specific check.

public class baseviewmodel : inotifypropertychanged {      public abstract bool isbusy { get; }  }  public class fancyviewmodel : baseviewmodel {      public override bool isbusy     {         { return [check #1] && [check #2]...; }     } } 

now each specific viewmodel determine whether busy. crude mechanism have counter increment every time async function kicked off, , decrement when operation ends - if value equal 0 there no current async operations. when using flags or counters aware of various property reading issues can occur because of compiler optimisations, learn use volatile keyword in right place.

alternatively instead of keeping counter use thread safe countdownevent class. if want advanced can check out various thread signalling mechanisms in system.threading namespace, or @ task parallelism , task object.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -