I want to add BottomAppBar programatically from code-behind in C#. I got it done like this:
1: Added a resource file that has DataTemplate which contains a Grid, a StackPanel and two buttons.
2: In my BasePage.cs (that derives from the Page class) I define a new AppBar and set it’s ContentTemplate to the Resource created in step1.
3: I set this.BottomAppBar= AppBar from step2.
Now this add the AppBar to all my pages that are derived from BasePage. This works fine.
The issue:
I cannot get the PointerPressed or any other events fired from my two elements in AppBar.
I am sure it’s something very basic I am missing. Any ideas, anyone?
Update: Sample download link added below and what I would like is when the image in the BottomAppBar (Page1 and 2)is clicked, it should take me to MainPage.
AppBar Code
AppBar appbar = new AppBar();
appbar.Name = "BottomBar";
DataTemplate dt = Application.Current.Resources["BottomAppBarDT"] as DataTemplate;
appbar.ContentTemplate = dt;
this.BottomAppBar = appbar;
In Data Templating its always best to implement Commands instead of Event Handlers.
What you will want to do is make sure that you create a DelegateCommand that has the same methodology as the Button Click Event you are attempting to trigger.
The code for the DelegateCommand should be placed in the View Model that serves as the DataContext for the parent of this particular control (App Bar) or If you’re using an MVVM structure that has a Command Aggregator it should go in there.