wp7appdevelopment

Adventures in Windows Phone app development

Monthly Archives: March 2013

Win8/C#: Programmatically access constants defined in Xaml

If you have constants defined in your Xaml, you can access them programmatically in your C# code-behind file.

For example, assume you have the following definitions in your App.Xaml file:


<Application.Resources>
    <ResourceDictionary>
        <SolidColorBrush x:Key="BlueBrush" Color="#FF0066CC" />
     </ResourceDictionary>
</Application.Resources>

You can access this resource programmatically using C#:

    SolidColorBrush HeaderBrush = Application.Current.Resources["BlueBrush"] as SolidColorBrush;

You can check that a resource exists before assigning it, if needed:

    if (Application.Current.Resources.Contains("BlueBrush")
        SolidColorBrush HeaderBrush = Application.Current.Resources["BlueBrush"] as SolidColorBrush;

Windows 8 Apps: Add Rate and Review button to AppBar

Users will be able to review your game through the App Store and from the Settings Page of your published app. But it’s useful to add additional options to your app to prompt users to review your app. I like to add an option to the AppBar, and also prompt the user to review after they’ve played a number of games.

Here’s how you can add a Rate and Review button to the top AppBar in a Windows 8 (Metro) app:

  1. If you haven’t already done so, Choose Store->Associate App with Store.
  2. Add code to the Page.xaml file. For example:
         <Page.TopAppBar>
            <AppBar x:Name="TopAppBar" Padding="10,0,10,0" AutomationProperties.Name="Bottom App Bar">
                <Grid>
                    <StackPanel  Orientation="Horizontal" Grid.Column="1" HorizontalAlignment="Left">
                        <Button  Style="{StaticResource FavoriteAppBarButtonStyle}" Tag="Rate" Click="Review_Click"/>
                    </StackPanel>
                </Grid>
            </AppBar>
        </Page.TopAppBar>
    
  3. Get the Package Family name for your app:
    • Open the Package.appxmanifest file.
    • Go to the Packaging tab.
    • Copy out the Package family name:
  4. Add the Review_Click method to the page’s code-behind file, using your package family name:
            private void Review_Click(object sender, RoutedEventArgs e)
            {
                Windows.System.Launcher.LaunchUriAsync(new Uri("ms-windows-store:REVIEW?PFN=<ENTER YOUR PACKAGE FAMILY NAME>"));
            }
    

Adding About Page to Windows 8 App

  1. Add reference for Callisto library:
    • References -> Manage MuGet Packages
    • search online for “Callisto”
    • click ‘Install’
  2. Add ‘using’ statements to MainPage.xaml.cs:
     
    using Callisto.Controls;
    using Windows.UI.ApplicationSettings;
    
  3. In MainPage.xaml.cs, modify the OnNavigatedTo method to add a handler for the Settings Charm:
    SettingsPane.GetForCurrentView().CommandsRequested += GroupedItemsPage_CommandsRequested;
  4. Create a UserControl named ‘AboutUserControl’:
    • Add -> New Item
    • select User Control, and change the name to ‘AboutUserControl’
    • Edit AboutUserControl.xaml to contain the information you want to include in your About Page.
  5. In MainPage.xaml.cs, add the following methods. Note: if you are using advertising, you will need to uncomment the code to explicitly hide/display ads when the flyout is opened and closed.
     void GroupedItemsPage_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
     {
         AddSettingsCommands(args);
     }
    
      public static void AddSettingsCommands(SettingsPaneCommandsRequestedEventArgs args)
            {
                args.Request.ApplicationCommands.Clear();
    
                // add a link to our privacy policy
                SettingsCommand privacyPref = new SettingsCommand("privacyPref", "Privacy Policy", (uiCommand) =>
                {
                    Windows.System.Launcher.LaunchUriAsync(new Uri("http://www.squug.com/privacy.html"));
                });
    
                args.Request.ApplicationCommands.Add(privacyPref);
    
                // use the Callisto library to create/add a new settings flyout for the About panel.
                // Note: need to explicitly hide/display ads when flyout is opened and closed
                SettingsCommand aboutCommand = new SettingsCommand("about", "About", (handler) =>
                {
                    SettingsFlyout settings = new SettingsFlyout();
                    //settings.Closed += (s, e) =>
                    //{
                    //    adControl.Visibility = Visibility.Visible;
                    //};
                    settings.Content = new AboutUserControl();
                    settings.HeaderBrush = new SolidColorBrush(Colors.Yellow);
                    settings.HeaderText = "About";
                    //adControl.Visibility = Visibility.Collapsed;
                    settings.IsOpen = true;
                });
                args.Request.ApplicationCommands.Add(aboutCommand);
            }