Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6601139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:42:52+00:00 2026-05-25T18:42:52+00:00

I have a Silverlight application architected using an MVVM approach. In my ViewModel it

  • 0

I have a Silverlight application architected using an MVVM approach. In my ViewModel it is time to load some data only after the user is LoggedIn, so I need to intercept this event to trigger my LoadData().
With default configuration, and I mean this in the App.xaml.cs:

public App()
    {
        InitializeComponent();

        // Create a WebContext and add it to the ApplicationLifetimeObjects
        // collection.  This will then be available as WebContext.Current.
        WebContext webContext = new WebContext();
        webContext.Authentication = new FormsAuthentication();
        //webContext.Authentication = new WindowsAuthentication();
        this.ApplicationLifetimeObjects.Add(webContext);

If you try to subscribe to LoggedIn Or LoggedOut events in the ViewModel constructor, you get a bit of problems: WebContext doesn’t exists yet.

So I thought… I’ll create my WebContext first and then I’ll InitializeComponents() in my App but that made ExpressionBlend sad…

So, here it is my solution, I like to share it with you because I’m not totally convinced that this would be the right approach:

App.Current.Startup += (sender, eventArgs) => 
        {
            WebContext.Current.Authentication.LoggedIn += WebContext_LoggedIn;
            WebContext.Current.Authentication.LoggedOut += WebContext_LoggedOut;
        };

In my ViewModel ctor I subscribe to App.Current.Startup and my delegate will subscribe my ViewModel to Login events, this way I have not changed my App.xaml.cs and I’m sure to subscribe to Login events when WebContext exists…
So:

private void WebContext_LoggedIn(object sender, AuthenticationEventArgs e)
    {
        LoadData();
    }

EDIT

In this case I’m more interested in understanding if I’m right when I say that I shouldn’t change the order between InitializeComponent() and the rest and that I need to listen for a particular event to trigger my LoadData().

Just for the sake of completeness, here it is my refactor to get rid of that dependency in my ViewModel:

I created a message:

public class UserLoginStatusChangedMessage : MessageBase
{
    public bool IsLoggedIn { get; set; }
}

sent it here:

private void Application_Startup(object sender, StartupEventArgs e)
    {
        // This will enable you to bind controls in XAML files to WebContext.Current
        // properties
        this.Resources.Add("WebContext", WebContext.Current);

        // This will automatically authenticate a user when using windows authentication
        // or when the user chose "Keep me signed in" on a previous login attempt
        WebContext.Current.Authentication.LoadUser(this.Application_UserLoaded, null);

        // Show some UI to the user while LoadUser is in progress
        this.InitializeRootVisual();

        WebContext.Current.Authentication.LoggedIn += (s, a) => 
        { 
            Messenger.Default.Send(new UserLoginStatusChangedMessage 
            { IsLoggedIn = true }); 
        };

        WebContext.Current.Authentication.LoggedOut += (s, a) => 
        {
            Messenger.Default.Send(new UserLoginStatusChangedMessage 
            { IsLoggedIn = false });
        };
    }

and received it like this in ViewModel ctor:

 Messenger.Default.Register<UserLoginStatusChangedMessage>(this, msg => 
        {
            if (msg.IsLoggedIn)
            {
                LoadData();
            }
        });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:42:52+00:00Added an answer on May 25, 2026 at 6:42 pm

    I’d suggest using some kind of messenger class that would send a message when the user is logged in. MVVMLight has a nice messenger class that is easy to use. Then you would just send a message when the user’s logged in state changes and subscribe to that event in your view model that needs to know if the user is logged in.

    You could check the WebContext to see if it is created and if it is created if the user is logged in when your viewmodel is created and then after that just use the messages to determine if/when this changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Silverlight application using NHibernate as a ORM. I have projects for Data(mapp
I have a Silverlight application that needs to scroll some data (like stock information)
I have a Silverlight application where I periodically load more data and add it
I have a silverlight application which reads data from a db and displays them
I have a Silverlight application that is using a DataGrid. Inside of that DataGrid
I have a Silverlight application that needs to display data in both English (feet,
I have a silverlight application that requires the user to login. The problem is
I have a Silverlight application that implements some basic CRUD operations on a fairly
We have a Silverlight application using the RichTextBox as a rich text editor for
I have a Silverlight application using a WCF service hosted in IIS. I make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.