ApplicationBarIconButton is null - windows-phone-7

Why is my ApplicationBarIconButton null?
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" x:Name="appBar">
<shell:ApplicationBarIconButton x:Name="appbarSave"
IconUri="/Icons/appbar.save.rest.png Text="Save" IsEnabled="False"
Click="appbarSave_Click" />
</shell:Application Bar>
</phone:PhoneApplicationPage.ApplicationBar>
The appBarSave object is null, and trying this:
Initialize Component();
appbarSave.IsEnabled = true;
Results in a NullReferenceException. The only place the object works is in the click event (if I enable it):
private void appbarSave_Click(object sender, EventArgs e)
{
ApplicationBarIconButton button = (ApplicationBarIconButton)sender;
button.IsEnabled = false;
}
I would really like to be able to start the save button as disabled and enabled it later.

I remember running into this issue before: there's an explanation here. An easy workaround is just to instantiate it in code-behind rather than xaml (like here).
private ApplicationBarIconButton SaveEdit;
private void InitAppBar()
{
ApplicationBar appBar = new ApplicationBar();
SaveEdit = new ApplicationBarIconButton(new Uri("images/appbar.check.rest.png", UriKind.Relative));
SaveEdit.Click += new EventHandler(OnClick_Check);
SaveEdit.Text = Strings.Save_button;
appBar.Buttons.Add(SaveEdit);
ApplicationBarIconButton CancelEdit = new ApplicationBarIconButton(new Uri("images/appbar.close.rest.png", UriKind.Relative));
CancelEdit.Click += new EventHandler(OnClick_Cancel);
CancelEdit.Text = Strings.Cancel_button;
appBar.Buttons.Add(CancelEdit);
ApplicationBar = appBar;
}

try this
Microsoft.Phone.Shell.ApplicationBarIconButton btn = ApplicationBar.Buttons[0] as Microsoft.Phone.Shell.ApplicationBarIconButton;
btn.IsEnabled = false;

I use a bindable app bar control from here The download link is at the bottom of the article.
Makes life much easier and saves you from having to put code in the code behind.

I made this mistake today, the x:Name is ignored.
The ApplicationBar is part of the page already, whether you create it in XAML or not. There is no need to create a new one. Just use the ApplicationBar property in the code behind file.
Initialize Component();
ApplicationBar.IsEnabled = true;

I do it so, example for change icon
ApplicationBarIconButton btn = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
btn.IconUri = new Uri("/images/play.png", UriKind.Relative);

Related

fire a popup when ApplicationBarMenuItem clicked

I need to fire a popup when ApplicationBarMenuItem clicked. but nothing happen when I click MenuItem. here is my code;
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="Go by date" Click="GoByDate_Click" />
</shell:ApplicationBar.MenuItems>
private void GoByDate_Click(object sender, EventArgs e)
{
Popup popup = new Popup();
popup.Height = 480;
popup.Width = 480;
popup.VerticalOffset = 100;
DatePopupControl datePopup = new DatePopupControl(); // just a user control comes when add new
popup.Child = datePopup;
popup.IsOpen = true;
}
Something is wrong with your DatePopupControl, because your code worked fine for my own test control. Can you provide DatePopupControl source code?
Alright, let me try to guess. My first theory is: there is nothing to show in this popup.
For example, if you add a userControl, which has only Grid tag inside without anything, you will see in the editor in Visual Studio, but it won't be visible as a popup child. But if you add a single textblock, it will be.

Hyperlink button website

I need to put button that after click moves user to website. Can someone tell me what should I put into vb and in xaml?
<HyperlinkButton Content="Click here to learn about Silverlight" NavigateUri="http://www.silverlight.net" TargetName="_blank" />
You can do it also in C#
private void HyperlinkButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
var task = new Microsoft.Phone.Tasks.WebBrowserTask
{
URL = "http://siteaddress.com",
};
task.Show();
}

how to localize a application bar in wp7?

is it possible to localize the application bar?
i made the tutorial on msdn how to localize a application and everything was find. but the method with:
{Binding Localizedresources.Today, Mode=OneWay}
dont work on the app. bar
what can i do?
If you don't want to use a 3rd party solution like James Cadd suggested, you may create the application bar from your code-behind and using your resources to fill-in the Text-property:
public MainPage() {
InitializeComponent();
Loaded += MainPage_Loaded;
}
void MainPage_Loaded(object sender, RoutedEventArgs e) {
BuildApplicationBar();
}
private void BuildApplicationBar() {
ApplicationBar = new ApplicationBar();
var appBarButtonAdd = new ApplicationBarIconButton(new Uri("/img/add.png", UriKind.Relative)) { Text = AppResources.ABAdd };
appBarButtonAdd.Click += newEntry_Click;
ApplicationBar.Buttons.Add(appBarButtonAdd);
var appBarMenuReview = new ApplicationBarMenuItem(AppResources.ABMarketplace);
appBarMenuReview.Click += review_Click;
ApplicationBar.MenuItems.Add(appBarMenuReview);
}
There are a few implementations of a bindable ApplicationBar for WP7, you might try one of these:
http://www.maxpaulousky.com/blog/archive/2011/01/10/bindable-application-bar-extensions-for-windows-phone-7.aspx
http://dotnetbyexample.blogspot.com/2011/02/case-for-bindable-application-bar-for.html
You haven't included much to work with, but I wrote a pretty good (I think) blog on the subject, including code from start to finish on localizing an app. It covers localization from start to finish, with screenshots and downloadable code, including the application bar.

Windows Phone 7 Back button and application Tombstone?

In my application i had done this to map my uri in app.xaml.cs, now the thing is if my application deactivates my application exits on MainPage.xaml rather than Eula.xaml. Else the app exits on the same page on which it starts.
In App.xaml
<UriMapper:UriMapper x:Name="mapper">
<UriMapper:UriMapping Uri="/MainPageOrEULA.xaml"/>
</UriMapper:UriMapper>
and in App.xaml.cs
// Get the UriMapper from the app.xaml resources, and assign it to the root frame
UriMapper mapper = Resources["mapper"] as UriMapper;
RootFrame.UriMapper = mapper;
// Update the mapper as appropriate
IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (isoStorage.FileExists("DataBase/MyPhoneNumber.txt"))
{
mapper.UriMappings[0].MappedUri = new Uri("/MainPage.xaml", UriKind.Relative);
}
else
{
mapper.UriMappings[0].MappedUri = new Uri("/EULA.xaml", UriKind.Relative);
}
Please guide me for the same.
Regards,
Panache.
I suggest that rather than having a whole separate page (which interrupts navigation as you've noticed), just put a grid or usercontrol containing the EULA on the front page that isn't visible. When the user first opens the page, you show the grid/usercontrol, but on subsequent runs, you don't.
<Grid x:Name="LayoutRoot">
<Grid Name="EULA" Visibility="Collapsed" >
<TextBlock Text = "You agree ...." />
<Button Grid.Row="1" Content="I Agree" Click="AgreeClick" />
</Grid>
<Grid Name="MainGrid" >
....
Then in your code behind you can add your test to the loaded event
private void MainPageLoaded()
{
IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (!isoStorage.FileExists("DataBase/MyPhoneNumber.txt"))
{
EULA.Visibility = Visibility.Visible;
MainGrid.Visibility = Visibility.Collapsed;
}
}
Then when the "I agree" button is clicked you can store the file and show the main grid
private void AgreeClick(....)
{
// Create isolated storage file
....
// Hide eula control
EULA.Visibility = Visibility.Collapsed;
MainGrid.Visibility = Visibility.Visible;
}

OnClick on generated Textblock

Howdy,
I'm generating a bunch of Textblocks in a StackPanel. I would love to open another page when clicking on one Textbox:
sp.Children.Add(new TextBlock { Text = "Click me, I wanna open new content" });
How could I do that, it's probably something with "triggers" but I couldn't find anything on the web :-/.
Thanks!
You could use the Toolkit to add a gesture listener for the Tap event.
Alternatively you could use a HyperlinkButton as this contains a Click event.
Edit:
Example of using HyperlinkButton:
var sp = new StackPanel();
var hlb = new HyperlinkButton {Content = "click me"};
hlb.Click += hlb_Click;
sp.Children.Add(hlb);
ContentPanel.Children.Add(sp);
private void hlb_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/AnotherPage.xaml", UriKind.Relative));
}
Use TextBlock.ManipulationStarted event to detect a touch on it.

Resources