locked
Changed on Pivot, event handler not found on class myApp.App RRS feed

  • Question

  • Hello,

    I have a ApplicationBar that Changes on Pivot Index. My problem is that the Click-function resides in my MainPage.xaml.cs and it's not visible to the section in App.xaml where the Shell:applicationbar has to be.

    Code.

    app.xaml

    <shell:ApplicationBar x:Key="StartAppBarSave" IsVisible="True" IsMenuEnabled="True" >
    <shell:ApplicationBarIconButton IconUri="/Images/save.png" Text="Save" Click="myButtonSave" />
    </shell:ApplicationBar>

    Mainpage.xaml.cs

            private async void myButtonSave(object sender, EventArgs e)
            {
    //some code
    }

    Now, I can understand why this is. My question is if there's any way to make the code in mainpage.xaml.cs visible to app.xaml?

    Or how to go about with this. If you have pivot-Control with different applicationbars, is the only want to use the click-function to store all your code in app.xaml.cs? This seems a bit strange in my opinion?

    EDIT:

    Here is the example on msdn.

    http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394036(v=vs.105).aspx

    Saturday, July 27, 2013 9:39 AM

Answers

  • The example on MSDN is not ideal for many situations. It defines ApplicationBar resources in App.xaml so that different xaml pages can all refer to the same ApplicationBar, but that makes it awkward to handle a click for an action in a particular page.

    You can define your ApplicationBars directly in MainPage.xaml instead. Put it in the page resources before the LayoutRoot:

        <phone:PhoneApplicationPage.Resources>
            <shell:ApplicationBar x:Key="StartAppBarSave" IsVisible="True" IsMenuEnabled="True" >
                <shell:ApplicationBarIconButton IconUri="/Images/save.png" Text="Save" Click="myButtonSave" />
            </shell:ApplicationBar>
        </phone:PhoneApplicationPage.Resources>
    
        <!--LayoutRoot is the root grid where all page content is placed-->
        <Grid x:Name="LayoutRoot" Background="Transparent">
    


    Your MainPage.xaml.cs code will set it up like this:

                ApplicationBar = (ApplicationBar)Resources["StartAppBarSave"];

    If you are using multiple ApplicationBars in the pivot page, it's the same concept as in the MSDN article, but the resources will be in MainPage.xaml instead of App.xaml.


    Richard Woo

    • Marked as answer by coffeebakery Sunday, July 28, 2013 12:47 PM
    Saturday, July 27, 2013 5:11 PM

All replies

  • The example on MSDN is not ideal for many situations. It defines ApplicationBar resources in App.xaml so that different xaml pages can all refer to the same ApplicationBar, but that makes it awkward to handle a click for an action in a particular page.

    You can define your ApplicationBars directly in MainPage.xaml instead. Put it in the page resources before the LayoutRoot:

        <phone:PhoneApplicationPage.Resources>
            <shell:ApplicationBar x:Key="StartAppBarSave" IsVisible="True" IsMenuEnabled="True" >
                <shell:ApplicationBarIconButton IconUri="/Images/save.png" Text="Save" Click="myButtonSave" />
            </shell:ApplicationBar>
        </phone:PhoneApplicationPage.Resources>
    
        <!--LayoutRoot is the root grid where all page content is placed-->
        <Grid x:Name="LayoutRoot" Background="Transparent">
    


    Your MainPage.xaml.cs code will set it up like this:

                ApplicationBar = (ApplicationBar)Resources["StartAppBarSave"];

    If you are using multiple ApplicationBars in the pivot page, it's the same concept as in the MSDN article, but the resources will be in MainPage.xaml instead of App.xaml.


    Richard Woo

    • Marked as answer by coffeebakery Sunday, July 28, 2013 12:47 PM
    Saturday, July 27, 2013 5:11 PM
  • That did it! Thanks (once again!)
    Sunday, July 28, 2013 12:47 PM