c# - Pop up not getting opened when a new thread is started using Application.Current.Dispatcher.BeginInvoke in mvvm wpf -


i want show pop having animation when user log in application. scenario this: have main window content bound property selectedvm in backend. property can have 2 values, 1 loginviewmodel, , 2 containerviewmodel.

now, want show pop after login till data appears in containerviewmodel.

i have kept pop in mainwindow.xaml.

i have singleton class session in have property isopen of pop up. set in loginviewmodel when data loading functionality called. can see property being set through checkbox pop not show up. below code.

mainwindow.xaml

<window><window.resources>      <datatemplate datatype="{x:type viewmodel:containerviewmodel}">         <view:containerview></view:containerview>     </datatemplate>      <datatemplate datatype="{x:type viewmodel:loginviewmodel}">         <view:loginscreenview></view:loginscreenview>     </datatemplate> </window.resources>  <dockpanel>     <dockpanel.background>         <imagebrush imagesource="images/back.jpg"/>     </dockpanel.background>      <popup x:name="waitscreen"             placement="center"             visibility="{binding path=currentsession.isloading}"            >          <local:loadanimation  background="black" margin="110,0,0,0"/>     </popup>      <contentcontrol content="{binding selectedvm}"></contentcontrol>      <!--<view:loginscreenview dockpanel.dock="top" height="100" width="250" horizontalalignment="stretch" verticalalignment="stretch"/>--> </dockpanel></window> 

mainwindowviewmodel.cs

 public class mainwindowviewmodel: viewmodelbase {    public mainwindowviewmodel()    {        try        {            this.currentsession.isloading = "hidden";            this.currentsession.propertychanged += this.currentsession_propertychanged;            this.currentsession.vmbinstance = "loginviewmodel";         }        catch (exception ex)        {            messagebox.show(ex.tostring());        }    }          void currentsession_propertychanged(object sender, system.componentmodel.propertychangedeventargs e)    {       if(e.propertyname=="vmbinstance")       {           switch (this.currentsession.vmbinstance)           {               case "loginviewmodel":                   {                       this.selectedvm = new loginviewmodel();                       break;                   }               case "containerviewmodel":                   {                        this.selectedvm = new containerviewmodel();                       break;                   }               default:                   {                       this.selectedvm = new loginviewmodel();                       break;                   }            }       }    }    private viewmodelbase selectedvm;     public viewmodelbase selectedvm    {        { return selectedvm; }        set { selectedvm = value; onpropertychanged(()=>this.selectedvm); }    }  } 

loginviewmodel.cs

 class loginviewmodel :viewmodelbase {         public loginviewmodel()     {         submit = false;         this.currentsession.isloading = "hidden";         //this.logincommand = new relaycommand(o => this.worker.runworkerasync(), o => !this.worker.isbusy);         //this.worker.dowork+=this.dowork;     }        #region properties       private double _currentprogress;     public double currentprogress     {         { return _currentprogress; }         private set         {             if (_currentprogress != value)             {                 _currentprogress = value;                 onpropertychanged("currentprogress");             }         }     }     private jira session { get; set; }     private string _username;      public string username     {                  {             if (_username == string.empty || _username == null || _password == string.empty || _password == null)             {                 submit = false;              }             return _username;          }         set {              _username = value;             if (value!=string.empty || value!=null)             {                 submit = true;              }             onpropertychanged(() => this.username); }     }      private bool submit;      public bool submit     {         { return submit; }         set { submit = value; onpropertychanged(() => this.submit);}     }      private string _password;      public string password     {                 {             if (_password == string.empty || _password == null || _username == string.empty || _username == null)             {                 submit = false;              }             return _password;          }          set          {             _password = value;             if (value != string.empty || value != null)             {                 submit = true;              }             onpropertychanged(() => this.password);         }     }        #endregion      #region commands     public icommand logincommand     {                   {             try             {                  return new relaycommand(param => login());              }             catch(exception ed)             {             messagebox.show("invalid login credentials");             return null;             }         }      }       public bool login()     {         try         {              //this.dispatcher.begininvoke(new action(()=> this.currentsession.isloading=true));               application.current.dispatcher.begininvoke             (new action(() => {                 this.currentsession.isloading = "visible";              }));              this.session = new jira("http://jira.mcm.com:8080/", this.username, this.password);              string test = this.session.getaccesstoken();             if (this.session == null)             {                 return false;             }              this.currentsession.jiraobj = this.session;             this.currentsession.username = this.username;             this.currentsession.vmbinstance = "containerviewmodel";             //this.currentsession.isloading = true;              application.current.dispatcher.invoke(new action(() => this.currentsession.isloading = "hidden"));             //this.dispatcher.begininvoke(new action(() => this.currentsession.isloading = false));              return true;         }         catch (exception ex)         {             messagebox.show("invalid login credentials");              return false;         }                  {             this.session = null;          }     }     #endregion } 

can please tell me how make pop visible in new ui thread?

your view binds isloading popup's visbility property, property string.

change isloading property visbility data type:

application.current.dispatcher.begininvoke         (new action(() => {             this.currentsession.isloading = visibility.visible;         })); 

some standard things check:

  • is context correct when bind?
  • does property notify ui of change (inotifypropertychanged)
  • is binding direction correct? (one-way, two-way etc.)
  • are properties correct data type
  • is converter messing around? (if i'm using one). breakpoint , debug it.

Comments