wp7appdevelopment

Adventures in Windows Phone app development

Category Archives: Uncategorized

A+ Math Frenzy mobile app

Looking for a fun way to learn and practice addition, subtraction, multiplication and division? Check out the A+ Math Frenzy mobile app!

Supported operating systems:

  • Windows 10 Mobile
  • Windows Phone 8.1
  • Windows Phone 8

 

 

Entertainment Software Rating Board

Get it from Microsoft

Windows Phone 8.1 Universal Apps: searching, viewing, and rating apps in the store

I’m porting a Windows Phone 8 app to a universal Windows App, and I was looking for the equivalents for MarketplaceSearchTask and MarketplaceDetail Task. Here’s what I found worked for me:

  • Searching in the store:
    Windows.System.Launcher.LaunchUriAsync(new Uri("ms-windows-store:search?keyword=MyKeyword&contenttype=app"));
    

    note: it might be possible to also use the ‘publisher=’ search clause, but I didn’t test this

  • Going to the Rate & Review page for an app:
    Windows.System.Launcher.LaunchUriAsync(new Uri("ms-windows-store:reviewapp?appid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"));
    

    note: if you leave off the ‘appid=’, it should take you to the rate&review page for the current app. But I couldn’t test this because my app isn’t currently published.

  • Going to the page for a specific app in the store:
    Windows.System.Launcher.LaunchUriAsync(new Uri("ms-windows-store:navigate?appid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"));
    

Windows Phone: WrapPanel

The WrapPanel control is available for download as part of the Windows Phone Toolkit (Microsoft.Phone.Controls.Toolkit). Similar to a ListBox, this control positions multiple child elements. Like a ListBox, it positions child elements sequentially — either left to right (horizontally), or top to bottom (vertically). However, with a WrapPanel, when the elements extend beyond the panel’s edge, they are then positioned in the next row or column.

Here are some good resources for learning more about WrapPanel: 

 

 

Tombstoning in Windows Phone 8 Apps

I have a simple Windows Phone game, and my way of dealing with tombstoning is to just start a new game. To do this, I need to first detect that that app was tombstoned. I check for this every time I navigate to a page. If tombstoning has occurred, I navigate to the main page of my app/game and delete the navigation back stack, effectively starting fresh.

NOTE: Because my game is so simple, I don’t need to bother with saving the game state or any data. Most apps will want to save info instead of just starting a new game.

[1] Create a entry in the isolated storage to store a boolean value of whether the app has been tombstoned.

I use a helper class, AppSettings, to store and retrieve values from Isolated Store. See this post for more info. The default value is false.

const string WasTombstonedKeyName = "WasTombstoned";
const bool WasTombstonedDefault = false;
 public bool wasTombstoned
        {
            get
            {
                return GetValueOrDefault<bool>(WasTombstonedKeyName, WasTombstonedDefault);
            }
            set
            {
                AddOrUpdateValue(WasTombstonedKeyName, value);
                Save();
            }
        }

[2] You can check if an app has been activated in the Application_Activated() method of App class (App.xaml.cs). This method is executed when an application is activated (brought to the foreground). It does not execute when the application is first launched.

Add the following code to set the value of our WasTombstoned flag in the (isolated store) to either true or false:

 private void Application_Activated(object sender, ActivatedEventArgs e)
        {
            AppSettings mySettings = new AppSettings();

            if (e.IsApplicationInstancePreserved)
                mySettings.WasTombstoned = false;
            else
                mySettings.WasTombstoned = true;
        }

[3] Modify the OnNavigatedTo method of your MainPage.xaml.cs. You’ll want to clear the WasTombstoned flag, and also clear the navigation backstack.

 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            // Remove entries from the backstack, so that a back key press exits the app.
            try
            {
                while (NavigationService.CanGoBack)
                    NavigationService.RemoveBackEntry();
            }
            catch
            {
            }

            // clear the wasTombstoned flag
            mySettings.wasTombstoned = false;

            base.OnNavigatedTo(e);
        }

[4] On all other pages, edit the OnNavigatedTo method to check for Tombstoning and return to the MainPage if it occurred.

 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            AppSettings myAppSettings = new AppSettings();

            base.OnNavigatedTo(e);

            if (myAppSettings.wasTombstoned)
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

[5] Testing
Assuming you are using Visual Studio to build your app, the easiest way to test tombstoning is to edit the Properties:

  1. In the Solution Explorer, double-click on Properties to open that panel.
  2. Left-click on the Debug tab (left side of panel).
  3. Click the checkbox for “Tombstone upon deactivation while debugging.”

Now, when you are testing, when you leave the application (for example, when you press the windows key or search key on the bottom of the phone) and then return to the application (for example, by pressing the back key), the application will have been tombstoned.

Capabilites Needed for PubCenter Advertising

You need to include the following capabilities in the manifest (WMAppManifest.xml) of apps that use PubCenter ads.

Silverlight Apps: Windows Phone 8

  • ID_CAP_IDENTITY_USER
  • ID_CAP_MEDIALIB_PHOTO
  • ID_CAP_NETWORKING
  • ID_CAP_PHONEDIALER
  • ID_CAP_WEBBROWSERCOMPONENT

Silverlight Apps: Windows Phone 7

  • ID_CAP_IDENTITY_USER
  • ID_CAP_MEDIALIB
  • ID_CAP_NETWORKING
  • ID_CAP_PHONEDIALER
  • ID_CAP_WEBBROWSERCOMPONENT

more info: http://msdn.microsoft.com/en-us/library/advertising-mobile-windows-phone-manifest-capabilities(v=msads.20).aspx

Localize Windows Phone 8 App

Localizing App

First, follow the directions on “How to build a localized app for Windows Phone” post.  You’ll need to:

  • Replace hard-coded strings (textblocks, button content, headers, etc.) with strings from a resource file.  For example:
    <TextBlock Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}" 
        Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
  • Then, in Visual Studio, go to the project’s Properties page, and in the Supported Culture box, select the other languages you want to support.
    After you save the project, Visual Studio will automatically create new resources files (one for each language).  These files will have copies of the original language. You’ll need to edit them with the translated values.

Localizing the App bar

You have a choice: you can define your app bar in the XAML or in the C# page code-behind. But, the XAML app bar doesn’t support dynamic data binding. So, you’ll need to add a method to create the app bar and call it in your constructor or other method.

// Build a localized ApplicationBar
private void BuildLocalizedApplicationBar()
{
    // Set the page's ApplicationBar to a new instance of ApplicationBar.
    ApplicationBar = new ApplicationBar();

    // Create a new button and set the text value to the localized string from AppResources.
    ApplicationBarIconButton appBarButton = 
        new ApplicationBarIconButton(new   
        Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        appBarButton.Text = AppResources.AppBarButtonText;
    ApplicationBar.Buttons.Add(appBarButton);

    // Create a new menu item with the localized string from AppResources.
    ApplicationBarMenuItem appBarMenuItem = 
        new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
    ApplicationBar.MenuItems.Add(appBarMenuItem);
}

 

Localizing App Title

Follow the directions on:

  • “How to localize an app tile for Windows Phone” post.
    [Note: this procedure moves the final *.mui files to the Resources folder. In my testing, this didn’t work. Leaving them in the root directory worked correctly.]
  •   “Language-Neutral Resource Project” post.

I don’t have Visual Studio (only Visual Studio Express), so I needed to download their “Language-Neutral REsource Project. However, in order to compile it, I needed to do the following:

  1. Replace the ‘include “afxres.h” ‘ by ‘ include “winnt.rh” ‘
  2. Delete all included libraries in Project tab, Properties > Linker > Input > Additional Dependencies

Best Example

The best example I found for Windows Phone 8, using Visual Studio Express for Windows Phone 2012 included these instructions.

Applies to: Windows Phone 8 only

This project is a language-neutral resource DLL project. You can use it to create localized language resource strings for your Windows Phone app title and app Tile title.

Important Note:
You can use this language-neutral resource project in Visual Studio Express 2012 for Windows Phone or Visual Studio to create localized app title and app Tile title string for your Windows Phone app. For more information, see How to localize an app title for Windows Phone.

Creating language resource strings for your app

In this set of procedures, you use Visual Studio or Visual Studio Express 2012 for Windows Phone to edit the language-neutral resource string table to contain localized resource title strings. Then, you rebuild the DLL that contains the new localized title strings and rename the DLL file.

Creating the first specific language resource strings

In this procedure, you create localized title strings for English (United States).

To create the first specific language resource strings for your app

  1. Open the DLL project that you downloaded in Visual Studio or Visual Studio Express 2012 for Windows Phone.
  2. In Solution Explorer, expand the Resource Files folder.
  3. Right-click the AppResLib.rc resource, and then click View Code.
  4. Modify the AppResLib.rc file by changing the resource string values as follows:
    ID Value
    AppTitle en-US App Title
    AppTileTitle en-US Tile Title

    The AppTitle string contains the English (United States) name of your app to be displayed in the app list. The AppTileTitle string contains the English (United States) name of your app to be displayed in the app Tile when pinned to Start.

  5. In the Build menu, select Build Solution.
  6. In Solution Explorer, right-click Solution ‘AppResLib’, and then click Open Folder in File Explorer.File Explorer displays the project files.
  7. In File Explorer, open the Release folder, and locate the latest AppResLib.dll file that you built.
    Note:
    The project folder and the solution folder both contain a Release folder. The AppResLib.dll file is contained in the Release folder at the root of the solution. The solution folder is easy to recognize because it contains the AppResLib.sln solution file.
  8. Rename the file AppResLib.dll file to AppResLib.dll.0409.mui.

The DLL file that you created and renamed contains the English (en-US) app title and the app Tile title. Later, you use this file in your Windows Phone app. Your Windows Phone app recognizes this file based on the 0409 LCID value that represents English (en-US).

Creating additional language resource strings

You can create more language resource strings and the DLL that contains those resources by following the preceding procedure. For each localized set of resource strings, you create a new DLL and rename it with the LCID and .mui extension.

Repeat the previous procedure, however, you must provide a localized AppTitle string value and AppTileTitle string value for each locale. You must also rename the DLL file for each locale. Each DLL file name has the format AppResLib.dll.[locale ID].mui. For more information about supported display languages, culture codes, and culture values, see How to localize an app title for Windows Phone.

In the next procedure, you copy the MUI files and language-neutral DLL file to the root directory of your Windows Phone app.

Using the localized resource strings in your Windows Phone app

By adding the localized MUI files and the language-neutral DLL file to your Windows Phone app, app users see the localized app title and the localized app Tile title based on the selected Windows Phone language that they choose for their Windows Phone.

To use the localized resource strings in your Windows Phone app

  1. Open your existing Windows Phone 8 app or create a Windows Phone 8 app in either Visual Studio or Visual Studio Express 2012 for Windows Phone.
  2. In Solution Explorer, select your Windows Phone 8 project.
  3. On the Project menu, click Add, and then click Existing Item.The Add Existing Item dialog box appears.
  4. Find and select the AppResLibLangNeutral.dll file and all the AppResLib.dll.*.mui files that you created in the preceding procedures, and then click Add.The files are added to the root your Windows Phone project.
  5. In Solution Explorer, right-click the AppResLibLangNeutral.dll file and rename it to the following:AppResLib.dll
  6. In Solution Explorer, select the imported files (AppResLib.dll and AppResLib.dll.*.mui).In the Properties window, set the Build Action property to Content.
    Note:
    If the Properties window is not displayed, select Properties Window from the View menu.
  7. In Solution Explorer, expand Properties and then open the WMAppManifest.xml file.The Windows Phone app manifest designer is displayed.
  8. Select the Application UI tab if it’s not already selected.
  9. Set the Display Name to the following:@AppResLib.dll,-100
  10. Set the Tile Title to the following:@AppResLib.dll,-200
  11. Save and build the Windows Phone app.

The Windows Phone 8 app displays the localized app title and the app Tile title based on the language and country settings of the user’s Windows Phone 8 phone. The AppTitle string and AppTileTitle string contained in the .mui files correspond to the language and country setting of the user’s Windows Phone 8 phone. If you don’t include a matching .mui file for a specific supported locale, your Windows Phone 8 app uses the language-neutral AppTitle string and AppTileTitle string contained in the AppResLib.dll file.

Testing the localized title

To test your localized app title, follow the steps listed at How to test a localized app for Windows Phone.

Windows Phone 8: Fast App Resume

Fast App Resume is a feature that controls how an app resumes from Start. When a user launches the app from the Start menu or the App list, what do you want to happen? Do you want a completely new instance of the app to be created? Or do you want to user to be able continue with whatever page they were on when they last used your app?

To enable Fast App Resume, all you need to do is manually edit the WMAppManifest.xml file:

<DefaultTask Name="_default" NavigationPage="MainPage.xaml" ActivationPolicy="Resume|Replace" />

But of course it’s not quite that simple. You also need to think about how users interact with your app and what you want to happen if they re-launch your app from the Start page or App list.   Do you want them to Resume, or get a Fresh Start experience, or do you want it to depend on how long ago they last used your app.

Here’s a great blog post that explains it:

http://blogs.windows.com/windows_phone/b/wpdev/archive/2013/09/19/windows-phone-fast-app-resume.aspx

A more complete example is available from Microsoft:

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

And finally, and sample project you can download:

http://code.msdn.microsoft.com/wpapps/Fast-app-resume-backstack-f16baaa6

Integrating with the Sharing Charm (Windows 8/Win8 Metro)

Simple example of integrating with the Sharing charm in a Windows 8 (Win8 metro) app. This example assumes XAML/C#, and shows how to share plain text.  It’s also possible to share URIs, HTML, formatted text, bitmaps, files, and developer-defined data.

  • The first thing you need to do is add the DataTransfer namespace. This is all that’s needed for sharing text.
using Windows.ApplicationModel.DataTransfer;
  • Next, get the instance of the DataTransferManager class that’s been assigned to the current window:
DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
  • This class supports a datarequested event, which is fired when the user taps the Share charm. You will need to listen for this event to know when the user wants to share something. The following registers an event handler that is called whenever a datarequested event occurs. This handler gets a DataRequestedEventArgs object, which contains the DataRequest object.
dataTransferManager.DataRequested +=
    new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.ShareTextHandler);
  • Next, in the data handler (ShareTextHandler in this example), set the data that the user wants to share:
private void ShareTextHandler(DataTransferManager sender, DataRequestedEventArgs e)
{
    DataRequest request = e.Request;

    // Now add the data you want to share.
    request.Data.Properties.Title = "Share Text Example";
    request.Data.Properties.Description = "A demonstration that shows how to share text.";
    request.Data.SetText("Include your share data here. This will be used by applications (such as email or twitter)");
}

Complete Example:

using Windows.ApplicationModel.DataTransfer;
//To see this code in action, add a call to RegisterForShare to your constructor or other
//initializing function.
private void RegisterForShare()
{
    DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
    dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.ShareTextHandler);
}

private void ShareTextHandler(DataTransferManager sender, DataRequestedEventArgs e)
{
    DataRequest request = e.Request;
    request.Data.Properties.Title = "Share Text Example";
    request.Data.Properties.Description = "A demonstration that shows how to share text.";
    request.Data.SetText("Hello World!");
}

Known bugs:

This approach works fine if you have a single page. But if you navigate to a different page and then navigate back, you’ll get an error when you try to add the handler a second time. The work-around for this is to remove the handler in your OnNavigatingFrom method:


protected override void OnNavigatingFrom(NavigationEventArgs e)
{
DataTransferManager datatransferManager = DataTransferManager.GetForCurrentView();
datatransferManager.DataRequested -= DataRequested;
}

Alternate Approach: Create ShareablePage abstract class

In my app, I had multiple pages. And I wanted the user to be able to share a link to my app from any of these pages. To avoid duplicating the code and making sure I added/removed the handlers each time I navigated to/from a new page, I created a ShareablePage abstract class.

My ShareablePage class has OnNavigatedTo and OnNavigatedFrom methods, which take care of registering and removing the handlers for the Sharing Charm datarequested.

I modified LayoutAwarePage to inherit from my ShareablePage class. (I also needed to modify the OnNavigatedTo/OnNavigatedFrom methods in LayoutAwarePage to call base functions.) Now when I create a new LayoutAwarePage in my app, it automatically is integrated with the Sharing charm.

using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace MyApp
{
    public abstract class ShareablePage : Page
    {
        private DataTransferManager dataTransferManager;
        private TypedEventHandler<DataTransferManager, DataRequestedEventArgs> shareHandler;

        public ShareablePage()
        {
            dataTransferManager = DataTransferManager.GetForCurrentView();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            shareHandler = new TypedEventHandler<DataTransferManager,
                       DataRequestedEventArgs>
                       ((s, args) =>
                       {
                           Share(args.Request);
                       });
            dataTransferManager.DataRequested += shareHandler;
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            dataTransferManager.DataRequested -= shareHandler;
            base.OnNavigatedFrom(e);
        }

        protected void Share(DataRequest dataRequest)
        {
            // Now add the data you want to share.
            dataRequest.Data.Properties.Title = "My App";
            dataRequest.Data.Properties.Description = "Check this out!";
            dataRequest.Data.SetText("... include info to share....");
        }
    }
}

Key steps:

  1. Add ShareablePage class to your project
  2. Modify LayoutAwarePage class:
    • Inherit from ShareablePage class
    • OnNavigatedTo method needs to call base.OnNavigatedTo()
    • OnNavigatedFrom method needs to call base.OnNavigatedFrom()
  3. Create new LayoutAwarePages as usual.

Resources:

For more information on sharing other types of information, like images or files, see “Quickstart: Sharing content (Windows Store apps using C#/VB/C++ and XAML)”

Silverlight Predefined Colors (Windows Phone/Windows 8)

Need an easy way to see the predefined colors in for Silverlight running on WIndows Phone or Windows 8 (WinRT)? 

http://msdn.microsoft.com/en-us/library/system.windows.media.solidcolorbrush(v=vs.95).aspx

Windows 8 (WinRT): Linking to Apps in the Windows Store

If you are building a Windows 8 app and want to link directly to your app’s listing in the Windows Store, just append the Package Family Name of your app to the URL:

ms-windows-store:PDP?PFN=

You can find your Package Family Name in Visual Studio Express 2012 for Windows 8 in the Package.appxmanifest file.

You’ll need to associate your app with the store first. In Visual Studio, use the Store->Associate App with Store command.

Link to Review App in Windows Store

If you want to let the user review your app, append the Package Family Name to this URL:

ms-windows-store:REVIEW?PFN=

For example, you can create a button and create a Click method in the code behind. This example assumes XAML/C#:

In the XAML:

<Button  Content="Review App" Click="Review_Click"/>

In the XAML.cs code behind file:

private void Review_Click(object sender, RoutedEventArgs e)
{
    Windows.System.Launcher.LaunchUriAsync(new Uri("ms-windows-store:REVIEW?PFN=SquugLLC.PuzzleTiles_dm30yjndj09vg"));
}