Categories: Blog

Silverlight for Windows Phone Programming Tip #3

Application bars play an important role in the UI of many phone applications. An application bar can contain up to four buttons (and five menu items) giving the user quick and easy access to the app’s most commonly used features:

Each button is an instance of ApplicationBarIconButton, and the ApplicationBarIconButton class exposes a property named IsEnabled that allows buttons to be enabled and disabled.

Let’s say that you want to disable one of these buttons in response to something the user does in your application. For example, maybe you want to disable the Save button after the user clicks it to save whatever he or she is working on. If you try doing it this way, you’re in for a rude surprise:

 

// MainPage.xaml

<shell:ApplicationBar IsVisible="True">

  <shell:ApplicationBarIconButton x:Name="AddButton" … />

  <shell:ApplicationBarIconButton x:Name="SaveButton" Click="OnSaveButtonClicked" … />

  <shell:ApplicationBarIconButton x:Name="RefreshButton" … />

  <shell:ApplicationBarIconButton x:Name="SettingsButton" … />

</shell:ApplicationBar>

 

// MainPage.xaml.cs

private void OnSaveButtonClicked(object sender, EventArgs e)

{

    SaveButton.IsEnabled = false;

}

 

The code compiles just fine, but it throws a NullReferenceException at run-time because SaveButton isn’t automatically initialized with a reference to the Save button.

So how do you disable an application bar button in code? By doing this:

 

// MainPage.xaml

<shell:ApplicationBar IsVisible="True">

  <shell:ApplicationBarIconButton x:Name="AddButton" … />

  <shell:ApplicationBarIconButton x:Name="SaveButton" Click="OnSaveButtonClicked" … />

  <shell:ApplicationBarIconButton x:Name="RefreshButton" … />

  <shell:ApplicationBarIconButton x:Name="SettingsButton" … />

</shell:ApplicationBar>

 

// MainPage.xaml.cs

private void OnSaveButtonClicked(object sender, EventArgs e)

{

    (ApplicationBar.Buttons[1] as ApplicationBarIconButton).IsEnabled = false;

}

 

It’s not pretty, but it’s the way it is. And now that you know, you won’t lose an hour of development time the first time you run into it.

Jeff Prosise

Recent Posts

8-Step AWS to Microsoft Azure Migration Strategy

Microsoft Azure and Amazon Web Services (AWS) are two of the most popular cloud platforms.…

2 weeks ago

How to Navigate Azure Governance

 Cloud management is difficult to do manually, especially if you work with multiple cloud…

3 weeks ago

Why Azure’s Scalability is Your Key to Business Growth & Efficiency

Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…

1 month ago

Unlocking the Power of AI in your Software Development Life Cycle (SDLC)

https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…

2 months ago

The Role of FinOps in Accelerating Business Innovation

FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…

2 months ago

Azure Kubernetes Security Best Practices

Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…

2 months ago