Udostępnij za pośrednictwem


.NET for Windows Store apps overview

The .NET Framework provides a subset of managed types that you can use to create Windows Store apps using C# or Visual Basic. This subset of managed types is called the .NET for Windows Store apps and enables .NET Framework developers to create Windows Store apps within a familiar programming framework. Any types that are not related to developing Windows Store apps are not included in the subset.

You use these managed types with types from the Windows Runtime API to create Windows Store apps. Typically, you won't notice any differences between using the managed types and the Windows Runtime types except that the managed types reside in namespaces that start with System, and the Windows Runtime types reside in namespaces that start with Windows. Together, the .NET for Windows Store apps and the Windows Runtime provide the complete set of types and members available for developing Windows Store apps with C# or Visual Basic.

The subset of managed types and members was designed with a clear focus on Windows Store app development. As a result, it omits the following:

  • Types and members that are not applicable to developing Windows Store apps (such as console and ASP.NET types).

  • Obsolete and legacy types.

  • Types that overlap with Windows Runtime types.

  • Types and members that wrap operating system functionality (such as EventLog and performance counters).

  • Members that cause confusion (such as the Close method on I/O types).

In some cases, a type that you used in a .NET Framework desktop app doesn't exist within the .NET for Windows Store apps. Instead, you can use a type from the Windows Runtime. For example, the System.IO.IsolatedStorage.IsolatedStorageSettings class isn't included in the .NET for Windows Store apps, but the Windows.Storage.ApplicationDataContainer class provides similar behavior for storing app settings. Examples of common changes you might have to make are included in the section Converting your existing .NET Framework code.

The entire set of assemblies for the .NET for Windows Store apps is automatically referenced in your project when you create a Windows Store app using C# or Visual Basic. Therefore, you can use any of the types supported by the .NET for Windows Store apps in your project without any additional actions. For a list of the combined namespaces provided by the .NET for Windows Store apps and the Windows Runtime (grouped by functional area), see the .NET Framework and Windows Runtime namespaces section.

For a list of namespaces and types included in the .NET Framework subset, see .NET for Windows Store apps APIs.

You can also create a Portable Class Library project to develop a .NET Framework library that can be used from a Windows Store app. The project must include .NET for Windows Store apps as one of the target platforms. The Portable Class Library is particularly useful when you want to develop classes that can be used from apps for different types of platforms, such as a Windows Phone app, desktop app, and Windows Store app. See Cross-Platform Development with the Portable Class Library.

This topic includes the following sections:

  • Converting your existing .NET Framework code

  • Extension methods for converting types

  • .NET Framework and Windows Runtime namespaces

These topics provide information on converting other parts of your app to the Windows Runtime:

Converting your existing .NET Framework code

Typically, you do not simply convert an existing .NET Framework app to a Windows Store app; you redesign the .NET Framework app for the new user experience. However, you may want to convert parts of an existing .NET Framework app for use in a new Windows Store app. When you convert existing .NET Framework code, you should be aware of the following changes you may need to make in your Windows Store app:

  • UI changes

  • I/O changes

  • Storage changes

  • Networking changes

  • Threading changes

  • Reflection changes

  • Security changes

  • Resource changes

  • Exception changes

  • WCF changes

  • Changes in general .NET Framework types

UI changes

When you convert UI code from a Silverlight-based app or Windows Phone app, you can use many of the same UI types, but the types are now located in the Windows.UI.Xaml namespaces instead of the System.Windows namespaces. These new UI types are similar to the previous .NET Framework UI types but contain some different members.

Replace

With

UI types in System.Windows.* namespaces

UI types in Windows.UI.Xaml.* namespaces

(for example, the Border class is located in the Windows.UI.Xaml.Controls namespace)

For more information about porting UI code, see Migrating a Windows Phone 7 app to XAML.

I/O changes

The I/O types include new members to support the new await keyword in the asynchronous programming model.

Replace

With

System.IO.Stream.BeginRead and EndRead methods

System.IO.Stream.ReadAsync method

For an example, see ReadAsync(Byte, Int32, Int32).

System.IO.Stream.BeginWrite and EndWrite methods

System.IO.Stream.WriteAsync method

For an example, see WriteAsync(Byte, Int32, Int32).

Close() method on I/O classes

Dispose() method on I/O classes.

-or-

Declare and instantiate the I/O object within a using (C#) or Using (Visual Basic) statement to ensure that it is properly disposed; for example:

using (StreamReader sr = 
  new StreamReader(await passedFile.OpenStreamForReadAsync()))
{
    while ((nextLine = await sr.ReadLineAsync()) != null)
    {
        contents.Append(nextLine);
    }
}
Using sr As StreamReader = 
  New StreamReader(Await passedFile.OpenStreamForReadAsync())
    While (nextLine = Await sr.ReadLineAsync()) <> Nothing
        contents.Append(nextLine)
    End While
End Using

System.IO.File.ReadAllText method

The ReadTextAsync method in the Windows.Storage.PathIO class

Code to retrieve and open a file

public static async void ReadFileSamples()
{
  // Read a file from package
  StorageFolder packageFolder = 
      ApplicationModel.Package.Current.InstalledLocation;
  StorageFile packagedFile = 
      await packageFolder.GetFileAsync("FileInPackage");

  // Read a file from AppData
  StorageFolder localFolder = ApplicationData.Current.LocalFolder;
  StorageFile localFile = 
    await localFolder.GetFileAsync("FileInAppData");
}
Public Async Shared Sub ReadFileSamples()
  ' Read a file from package
  Dim packageFolder As StorageFolder = 
      ApplicationModel.Package.Current.InstalledLocation
  Dim packagedFile As StorageFile = 
      Await packageFolder.GetFileAsync("FileInPackage")

  ' Read a file from AppData
  Dim localFolder As StorageFolder = ApplicationData.Current.LocalFolder
  Dim localFile As StorageFile = 
    Await localFolder.GetFileAsync("FileInAppData ")
End Sub

Storage changes

Instead of using the System.IO.IsolatedStorage class, use the types in the Windows.Storage namespaces to store local data and files.

Replace

With

System.IO.IsolatedStorage.IsolatedStorageFile class

The LocalFolder property on the Windows.Storage.ApplicationData class

ApplicationData.Current.LocalFolder

System.IO.IsolatedStorage.IsolatedStorageSettings class

The LocalSettings property on the Windows.Storage.ApplicationData class

ApplicationData.Current.LocalSettings

For more information, see Application data.

Networking changes

Replace

With

System.Net.WebClient class

HttpClient class for sending HTTP requests and receiving HTTP responses

-or-

Types in the Windows.Networking.BackgroundTransfer namespace to upload or download large amounts of data

Types in the System.Net.Sockets namespace

Types in the Windows.Networking.Sockets namespace

Relative URIs, when passed to Windows Runtime types

Absolute URIs

For more information, see Passing a URI to the Windows Runtime.

Exception handling code that catches the UriFormatException exception

Code that catches the FormatException exception, which is the parent class of UriFormatException

Threading changes

Some of the .NET Framework threading members have changed, and some types are now available in the Windows Runtime API.

Replace

With

System.Threading.Thread.MemoryBarrier method

Interlocked.MemoryBarrier method in the System.Threading namespace

System.Threading.Thread.ManagedThreadId property

Environment.CurrentManagedThreadId property in the System namespace

System.Threading.Thread.CurrentCulture property

CultureInfo.CurrentCulture property in the System.Globalization namespace

System.Threading.Thread.CurrentUICulture property

CultureInfo.CurrentUICulture property in the System.Globalization namespace

System.Threading.Timer class

Windows.System.Threading.ThreadPoolTimer class

System.Threading.ThreadPool class

Windows.System.Threading.ThreadPool class

Code that queues work to the pool

Task.Run(() => 
{ 
  // work goes here
});
Task.Run(
    Sub()
        ' work goes here
    End Sub)

Code that queues work to the pool and waits for completion

await Task.Run(() => 
{ 
  // work goes here
});
Await Task.Run(
    Sub()
        ' work goes here
    End Sub)

Code that creates a long-running work item

Task.Factory.StartNew(() => 
{ 
  // work goes here
}, TaskCreationOptions.LongRunning);
Task.Factory.StartNew(
    Sub()
        ' work goes here
    End Sub, TaskCreationOptions.LongRunning)

Reflection changes

Most members from the System.Type class have been moved to the System.Reflection.TypeInfo class. You can retrieve the TypeInfo object by calling the System.Reflection.IntrospectionExtensions.GetTypeInfo(System.Type) method, which is an extension method for Type.

Replace

With

type.Assembly

type.GetTypeInfo().Assembly

type.GetMethods(BindingFlags.DeclaredOnly)

type.GetTypeInfo().DeclaredMethods

type.GetMethod("MethodName", BindingFlags.DeclaredOnly)

type.GetTypeInfo().GetDeclaredMethod("MethodName")

type.GetNestedTypes()

type.GetTypeInfo().DeclaredNestedTypes

System.Delegate.CreateDelegate method

MethodInfo.CreateDelegate method

For more information, see Reflection in the .NET Framework for Windows Store Apps in the MSDN Library.

Security changes

Many of the types for security, authentication, and cryptography operations are available through Windows Runtime types. For a complete list of the security namespaces that are available for Windows Store apps, see the security namespaces list later in this topic.

Resource changes

For Windows Store apps, you create a single resource file instead of the hub-and-spoke model that is used in desktop apps. In addition, use the resource types in the Windows.ApplicationModel.Resources and Windows.ApplicationModel.Resources.Core namespaces instead of the System.Resources namespace.

For more information, see Creating and retrieving resources in Windows Store apps.

Exception changes

In some cases, a managed type throws an exception that is not included in the .NET for Windows Store apps. In these cases, you can catch the parent class of the exception that is not included. For example, in a desktop app, you catch the UriFormatException exception to handle an invalid URI; but in a Windows Store app, you catch the FormatException exception because UriFormatException is not included in the .NET for Windows Store apps. FormatException is the parent class of UriFormatException.

WCF changes

In Windows Store apps, you can utilize Windows Communication Foundation (WCF) client functionality to retrieve data from a WCF service, but you cannot create a WCF service to serve data.

Changes in general .NET Framework types

Replace

With

System.Xml.XmlConvert.ToDateTime method

XmlConvert.ToDateTimeOffset method

System.ICloneable interface

A custom method that returns the appropriate type

System.Array.AsReadOnly and System.Collections.Generic.List<T>.AsReadOnly methods

A new instance of the ReadOnlyCollection class, created as follows:

new ReadOnlyCollection<string>(selectedList)
New ReadOnlyCollection(Of String)(selectedList)

Extension methods for converting types

In most cases, you develop Windows Store apps by using .NET Framework types and Windows Runtime types together without any special consideration or conversion. However, in a few cases, the .NET Framework provides extension methods to simplify the interaction between .NET Framework types and Windows Runtime types. These extension methods are in the following classes:

.NET Framework and Windows Runtime namespaces

The following sections list the namespaces provided in the .NET for Windows Store apps and Windows Runtime, organized by functionality.

Collections

Core

Data and content

Devices

Diagnostics

Files and folders

Globalization

Graphics

Managed Extensibility Framework (MEF)

To install the following namespaces, open your project in Visual Studio 2012 or later, choose Manage NuGet Packages from the Project menu, and search online for the Microsoft.Composition package.

Media

Networking

Presentation

Printing

Reflection

Resources

Security

Social

Threading

UI Automation

User interaction

Languages and compilers

See Also

Concepts

.NET for Windows Store apps APIs

Creating Windows Runtime Components in C# and Visual Basic

Other Resources

.NET Framework Support for Windows Store Apps and Windows Runtime