I'm new to programming for Windows Phone 8 and I'm working on a simple notepad. I can't seem to figure out this error.
I want to enable a save button when a textbox is no longer empty. I used a keyup event on the textbox itself:
<TextBox x:Name="TextBoxNote" KeyUp="TextBoxNote_KeyUp" />
Code behind:
private void TextBoxNote_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (TextBoxNote.Text == "")
{
ApplicationBarSaveButton.IsEnabled = false;
}
else
{
ApplicationBarSaveButton.IsEnabled = true;
}
}
In XAML I set IsEnabled to false
<shell:ApplicationBarIconButton x:Name="ApplicationBarSaveButton"
IconUri="/Assets/AppBar/e105-Save.76 (1).png"
Text="save" IsEnabled="False"
Click="ApplicationBarIconButton_Click_1" />
If I enter something in the textbox, I get this error:
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=Notepad
StackTrace:
at Notepad.WriteNote.TextBoxNote_KeyUp(Object sender, KeyEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
InnerException:
On line:
ApplicationBarSaveButton.IsEnabled = true;
Any Ideas on this one?
Try this,
private void TextBoxNote_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
ApplicationBarIconButton button= (ApplicationBarIconButton)ApplicationBar.Buttons[0];
if (TextBoxNote.Text == "")
{
button.IsEnabled = false;
}
else
{
button.IsEnabled = true;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.