Launcing user control in Application_Launching Error

Sep 16, 2011 at 7:08 PM

I am getting this error at the launching event of WP7

Value can not be null.Parameter name: element

Can anyone please tell me what does this mean.?

Sep 16, 2011 at 8:22 PM

this error comes at

mpPassphrase.Show();

 

it seems to me that this control needs a base page to display this prompt. as on App.xaml its not able to find it so its causing this error.

Coordinator
Sep 16, 2011 at 9:33 PM

Can you provide more here?  like, where in App.xaml.cs you're calling this?  I know why this is happening but need to know a bit more so I can simulate it.

Sep 16, 2011 at 10:04 PM

When my application is launched i need the password to be prompted so i created a user control and wrote something like this

 

    
mpPassphrase = new MessagePrompt();
            //PasswordInputPrompt messagePrompt = new PasswordInputPrompt();
            mpPassphrase.Body = new ucDefaultPassword();
            mpPassphrase.ActionPopUpButtons.Clear();

            //Add ChangePassphrase ok button
            Button passphraseOkButton = new Button() { Content = DefaultStrings.Ok };
            passphraseOkButton.Click += new RoutedEventHandler(passphraseOkButton_Click);
            mpPassphrase.ActionPopUpButtons.Add(passphraseOkButton);

            //Add ChangePassphrase Cancel Button
            Button passphraseCancelButton = new Button() { Content = DefaultStrings.Cancel };
            passphraseCancelButton.Click += new RoutedEventHandler(passphraseCancelButton_Click);
            mpPassphrase.ActionPopUpButtons.Add(passphraseCancelButton);

            mpPassphrase.Completed += new EventHandler>(messagePrompt_Completed);
            mpPassphrase.Show();

mpPassphrase is decleared on the top. so when

 

Application_Launching event of App.xaml runs it throws above error ArgumentNullException

 

Sep 16, 2011 at 10:46 PM

this was the error msg

Value can not be null.Parameter name: element

Coordinator
Sep 16, 2011 at 11:22 PM

easy fix for this is wrap your control in Dispatcher.BeginInvoke(() => { /* your code */ });  this is how the control was designed, I have two things I'm going to do moving forward that should correct this.

The underling cause is the visual DOM hasn't been created yet so there is nothing for my control to append into.

Sep 16, 2011 at 11:47 PM

I was not able to use the Dispatcher.BeginInvoke(() => { /* your code */ }); as is  as this was giving me the error

Error 3 An object reference is required for the non-static field, method, or property 'System.Windows.Threading.Dispatcher.BeginInvoke(System.Action)' 

then i created a Dispatcher instance in my app.xaml

 Dispatcher myDispatcher;

 

 
 
by this i was able to compile but this gave me NullReferenceException with this code

 

myDispatcher.BeginInvoke(() =>
            {
                mpPassphrase = new MessagePrompt();
                //PasswordInputPrompt messagePrompt = new PasswordInputPrompt();
                mpPassphrase.Body = new ucDefaultPassword();
                mpPassphrase.ActionPopUpButtons.Clear();

                //Add ChangePassphrase ok button
                Button passphraseOkButton = new Button() { Content = DefaultStrings.Ok };
                passphraseOkButton.Click += new RoutedEventHandler(passphraseOkButton_Click);
                mpPassphrase.ActionPopUpButtons.Add(passphraseOkButton);

                //Add ChangePassphrase Cancel Button
                Button passphraseCancelButton = new Button() { Content = DefaultStrings.Cancel };
                passphraseCancelButton.Click += new RoutedEventHandler(passphraseCancelButton_Click);
                mpPassphrase.ActionPopUpButtons.Add(passphraseCancelButton);

                mpPassphrase.Completed += new EventHandler>(messagePrompt_Completed);
                mpPassphrase.Show();
            });

still the code didn't run.

 

Sep 16, 2011 at 11:52 PM

I also tried with

Deployment.Current.Dispatcher.BeginInvoke(() => { //code });

it gave  me ArgumentNullReferenceException

Value can not be null.Parameter name: element

Coordinator
Sep 17, 2011 at 5:51 PM

Issue is there is no visual page to append to so i have nothing to append to.  Looking at this, even a primative popup doesn't seem like it can correct where you want to put it in.  This is due to where you want to create the control.  You want to do something much too early in the application's life cycle that requires items that haven't been created yet.

I think you'll have to either use a primative messagebox control or leverage the page's onnavigatedto event and could do a static function in app.xaml.cs that your page's call.

sorry, tried for about an hour to do it where you'd want it but couldn't see a path that didn't seem like a total hack

Sep 19, 2011 at 4:33 PM

NP....:)

But thanks for giving a try and appreciate you reply.

Have a wonderful life...:)

Also thanks for this wonderful toolkit.

Coordinator
Sep 19, 2011 at 6:03 PM

What you want to do is possible, just requires a bit more works and being clever.  Have onnavigated to on any entry point to your application call a function in app.xaml.cs.  That function then checks a setting var where if false, it displays tour prompt, sets to true, and saves the setting file.