2012年12月18日 星期二

Tips of Windows Runtime Component

Different with .NET dll, the Windows Runtime Component has some limitations when we want to build it using managed code. Here is something talking about creating Windows Runtime Component in managed code from MSDN.

In the section of Declaring types in Windows Runtime Components, we can know Windows Runtime only allowed Windows Runtime types for returned type of public method in the component, and something we should notice that the implementation of component. Following sample code may be help us knowing more easily.

 public sealed class WinRTClass  
 {  
   private List<string> _frusts;  
   public WinRTClass()  
   {  
     _frusts = new List<string> { "Apple", "Pear", "Strawberry", "Blueberry", "Plum" };  
   }  
   public List<string> GetFrusts()  
   {  
     return _frusts;  
   }  
 }  
This is very simple class for Windows Runtime Component, it has a private list to saving strings and a method to return that object. But it will show a compiler error when we build this project.
 Method 'testRTCompoment.WinRTClass.GetFrusts()' has a parameter of type 'System.Collections.Generic.List<System.String>' in its signature. Although this generic type is not a valid Windows Runtime type, the type or its generic parameters implement interfaces that are valid Windows Runtime types. Consider changing the type 'System.Collections.Generic.List<T>' in the method signature to one of the following types instead: 'System.Collections.Generic.IList<T>, System.Collections.Generic.IReadOnlyList<T>, System.Collections.Generic.IEnumerable<T>'.  
It shows the return type List<T> in its signature, this generic type is not a valid Windows Runtime type.

Because of coding managed code in Windows Store app, the compiler (Visual Studio 2012) will enumerate .NET framework types to Windows Runtime types in compiling phase, likes IList<T> will be transfer to IEnumerable<T> automatically. So we can use .NET framework type to coding Windows Store app, but can not use them for component return type to other applications.

We can modify the return type to IEnumerable<T> to commit the requirement of Windows Runtime.
  public sealed class WinRTClass   
  {   
   private List<string> _frusts;   
   public WinRTClass()   
   {   
    _frusts = new List<string> { "Apple", "Pear", "Strawberry", "Blueberry", "Plum" };   
   }   
   public IEnumerable<string> GetEnumFrusts()  
   {  
     return _frusts;  
   }  
  }   

In another sample, we want to use asynchronous task for return type in functional method. But the reason is same of List<T>, the Task<T> is also a .NET framework type not a valid Windows Runtime type.
So we can design the codes like.
 /// it will fail to compiling stage because Task<T> is not a valid Windows Runtime component  
 /*  
 public async Task<IEnumerable<string>> GetBerriesAsync()  
 {  
   return await Task.Run(() => _frusts.Where(x => x.Contains("berry")));  
 }  
 */  
 ///complies OK because it is private  
 private async Task<IEnumerable<string>> GetBerriesAsync()  
 {  
   return await Task.Run(() => _frusts.Where(x => x.Contains("berry")));  
 }  
 ///using WinRT type for accessing method to GetBerriesAsync()  
 public IAsyncOperation<IEnumerable<string>> GetBerriesAsyncOperation()  
 {  
   return GetBerriesAsync().AsAsyncOperation();  
 }  
In GetBerriesAsyncOpeartion method, it uses extension method of Task object which named AsAsyncOperation to enumerate IAsyncOperation.

沒有留言:

張貼留言