Silverlight for Windows Embedded tutorial (三)

Silverlight for Windows Embedded tutorial (三),第1张

概述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

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,103" x:name="MyImage" Source="/windows/img01.JPG"/>
        <button Height="49" margin="259,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

总结

以上是内存溢出为你收集整理的Silverlight for Windows Embedded tutorial (三)全部内容,希望文章能够帮你解决Silverlight for Windows Embedded tutorial (三)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1041048.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存