Application bar in windows phone 7
I am trying to create a application bar in my windows phone 7 application. But i think there is some problem with the click event because sometimes it works and sometimes it does not work.
Below is the code in xaml
and .cs
.
xaml
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="False">
<shell:ApplicationBarIconButton IconUri="/Icons/appbar.edit.rest.png" Text="Edit" Click="btnEdit_Click" IsEnabled="True"/>
<shell:ApplicationBarIconButton IconUri="/Icons/appbar.delete.rest.png" Text="Delete" Click="btnDelete_Click" IsEnabled="True"/>
<shell:ApplicationBarIconButton IconUri="/Icons/appbar.feature.email.rest.png" Text="Email" Click="btnEmail_Click" IsEnabled="True"/>
<shell:ApplicationBarIconButton IconUri="/Icons/appbar.back.rest.png" Text="Back" Click="btnBack_Click" IsEnabled="True"/>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
.cs
void btnBack_Click(object sender, 开发者_如何学JAVAEventArgs e)
{
NavigationService.Navigate(new Uri("/Library.xaml", UriKind.Relative));
}
void btnDelete_Click(object sender, EventArgs e)
{
MessageBox.Show("Can click");
}
Can anyone help me with it. Thanks.
One possible situation that I can think of is that Library page has a heavy constructor so navigating to that page takes so long. This situation can make an illusion that you tap on the button two three times and you think that it is not working while the first one has already been triggered but your thread is still thinking building up next page.
Try a very simple messagebox instead of Navigation to see if this guess is true.
If it is, then try to make your page constructor as light as possible and do your data loading "Asynchronus" and in "OnNavigatedTo".
For data load: You are probably reading your list of items to load on Library page. Loading items requires reading them from file system, web service or any other media which is time-consuming and can block UI. You need to do it in OnNavigatedTo (to make sure it starts after user is navigated to page) and asynchronously like this:
public override void OnNavigatedTo(...)
{
System.Threading.ThreadPool.QueueUserWorkItem(LoadData);
}
void LoadData(object o)
{
res = // load data from media.
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// add res to your page
});
}
Anything you write directly in code behind of your page would be run on UI Thread (except animations which are handled by a separate thread). So your need to keep tasks that are not related to UI in a separate thread (in the way shown above).
Note that your still need to trigger UI changes in the UI thread otherwise you will get "Corss-thread exception". (Thats why we are invoking a code in Dispatcher.BeginInvoke).
精彩评论