views:

63

answers:

2

Good day everyone. I'm writing my first device-specific application which is a Windows Phone 7 panorama app. I'm currently still busy on the UI as I'm playing around with the features then I stumbled upon a problem that I couldn't fix. You see, I have two checkboxes for some sort of login form. One is a "Remember me" and the other a "Sign me in automatically" checkbox. The action I want is that when I uncheck Remember Me, I would like the Sign in Automatically checkbox to be unchecked and disabled. That I was able to do but the reverse always causes an error. I used to write simple PHP web apps and Javascript so I have some programming knowledge but C# is fairly new to me.

private void RememberMe_Unchecked(object sender, RoutedEventArgs e)
{
    AutoSignIn.IsChecked = false;
    AutoSignIn.IsEnabled = false;
}

That one works but this one doesn't:

private void RememberMe_Checked(object sender, RoutedEventArgs e)
{
    AutoSignIn.IsEnabled = true;
}

The latter throws a "NullReferenceException was unhandled" error.

My XAML code looks like this:

<CheckBox Content="Remember me" Height="71" Name="RememberMe" Unchecked="RememberMe_Unchecked" Checked="RememberMe_Checked" IsEnabled="True" IsChecked="True" />
<CheckBox Content="Sign me in automatically" Height="71" Name="AutoSignIn" IsEnabled="True" IsChecked="True" />

I've done some research and my approach seem to be wrong but I'm not sure how to make it work. I would highly appreciate any helpful advise. Thanks!

A: 

Your code works for me.

I used your event handlers with the following XAML:

<CheckBox Name="AutoSignIn" />
<CheckBox Name="RememberMe" Checked="RememberMe_Checked" Unchecked="RememberMe_Unchecked" />

Is the error thrown on the line in RememberMe_Checked? or on something else which is set as a consequence of changing the enabled state of AutoSignIn?
Do you, for instance, have any databindings that could be affecting this?

Matt Lacey
Thanks for your response. My XAML looks similar to yours except that both checkboxes have the IsChecked property set to "true" as I want them set on default. If they are set to false at runtime they work. The error seem to be triggered by AutoSignIn.IsEnabled = true when the application starts.
DeVilFisCh
A: 

Without the XAML I can't be 100% sure, but make sure you are not setting the IsChecked property programmatically. When you do so, the IsChecked method will get called once before everything is initialised properly on the page. So while the code posted by Matt works:

<CheckBox Name="AutoSignIn" />
<CheckBox Name="RememberMe" Checked="RememberMe_Checked" Unchecked="RememberMe_Unchecked" />

The following won't (because it tries to reference the AutoSignIn box before the page has finished initializing)

<CheckBox Name="AutoSignIn" />
<CheckBox Name="RememberMe" IsChecked="True" Checked="RememberMe_Checked" Unchecked="RememberMe_Unchecked" />

To fix this, you can set the IsChecked property programmatically instead of in XAML, or there might be some other way around this that someone else can point you to.

Blakomen
Yes, that makes sense. I now tried setting IsChecked to true for both checkboxes in the MainPage_Loaded method instead of in the XAML and it now works flawlessly. It's now one of the "Why didn't I think of that?" moments for me. :P Thanks a lot!
DeVilFisCh
dont worry, the only reason i know is cause i fell for the exact same thing before :P
Blakomen