locked
how to reuse regular .NET project into Windows Runtime Component

    Question

  • continuing on my attempt to learning how to Wrap a .Net library in a Windows Runtime Component,  I have created .net library that has a static method which returns List<string>. I need to access this code from Windows Runtime component. I guess my very first step in order to reuse regular .net code into Windows Runtime component is to add reference of regular .net library project into this WinRT project. However when I try to do it I get error stating "unable to add a reference to project MyDotNetLibrary". Is this not possible? Without doing this, I am confused as what does wrapping a .NET Library in a Windows Runtime Component mean. Does it mean to change the regular .NET Library project type to Windows Runtime project and re-compile the whole thing?
    Sunday, February 23, 2014 1:57 PM

Answers

  • Create a Windows Runtime Component.

    In the Windows Runtime Component reference your .Net library (either a .Net for Windows Store apps library or a Portable Class Library).

    In the Windows Runtime Component create classes and methods which expose the information you want to make available from the .Net library.

    For example, if we have the following .Net for Windows Store app class library:

    namespace DotNetClassLibrary1
    {
        public class DotNetClass1
        {
            public int Multiply(int x,int y)
            {
                return x * y;
            }
        }
    }

    We can reference it in a Windows Runtime Component:

    namespace WindowsRuntimeComponent1
    {
        public sealed class WRCClass1
        {
            DotNetClassLibrary1.DotNetClass1 dotNetClass;
            public WRCClass1()
            {
                dotNetClass = new DotNetClassLibrary1.DotNetClass1();
            }
            public int Multiply(int x,int y)
            {
                return dotNetClass.Multiply(x, y);
            }
        }
    }
    

    See Walkthrough: Creating a simple component in C# or Visual Basic and calling it from JavaScript for more details on creating and calling a Windows Runtime Component.

    --Rob

    Monday, February 24, 2014 10:02 PM
    Owner

All replies

  • Your .Net library must be built for the .NET for Windows Store apps or as a Portable Class Library.

    You cannot use libraries built against the full .Net Framework. Classes such as System.Drawing are not available to Windows Store apps.

    --Rob

    Sunday, February 23, 2014 8:07 PM
    Owner
  • OK. So what exactly you mean when say that .NET library could be wrapped in a Windows run time component? does it mean creating a portable class library and move your .NET code into this portable class library?
    Monday, February 24, 2014 1:53 PM
  • Create a Windows Runtime Component.

    In the Windows Runtime Component reference your .Net library (either a .Net for Windows Store apps library or a Portable Class Library).

    In the Windows Runtime Component create classes and methods which expose the information you want to make available from the .Net library.

    For example, if we have the following .Net for Windows Store app class library:

    namespace DotNetClassLibrary1
    {
        public class DotNetClass1
        {
            public int Multiply(int x,int y)
            {
                return x * y;
            }
        }
    }

    We can reference it in a Windows Runtime Component:

    namespace WindowsRuntimeComponent1
    {
        public sealed class WRCClass1
        {
            DotNetClassLibrary1.DotNetClass1 dotNetClass;
            public WRCClass1()
            {
                dotNetClass = new DotNetClassLibrary1.DotNetClass1();
            }
            public int Multiply(int x,int y)
            {
                return dotNetClass.Multiply(x, y);
            }
        }
    }
    

    See Walkthrough: Creating a simple component in C# or Visual Basic and calling it from JavaScript for more details on creating and calling a Windows Runtime Component.

    --Rob

    Monday, February 24, 2014 10:02 PM
    Owner