Windows CE R3 Silverlight for Embedded tutorial

Source: Internet
Author: User
Silverlight for Embedded tutorial

Windows Embedded ce 6.0 R3 has been released yesterday, you can download it from here:
Http://www.microsoft.com/downloads/details.aspx? Displaylang = en & familyid = bc247d88-ddb6-4d4a-a595-8eee3556fe46
One of the most exciting new features of R3 is Silverlight for embedded.
With this technology the UI of an application can be described using XAML, an XML-based language, and can be designed using visual tools like expression blend.
This will allow UI designers to work on embedded devices using the same tools they use on the desktop and it will let embedded developers concentrate on the core application features and not on the design of the UI.
I hope that this will lead to always Ally pleasing but also more user friendly interfaces on any kind of embedded device. using visual tools for UI design shoshould also improve development time and allow minor fixes to the UI without code modifications. this will improve device development times and reduce maintenance costs.
This simple tutorial will show how you can design a very basic UI in expression blend 2, load it inside an application and interact with it.
The first step you need to perform is include the components needed to support Silverlight for embedded inside your OS image and rebuild it.
Jochen dieckfo provided a good description of that process on his blog here:
Http://discovertheexperience.blogspot.com/2009/09/windows-embedded-ce-60-r3-using.html
Now that you have an OS image supporting Silverlight for Embedded you can start to design your UI.
Start expression blend and create a new Silverlight application.

You'll have to use expression blend 2 SP1 (thanks Shai for the useful advice !) Because this is the release that support Silverlight 2, the version that is currently supported on embedded devices.
This will create a visual Studio solution (that we don't need for this tutorial ).
As you can see the wizard allows you to choose a language between C # and Visual Basic. silverlight for Embedded supports only C ++ programming and it's currently not integrated with expression blend, so you'll not use the source code generated by this tool.
Remember to disable Visual Studio integration inside expression blend or it will generate C # Or VB. NET code for event handlers etc.

After you created a new project, expression blend will present you with an empty XAML document (named page. XAML by default ).

We can draw a simple button inside it picking it from the rich collection of controls provided by Silverlight.

Using the Properties window we can assign a name to our new button.

The properties toolbox allows you to customize your aspects of your button: It's colors, it's rotation (yes you can have a vertical button or a button turned at 45 degrees !), It's Opacity etc.
We will experiment with all these features in the next tutorials, for now we will keep the button as is and save our XAML.
If you open the XAML file using a text editor or view it using the XAML view inside expression blend you'll see that it's quite simple:

<Usercontrol xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" X: class = "simpleapp. Page"
Width = "640" Height = "480">
<Grid X: Name = "layoutroot" background = "white">
<Button Height = "87" margin = "189,106,209, 0" verticalalignment = "TOP" content = "button"
X: Name = "mybutton"/>
</GRID>
</Usercontrol>

We have a top level container (usercontrol) that uses a grid layout (objects are organized in rows and columns) and contains our button.
The button has attributes for placement inside its own grid cell (the only one we have), alignment, content (the text "button") and its name ("mybutton ").
Now we can start platform builder and add a new subproject to our OS design.
You may develop your Silverlight for embedded application also using Visual Studio 2005 or 2008 smart device application if you generate an SDK from your R3 OS design and install it on your development PC, but for this tutorial we will keep things simple...
Move to the solution tab, right click on the subprojects node and select "Add new ...".
Create a Win32 application.

And choose the simple Win32 Application Template.

This will create an application containing only winmain, it's enough to start experimenting with our first Silverlight for Embedded UI.
The first thing we shoshould do is add the Silverlight for embedded between des to our source code:

# Include "pwinuser. H"
 
# Include "xamlruntime. H"
 
# Include "xrdelegate. H"
 
# Include "xrptr. H"

We will add the XAML that we generate with expression blend to our application resources. in this way we will not need to distibute the XAML file with it. on the other side changes to the XAML will require a rebuild of the application, you may choose the best method to integrate XAML and C ++ source code in your application.
To include resources inside your executable file you need to add a resource script (RC file) to your subproject.
Right click on the subproject in solution view, select "add \ new item" and then select resource file from the dialog box and assign a name to it (usually you have only one resource file per application so using the application name for that file sounds like a good idea ).

Now that you have your resource file you can add the XAML data to it by creating a new resource.
Go to the "resource view" tab, right click on your resource file and select "Add Resource ..."

Now you can import your XAML file data inside your executable. It will be stored inside it (at the end of the file) as binary data.

Type "XAML" as resource type.

For the tutorial you can leave the default ID (idr_xaml1) for that resource, but it's a good idea to give meaningful names to your XAML components in a real project.

To use our resource IDs we need to include "resource. H" inside our. cpp file:

 
# Include "resource. H"

Then, inside our application main function (winmain) We can start to interact with the XAML runtime.
First of all we need to initialize it:

 
If (! Xamlruntimeinitialize ())
 
Return-1;

IfXamlruntimeinitializeSucceeded, the Silverlight for Embedded Runtime is loaded inside your application and it's ready to handle the UI.
Each Silverlight for embedded application has a singleton "application" object that allows us to access global application properties and features.
To access it We shocould useGetxrapplicationinstanceAPI.

 
 Hresult retcode;
 

Ixrapplicationptr app;
 
 
 
If (failed (retcode = getxrapplicationinstance (& App )))
 return-1; 

Someone will notice that we are using hresults and classes that use the "I" prefix and the "PTR" suffix and understand that this new technlogy is based on COM (Component Object Model) that may sound like an alert ent tool in those. net and managed code days but it's still the foundation of your technlogies on both Ce and desktop Windows releases. all Silverlight for embedded objects are implemented as COM objects that export specific interfaces (COM interfaces have names beginning with "I "). since using interfaces directly requires handling of their reference count and this may lead to memory leaks or premature object deletions, com programmers prefer to use "smart Pointers" that are wrappers around the interfaces and manage reference counting internally, destroying the COM object when the smart pointer object goes out of scope inside your c ++ application (function return if it's allocated on the stack, object destruction if it's declared as a class member etc .). smart point classes add the "PTR" suffix to the interface name.
After this 30 seconds introduction to com, we can return to our application.
The first thing we have to do with our application object is tell it where it can find its resources (XAML, images etc .).
We have ded them inside the executable using the resource file and so we can pass our hinstance handle to it:

 
If (failed (retcode = app-> addresourcemodule (hinstance )))
 
Return-1;

Now that we have initialized our application object we can create our main window and let Silverlight for Embedded manage its contents:

Xrwindowcreateparams WP;
 

Zeromemory (& WP, sizeof (xrwindowcreateparams ));
 

WP. Style = ws_overlapped;
 
WP. ptitle = l "s4e test ";
 
WP. Left = 0;
 
WP. Top = 0;
 

Xrxamlsource xamlsrc;

Xamlsrc. setresource (hinstance, text ("XAML"), makeintresource (idr_xaml1 ));
 

Ixrvisualhostptr vhost;
 

If (failed (retcode = app-> createhostfromxaml (& xamlsrc, & WP, & vhost )))
 
Return-1;

The visualhost object "hosts" the runtime and allows us to access its contents and load our XAML from resources (using the xrxamlsource object ).
Now our XA
The object inside our Silverlight for embedded application are organized in a objects tree. To access the object inside it we need a pointer to its root:

Ixrframeworkelementptr root;
 

If (failed (retcode = vhost-> getrootelement (& root )))
 
Return-1;

From the root object we can access our button using the name we assigned to it inside expression Blend:

 
Ixrbuttonbaseptr BTN;
 
 
 
If (failed (retcode = root-> findname (text ("mybutton"), & BTN )))
 
Return-1;

To receive a notification when the user clicks our button we need to provide a delegate. A delegate is a pointer to a member of an istance of a C ++ class that shoshould have a specific prototype.
We can declare a simple C ++ class inside our. cpp source and implement our button click event Delegate inside it:

 
Class btneventhandler
 
{
 
Public:
 

Hresult onclick (ixrdependencyobject * Source, xrmousebuttoneventargs * ARGs)
 
{
 
MessageBox (null, text ("Click! "), Text (" Silverlight for embedded test "), mb_ OK );
Return s_ OK;
 
}
 
};

Our event handler will simply display a message box when the button is clicked.
As you can see the event handler takes two parameters. a pointer to the object that generates the event (Our button) and a pointer to a structure that contains the event parameters.

To connect our event handler to the button we have to create a delegate object:

 
Btneventhandler handler;
 

Ixrdelegate <xrmousebuttoneventargs> * clickdelegate;

If (failed (retcode = createdelegate (& handler, & btneventhandler: onclick, & clickdelegate )))
 
Return-1;
 

If (failed (retcode = BTN-> addclickeventhandler (clickdelegate )))
 
Return-1;

Our Event Handler has been connected to the button, now we can show our UI and wait that the user presses our wonderful button:

 
Uint exitcode;
 

If (failed (retcode = vhost-> startdialog (& exitcode )))
Return-1;
 

Our clickdelegate object is not a smart pointer, so we will have to release it explicitly:

 
Clickdelegate-> release ();

Before we can build our subproject we need to add the include directories and the libraries needed to support Silverlight for embedded inside our application.
Open the subproject sources script by Right clicking on the subproject and choosing "open ".
Add nodes:

 
Includes = $ (_ oemincpath)
 

And Libraries:
 

Targetlibs = \
$ (_ Projectroot) \ cesysgen \ SDK \ Lib \ $ (_ cpuindpath) \ coredll. Lib \
 
$ (_ Projectroot) \ cesysgen \ SDK \ Lib \ $ (_ cpuindpath) \ xamlruntime. Lib \
 
$ (_ Projectroot) \ cesysgen \ SDK \ Lib \ $ (_ cpuindpath) \ UUID. Lib \

(You shoshould already have the targetlibs directive inside your sources script)
Now you can run the application on your device and push that button!

You, like me, are too lazy to type sample code, you can download a zip containing our demo application here:
Http://cid-9b7b0aefe3514dc5.skydrive.live.com/self.aspx/.Public/SilverlightSample.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.