共用方式為


How to target multiple versions with your app for Windows Phone 8

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

You can use several different methods to create and maintain an app that targets both Windows Phone 8 and Windows Phone OS 7.1.

  • Create a single Windows Phone OS 7.1 app that runs on both versions.

    You can create a single project that uses only Windows Phone OS 7.1 features and that you test against both versions. This is the “least common denominator” approach. With this approach, your app can’t use any of the enhancements or new features introduced in Windows Phone 8.

  • Create a separate copy of the app for each version.

    You can create separate projects that target different versions. With this approach, it’s difficult to maintain two separate versions of the app and to replicate every update that you make in two places.

  • Create two copies of the app that share a class library of common code.

    You can create two projects in the same solution and refactor common code into a shared class library project. The class library project must target Windows Phone OS 7.1. With this approach, your shared class library can’t use any of the enhancements or new features introduced in Windows Phone 8.

  • Create two copies of the app that share linked source code files.

    You can create two projects in the same solution that share source code. Then you can use conditional compilation constants in your code to isolate app features that are new in Windows Phone 8.

This topic contains the following sections.

 

Creating a single Windows Phone OS 7.1 app that runs on Windows Phone 8 and Windows Phone OS 7.1 

The easiest way to create an app or game that runs on both Windows Phone 8 and Windows Phone OS 7.1 is to create an app that targets Windows Phone OS 7.1 and test it against both versions. However, this app can’t use new features or APIs introduced in Windows Phone 8.

To create a single Windows Phone OS 7.1 app that runs on Windows Phone 8 and Windows Phone OS 7.1 

  1. Using Windows Phone SDK 8.0, create an app that targets Windows Phone OS 7.1.

  2. Test the app using the Windows Phone OS 7.1 Emulator provided with Windows Phone SDK 8.0. Also deploy and test the app on a Windows Phone OS 7.1 device.

  3. Test the app using the Windows Phone 8 Emulator provided with the Windows Phone SDK 8.0. Also deploy and test the app on a Windows Phone 8 device.

An alternate approach is to factor common logic into a library project that targets Windows Phone OS 7.1. Then reference this library project from both the Windows Phone OS 7.1 and the Windows Phone 8 app projects.

Creating separate copies of an app for Windows Phone 8 and Windows Phone OS 7.1 

Another way to create an app or game that runs on both Windows Phone 8 and Windows Phone OS 7.1 is to create an app that targets Windows Phone OS 7.1, and then create a Windows Phone 8 version of the same app. Each app resides in a separate project in Visual Studio, and each project is maintained separately. This approach doesn’t benefit from code reuse because you have to make improvements in separate copies of the app.

To create separate copies of an app for Windows Phone 8 and Windows Phone OS 7.1 

  1. Using Windows Phone SDK 8.0, create an app that targets Windows Phone OS 7.1.

  2. Make a copy of the app project in the file system.

  3. Open the copy in Visual Studio and upgrade the project to Windows Phone 8. For more information, see How to upgrade an app project to Windows Phone 8.

  4. Develop, test, and maintain each copy of the app separately. However, you can combine both projects in a single solution in Visual Studio.

Creating two copies of the app that share a class library of common code.

You can create two projects that share a common class library. This solution reduces duplicate code. However, since the class library project must target Windows Phone OS 7.1, the shared class library can’t use any of the enhancements or new features introduced in Windows Phone 8.

To create two copies of the app that share a class library of common code.

  1. Using Windows Phone SDK 8.0, create or open an app that targets Windows Phone OS 7.1.

    Optionally, name or rename the Windows Phone OS 7.1 project to distinguish it from the Windows Phone 8 project that you’re going to create in the same solution. For example, you can rename SharedLibraryApp to SharedLibraryApp.WP71.

  2. In the same solution in Visual Studio, add a new Windows Phone Class Library project that targets Windows Phone OS 7.1.

  3. In the app project, add a reference to the new class library project.

  4. Refactor as much code as possible from the original app project into the class library.

  5. In the same solution in Visual Studio, add a new app project that targets Windows Phone 8.

  6. In the new app project, add a reference to the shared class library project.

Creating two copies of the app that share linked source code files

You can create two linked projects that share code, and use conditional compilation to isolate version-specific sections. This solution minimizes duplication, helps ensure consistency, and makes maintenance easier.

To create two copies of the app that share linked source code files

  1. Using Windows Phone SDK 8.0, create or open an app that targets Windows Phone OS 7.1.

    Optionally, name or rename the Windows Phone OS 7.1 project to distinguish it from the Windows Phone 8 project that you’re going to create in the same solution. For example, you can rename MultiVersionApp to MultiVersionApp71.

  2. In File Explorer, create a copy of the project in the same solution folder.

  3. In File Explorer, rename the new project’s folder and project file to MultiVersionApp80.

  4. In Visual Studio, in Solution Explorer, right-click on the solution. From the context menu, select Add, then Existing Project.

  5. In the Add Existing Project dialog box, navigate to the new project MultiVersionApp80. Select the project and click Open.

    The project is added to the solution.

  6. In Solution Explorer, right-click on the MultiVersionApp80 project and select Upgrade to Windows Phone 8.0.

    The project is upgraded. The solution now contains a copy of the app that targets Windows Phone OS 7.1, and a copy of the app that targets Windows Phone 8. The next step is to share code.

  7. In Solution Explorer, in the Windows Phone 8 project, delete the files that you want to share between projects. For example, you can share XAML pages and their code-behind files. You may also be able to share JPG and PNG graphics, when the resolution of the images is correct for both versions.

  8. In the Windows Phone 8 project, link to the files in the Windows Phone OS 7.1 project that you want to share between projects. You can use two different methods to add files as links:

    • In the Windows Phone 8 project, for each file that you want to link, right-click on the project, and then select Add. Select Existing Item. In the Add Existing Item dialog box, select the file. Click the arrow next to the Add button to select Add As Link. With this method, you can link multiple files at one time.

      - OR -

    • In the Windows Phone OS 7.1 project, select the file that you want to link. Hold down the Alt key and drag the file into the Windows Phone 8 project. The mouse pointer changes to indicate that links will be added. Release the left mouse button. With this method, you can only link one file at a time.

    A link to the file is added to the project with an icon to indicate that the file is linked, as show in the following illustration:

  9. Define a conditional compilation symbol by using the #define directive in your code or the Build page of Project Designer. For example, define WP8 in the Windows Phone 8 project.

  10. Use conditional compilation directives such as #if WP8 … #endif in your shared code files to isolate sections of code that use new features introduced in Windows Phone 8.

  11. Test and debug each project with the emulator and devices for the appropriate Windows Phone version.