.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 System.Diagnostics.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. .
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).
For a list of namespaces and types included in the .NET Framework subset,
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.
This topic includes the following sections:
- Converting your existing .NET Framework code
- Extension methods for converting types
- .NET Framework and Windows Runtime namespaces
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)
|
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
|
System.IO.Stream.BeginWrite and EndWrite methods
|
System.IO.Stream.WriteAsync method
|
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:
|
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"); } |
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 |
Networking changes
Replace
|
With
|
|---|---|
System.Net.WebClient class
|
System.Net.Http.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
|
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
| |
Code that queues work to the pool and waits for completion
| |
Code that creates a long-running work item
|
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
|
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.
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 System.Collections.ObjectModel.ReadOnlyCollection<T>class, created as follows:
|
No comments:
Post a Comment