if there's any way to make the code in mainpage.xaml.cs visible to app.xaml

if there's any way to make the code in mainpage.xaml.cs visible to app.xaml

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?

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.

  • Back aarticle:No
  • Next aarticle: No
  • Copyright © 2007-2012 www.chuibin.com Chuibin Copyright