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

  • SEARCH
  • Home
  • 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 5843359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:08:37+00:00 2026-05-22T12:08:37+00:00

I am trying to make my very first Silverlight App ever, but I can’t

  • 0

I am trying to make my very first Silverlight App ever, but I can’t get the LogOn function to work, can you help me? This should properly be super simple for all of you, I will show you my two files: LogOn.xaml.cs and LogOnViewModel.cs

Apparently the problem is that UserId gets not set early enough to be availble in LogOn.xaml.cx when I need it, can you help me make it work, that would lift my moment quite a bit 🙂

public partial class LogOn : PhoneApplicationPage
{
    public LogOn()
    {
        InitializeComponent();
        this.DataContext = LogOnViewModel.Instance;
    }

    private void btnLogOn_Click(object sender, RoutedEventArgs e)
    {
        if ((!string.IsNullOrEmpty(txtEmailAddress.Text)) && (!string.IsNullOrEmpty(txtPassword.Password)))
        {
            txbLogonMessage.Text = "";
            LogOnViewModel.Instance.UserLogin(txtEmailAddress.Text, txtPassword.Password);

            if (LogOnViewModel.Instance.UserId > 0)
                NavigationService.Navigate(new Uri("/_2HandApp;component/Views/Main.xaml", UriKind.Relative));
            else
                txbLogonMessage.Text = "Login was unsuccessful. The user name or password provided is incorrect. Please correct the errors and try again. ";
        }
    }
}


public sealed class LogOnViewModel : INotifyPropertyChanged
{
    public static LogOnViewModel Instance = new LogOnViewModel();
    //public static int userId;

    private SHAServiceClient WS;

private int userId;
    public int UserId
    {
        get
        {
            return userId;
        }

        set
        {
            userId = value;
            this.RaisePropertyChanged("UserId");
        }
    }


private LogOnViewModel()
    {
        WS = new SHAServiceClient();
        WS.UserLoginCompleted += new EventHandler<UserLoginCompletedEventArgs>(WS_UserLoginCompleted);
    }

    void WS_UserLoginCompleted(object sender, UserLoginCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            this.UserId = e.Result;
        }
    }


    public void UserLogin(string email, string password)
    {
        WS.UserLoginAsync(email, password);
    }

/* Implementing the INotifyPropertyChanged interface. */
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null))
        {
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
  • 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-22T12:08:38+00:00Added an answer on May 22, 2026 at 12:08 pm

    The cause of the problem is what has been highlighted by @flq. You’re making an asynchronous call, meaning that you won’t get the expected result right away (in your case, the UserId being assigned), but instead, you can subscibe to the Completed event (or provide a callback) to handle things when the asynchronous task finishes.

    Now, the “MVVM way” to do this (or at least what I would do) is as follows: first of all, go get MVVM Light! it’s a lightweight MVVM framework which would be very helpful. You should have your ViewModel class implement the ViewModelBase base class from MVVMLight, this would provide the change notification and messaging as well as other useful stuff. Then, you should encapsulate the login functionality in a command to be able to wire up it up from xaml, for that you can use MVVMLight’s RelayCommand. Once the login is complete, you can just send a message to your view letting it know that (in a pretty decoupled way), and the view can simply initiate the navigation.

    Here’s the bits of code for that:

    public class LogOnViewModel : ViewModelBase
    {
        private SHAServiceClient WS;
        public LogOnViewModel()
        {
           WS = new SHAServiceClient();
           WS.UserLoginCompleted += new EventHandler<UserLoginCompletedEventArgs>(WS_UserLoginCompleted);
           LoginCommand = new RelayCommand(UserLogin);
        }
        private int userId;
        public int UserId
        {
           get { return userId; }
           set
           {
              userId = value;
              RaisePropertyChanged(()=>UserId);
           }
        }
        private int password;
        public int Password
        {
           get { return password; }
           set
           {
              password = value;
              RaisePropertyChanged(()=>Password);
           }
        }
        private int username;
        public int Username
        {
           get { return username; }
           set
           {
              username = value;
              RaisePropertyChanged(()=>Username);
           }
        }
        private int loginErrorMessage;
        public int LoginErrorMessage
        {
           get { return loginErrorMessage; }
           set
           {
              loginErrorMessage = value;
              RaisePropertyChanged(()=>LoginErrorMessage);
           }
        }
        void WS_UserLoginCompleted(object sender, UserLoginCompletedEventArgs e)
        {
           if (e.Error == null)
           {
              this.UserId = e.Result;
              // send a message to indicate that the login operation has completed
              Messenger.Default.Send(new LoginCompleteMessage());
           }
        }
        public RelayCommand LoginCommand {get; private set;}
        void UserLogin()
        {
           WS.UserLoginAsync(email, password);
        }
    }
    

    for the xaml:

    <TextBox Text="{Binding Username, Mode=TwoWay}"/>
    <TextBox Text="{Binding Password, Mode=TwoWay}"/>
    <Button Command="{Binding LoginCommand}"/>
    <TextBlock Text="{Binding LoginErrorMessage}"/>    
    

    in the code behind:

    public partial class LogOn : PhoneApplicationPage
    {
        public LogOn()
        {
            InitializeComponent();
            this.DataContext = new LogOnViewModel();
            Messenger.Default.Register<LoginCompletedMessage>(
                                this,
                                msg=> NavigationService.Navigate(
                                        new Uri("/_2HandApp;component/Views/Main.xaml",
                                        UriKind.Relative) );
        }
      ....
    }
    

    You can see that there is a little bit more (but straightforward) code in the ViewModel and less in the code behind. This also took advantage of DataBinding which is in the heart of MVVM.

    Hope this helps 🙂

    P.S: the LoginCompletedMessage class is just an empty class in this case (used just to define the type message), but you can use it to send more info (maybe you still want to have the UserId sent)

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

Sidebar

Related Questions

I'm trying to make a very simple OSGi test but I can't get it
I'm trying to make a very simple updater app that reads current version.txt file
I was trying to make a tail-recursive version of this very simple SML function:
I am trying to make a program which does a very basic calculation, but
A little context: I'm trying to make a very simple hashing function/hash table as
I am trying to make a very basic game with Java and I am
I am trying to make a very simple object rotate around a fixed point
I am trying to make a very simple web-service which does the following: The
i'm trying to make a very simple YACC parser on Pascal language which just
I'm learning Objective-C and trying to make a very simple command line calculator. 'S'

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.