Welcome to MSDN Blogs Sign in | Join | Help

Silverlight 1.0とVirual Earthの連携、基礎編: SilverlightとHTML画面を重ね合わる方法

まずは、この図をご覧ください。

VE-SL integration

 プロジェクトの名前が表示されている部分を白い矩形でマスクしていますが、意図的なものです。

さて、このような画面はどうやってつくったらいいでしょうか。

ここで紹介する方法は、Silverlight 1.0および1.1とHTMLページの重ね合わせに利用できるので、新たなマッシュアップに応用できると思います。

もったいぶっても意味がありませんので、簡潔にからくりを説明します。

(1)Silverlight.createObject()を呼び出す際に、IsWindowlessプロパティをtrueに設定し、背景色を無色にする

[createSilverlight.js]
function createMySilverlightPlugin()

    Silverlight.createObject(
        "Page.xaml",                  // Source property value.
        parentElement,                  // DOM reference to hosting DIV tag.
        "mySilverlightPlugin",         // Unique plugin ID value.
        {                               // Per-instance properties.
            width:'960',                // Width of rectangular region of
                                        // plugin area in pixels.
            height:'540',               // Height of rectangular region of
                                        // plugin area in pixels.
            inplaceInstallPrompt:false, // Determines whether to display
                                        // in-place install prompt if
                                        // invalid version detected.
            background:'#00000000',       // Background color of plugin.
            isWindowless:'true',       // Determines whether to display plugin
                                        // in Windowless mode.
            framerate:'15',             // MaxFrameRate property value.
            version:'1.0'               // Silverlight version to use.
        },
        {
            onError:null,               // OnError property value --
                                        // event handler function name.
            onLoad: null
                                        // event handler function name.
        },
        null);                          // Context value -- event handler function name.
}

(2)DIV要素を使い、Virtual Earthの配置位置を決め、z-indexを0とする
(3)DIV要素を使い、Silverlightプラグインの配置位置を決め、z-indexを1とする

この二つの要件をまとめると、次のようなHTMLを用意すればできあがります。ここでは、(2)と(3)に対して、同じ配置位置を指定して重ね合わせを実現します。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head> 

<script type="text/javascript" src="Silverlight.js"></script>   
<script type="text/javascript" src="createSilverlight.js"></script>
<script type="text/javascript" src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=5"></script>
<script language="javascript" type="text/javascript">
var map = null;
var defaultZoomLevel = 4;
var currentZoomLevel;

function GetMap()
{
    map = new VEMap('myMap');
    map.LoadMap(new VELatLong(37.419369, 145.421603), defaultZoomLevel, 'h', true, VEMapMode.Mode2D, false);
    currentZoomLevel = defaultZoomLevel;
}

</script>
<style type="text/css">
  .VEHost {
   height: 540px;
   width: 960px;
   top:0px; left:0px;
   position: absolute;
   z-index: 0
  } 

  .silverlightHost {
   height: 540px;
   width: 960px;
   top:0px; left:0px;
   position: absolute;
   z-index: 1
  } 

</style>

</head>

<body style="font-weight: normal; font-family: Sans-Serif; vertical-align: baseline; text-align: left; font-size: small;" onload="GetMap();">
 <div id="myMap" class="VEHost"></div>
 <div id="SilverlightControlHost" class="silverlightHost"></div>

<script type="text/javascript">
            var parentElement = document.getElementById("SilverlightControlHost");
            createMySilverlightPlugin();
</script>

</body>
</html>

(4)雲が流れるビデオを再生するためにMediaElementを使い、Opacityプロパティで透過性をもった映像にする

Page.xaml

<Canvas
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Width="960" Height="540" Opacity="1">
  <MediaElement x:Name="BackgroundVideo" Canvas.Top="0" Canvas.Left="0" Source="SLV01.wmv" Width="960" Height="540" Stretch="Fill" AutoPlay="true" Opacity="0.3" />
</Canvas>

(5)ビデオの繰り返し再生が必要な場合は、下記のエントリーを参考にしてください。

Silverlight 1.0 : 試行錯誤を減らすための参考情報 : ビデオと画像の取り扱い

・・・

非常に単純なからくりですが、いろんな応用を考えることができます。まずは、既存のWebアプリケーションとSilverlightを重ね合わせることからスタートするのも面白いと思います。

 

Published Friday, October 05, 2007 2:40 AM by aonishi

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker