您的位置:首页 > 其它

Silverlight for Windows Embedded tutorial (三)

2009-12-11 13:50 357 查看
After the first two tutorial steps were published on this blog I received many requests about using images inside a Silverlight for Windows Embedded application. This is the topic of this post.
To be able to load and use image files (jpegs,bmps,gifs) inside your application you should include the imaging library components in your OSDesign.
Those component are not included automatically when you add the XAML runtime (the runtime can run also without the imaging components, it will simply not load your images!).
To display an image inside your application user interface you have to use the image control of Silverlight.
This is a very simple XAML file that includes just an Image object and a button:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ImgTest.Page"
Width="640" Height="480" x:Name="ImagePage">
<Grid x:Name="LayoutRoot" Background="White">
<Image Margin="17,25,25,103" x:Name="MyImage" Source="/Windows/img01.JPG"/>
<Button Height="49" Margin="259,0,253,28" VerticalAlignment="Bottom" Content="Button" x:Name="MyButton" Click="OnClick"/>
</Grid>
</UserControl>

The image object is named "MyImage" and the button is named "MyButton".
We also have an OnClick event handler for the Click event of the button.
We can create a new Win32 application inside platform builder.
Using the XAML2CPP we can generate some code for us.
We will just have to include "XAML2CPP.h" inside your main C++ source file to use the code that XAML2CPP created for us:
#include "XAML2CPP.h"
We also have to include "XAML2CPP.rc" inside the rc file of our application to include the XAML code as a resource inside our application.
XAML2CPP has created a base class that we can use to implement our own class and handle the OnClick event:
class ImagePage : public TImagePage<ImagePage> { ... }

In this sample we will just swap two images inside the image control each time you click on the button.
We have to declare a state flag (to be able to swap images) and two IXRBitmapImagePtr objects to store our bitmaps.
bool state; IXRBitmapImagePtr img01; IXRBitmapImagePtr img02;
A very simple constructor will reset the state:
ImagePage() { state=false; }
The main initialization will be performed inside our Init method (it's not a good idea to put this kind of initialization code inside the constructor because some API calls may fail and you don't have a way to return an error code from a C++ constructor).
virtual HRESULT Init(HINSTANCE hinstance,IXRApplication* app) { ... }
Inside our Init method we have to call the Init method of our base class (declared by XAML2CPP), and check its return code for errors:
HRESULT retcode; if (FAILED(retcode=TImagePage<ImagePage>::Init(hinstance,app))) return retcode;
We declared two IXRBitmapImagePtr objects but we still haven't initialized them.
To create a Silverlight for Windows Embedded object we should use the CreateObject method of the IXRApplication object:
if (FAILED(retcode=app->CreateObject(IID_IXRBitmapImage,&img01))) return retcode; if (FAILED(retcode=app->CreateObject(IID_IXRBitmapImage,&img02))) return retcode;
The we can load the bitmaps and store them inside the IXRBitmapImagePtr objects:
if (FAILED(retcode=img01->SetUriSource(TEXT("//Windows//img01.jpg")))) return retcode; if (FAILED(retcode=img02->SetUriSource(TEXT("//Windows//img02.jpg")))) return retcode;
In the OnClick event handler we just have to swap the image displayed by the MyImage object and change the state flag:
HRESULT OnClick(IXRDependencyObject* source,XRMouseButtonEventArgs* args) { HRESULT retcode; if (FAILED(retcode=MyImage->SetSource(state?img01:img02))) return retcode; state=state?false:true; return S_OK; }
The main function of this sample is quite simple and it's not much different for the WinMain functions of the previous samples (just the class name changes):
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { if (!XamlRuntimeInitialize()) return -1; HRESULT retcode; IXRApplicationPtr app; if (FAILED(retcode=GetXRApplicationInstance(&app))) return -1; ImagePage imagepage; if (FAILED(imagepage.Init(hInstance,app))) return -1; UINT exitcode; if (FAILED(imagepage.GetVisualHost()->StartDialog(&exitcode))) return -1; return 0; }
You can download the full source code of this sample here:
http://cid-9b7b0aefe3514dc5.skydrive.live.com/self.aspx/.Public/ImageTest.zip
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: