Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One of the big features added to Windows Phone 8 is the ability to include native code. There's two kinds of native code you can include. You can include a native DLL, for example, like SQLite. Or you can include a Windows Phone Runtime component. In order to be able to access a native DLL from a managed app, you have to use a Windows Phone Runtime component to get to the DLL. If you're building a C++ main app that uses DirectX3D, you can use your native DLLs directly just as you would in any other C++ environment.
When you include a native DLL, you have to compile it for both x86 and ARM CPU architectures. That's because there are two different kinds of CPUs out there in the world running Windows Phone 8. When you build a Windows Phone runtime component, it can automatically be compiled to what's called Any CPU. Which is subsequently natively compiled by the Windows Phone store infrastructure into the appropriate x86 or ARM architecture that matches the user's device.
When you build a Windows Phone runtime component, you can use the async and await keywords and access WinRT and APIs. You can also use manage compatible data formats such as String. So let's go take a look, and we'll build a simple managed app. We'll add a simple Windows phone runtime component, and be able to instantiate the C++ from our C# code. And then, in the next video, we'll go back and retool our Hello World app to include a call to a C++ module, inside the process of hello world.
So, let's switch over to Visual Studio. Here in Visual Studio, with do file new project, and we'll come up to the visual C++, we'll come down here to windows phone. And just select plain old Windows phone app. And we'll call this one ManagedPlusNative. Click on OK. Now, this is a standard Windows Phone 8 empty shell. But what we want to do now is show the process you need to do to add a Windows Phone runtime component in C++ and hook the two of them together. You go to the Solution, you right-click, you come down to the Add Sub-Menu Item, and select New Project.
We'll come down here to Other Languages, Visual C++, scroll down, select Windows Phone. And one of the templates we have available to us is Windows Phone Runtime Component. So, we click on OK, and we now have a C++ Windows Phone Runtime Component. And you notice that we have one public member which is the constructor. You might think, I can come back to my main page, open up my C++ file, come up here and say using Windows. And, wait a minute, I don't get Intellisense help for Windows Phone Runtime Component 1, so let's erase that.
And there's two reasons for that. The first reason is I need to add a project reference. So I'll come over to our Manage Plus Native Project, right-click, select Add Reference, come to Solution, select our Windows Phone Runtime Component project, and click OK. And now, I also have to come to the solution and select rebuild. Because Visual Studio doesn't know about the members in the methods inside the native library until its been build at least once. And each time, you change it in the sense of adding additional members or methods you have to rebuild before they're visible to Intellisense.
So, now I can come to my C# code and say, using Windows, and there's our Windows Phone runtime component 1, namespace. And, now I can come down into my code and say Windows Phone Runtime Component because that's my class name, WRC equals New Windows Phone Runtime Component. And now if I build this, this builds successfully. So, here we've instantiated an instance of the WindowsPhoneRuntimeComponent in C++, and we could call methods on it from our C# code.
In our next video, we'll start with our existing Hello World application and add some C++ code to it.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101478 Viewers
61 Video lessons · 88242 Viewers
71 Video lessons · 72114 Viewers
56 Video lessons · 103875 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.