Newbie question with a problem with EventHandler with C# on CRM RRS feed

  • Question

  • Hi All,

    I am trying Silverlight for CRM and having a problem with the following code. After debugging, found that both of the RoutedEventHandler and EventHandler do not fire.

    I would appreciate your input and help


    using System;
    using System.Data.Services.Client;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.Crm.Sdk.Data.Services;
    namespace SizeMatrix
      public partial class MainPage : UserControl
        private String _serverUrl;
        private efashionContext _context;
        private DataServiceCollection<Account> _accounts;
        public MainPage()
          this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        public void MainPage_Loaded(object sender, RoutedEventArgs e)
          //Get the ServerUrl (ServerUrl is formatted differently OnPremise than OnLine)
          _serverUrl = GetURL.GetServerUrl();
          if (!String.IsNullOrEmpty(_serverUrl))
            //Setup Context
            _context = new efashionContext(new Uri(
              String.Format("{0}/xrmservices/2011/organizationdata.svc/", _serverUrl), UriKind.Absolute));
            //This is important because if the entity has new attributes added the code will fail.
            _context.IgnoreMissingProperties = true;
        public void LoadExistingAccounts()
          var query = from account in _context.AccountSet
                select account;
          _accounts = new DataServiceCollection<Account>();
          _accounts.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(accounts_LoadCompleted);
        void accounts_LoadCompleted(object sender, LoadCompletedEventArgs e)
          _accounts.Add(new Account()
            Name = "New"
          dataGrid1.DataContext = _accounts;
    • Edited by Tito-Hafez Friday, May 13, 2011 4:17 PM fix
    Friday, May 13, 2011 4:15 PM

All replies

  • Was MainPage instantiated from your Silverlight application's entry point?
    Daniel Cai | http://danielcai.blogspot.com
    Friday, May 13, 2011 7:56 PM
  • Hi Daniel,


    Thank you for your reply.

    When tracing the code, it runs well up to the part where it intializes the components .... InitializeComponent();

    When it jumps to the following command -   this.Loaded += new RoutedEventHandler(MainPage_Loaded);

    It does NOT fire the RoutedEventHandler and execute the "MainPage_Loaded" class!!

    Thanks again for your help...


    Friday, May 13, 2011 8:59 PM
  • That is very strange. Your code looks good to me, but I am not sure why the constructor is called but the first event handler (MainPage_Loaded) was not invoked. The second event handler was not fired simply because the code to register the event handler has not been run, which is understandable. 

    What happen if you put a break point on InitializeComponent() line, and see what it will do after that? 

    Daniel Cai | http://danielcai.blogspot.com
    Saturday, May 14, 2011 5:28 AM

    Hi Daniel


    This is how I discouvered that it does not invoke the (MainPage_Loaded). I put a break point on the InitializeComponent() line and found that the after it initialize the page components, starts working on the ( this.Loaded += new RoutedEventHandler(MainPage_Loaded);) line.

    You see the line highlighted in yellow in VS2010 as it is being executed in debug mode. After that, it should start working on the execution of the lines in ( public void MainPage_Loaded(object sender, RoutedEventArgs e)).

    Instead, it just exits since the call for the event is the last line in the program.


    Thanks for your help...

    Saturday, May 14, 2011 1:12 PM
  • The problem that you have experienced is quite strange. You may try to recreate the Silverlight project and plumb in your code to see if it works. If that doesn't help, you may try to post your question to a Silverlight forum.


    Daniel Cai | http://danielcai.blogspot.com

    Monday, May 16, 2011 3:30 PM