.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

การใช้งาน Silverlight ครั้งที่ 1 Options · View
paedotnet
Posted: Thursday, January 03, 2008 10:00:29 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 345
Location: bkk

การใช้งาน Silverlight

หลังจากที่ติดตั้ง Microsoft Expression Blend 2 แล้วไปที่ File->New Project จากนั้นจะมีหน้าต่างๆ New->Project เกิดขึ้นมาดังรูป


ให้เลือกโปรเจคเป็น Silverlight Site แล้วตั้งชื่อโปรเจคแล้วกำหนดตำแหน่งที่จะเก็บไฟล์โปรเจคนี้แล้วคลิกที่ปุ่ม Ok
ให้เลือกไฟล์ .xaml (Silverlight จะเป็นส่วนหนึ่งใน WPF นะครับดังนั้นคำสั่งต่างๆจะคล้ายๆกับ WPF)
แล้วเขียนโค้ดดังนี้

<Canvas
 xmlns="http://schemas.microsoft.com/client/2007"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 Width="640" Height="480"
 Background="White"
 x:Name="Page"
 >
<TextBlock FontFamily="Verdana" Foreground="Pink" FontSize="50" Text="Hello Silverlight" Canvas.Left="100" Canvas.Top="50"/>
</Canvas>

 

หลังจากนั้นก็นำไฟล์ .xaml นี้ไปไว้ใน .html เพื่อแสดงผลบน Website ให้คลิกขวาที่ตรง ไฟล์ .html (Default.html) แล้วเลือก Edit in Visual Studioดังรูป

 

จากนั้นจะมีโปรแกรม
Visual Studio เกิดขึ้น แล้วให้เราเขียนโค้ดดังนี้ ( ใน ไฟล์ Default.html)
ให้เราเขียนในส่วนของ แท็ก body ส่วนแท็กอื่นโปรแกรมจะสร้างมาให้อยู่แล้วดังนี้

<body style="background:Orange;">
    <object type="application/x-silverlight" id="silverlightControlHost" width="200" height="100">
 <param name="source" value="Page.xaml" />
 </object>
(โค้ดด้านล่างเป็นโค้ดทั้งหมดของไฟล์ Default.html)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>Test1</title>
 <script type="text/javascript" src="Silverlight.js"></script>
 <script type="text/javascript" src="Default_html.js"></script>
 <script type="text/javascript" src="Page.xaml.js"></script>
 <style type="text/css">
  #silverlightControlHost {
   height: 480px;
   width: 640px;
  }
  #errorLocation {
   font-size: small;
   color: Gray;
  }
 </style>
</head>
<body style="background:Orange;">
    <object type="application/x-silverlight" id="silverlightControlHost" width="200" height="100">
 <param name="source" value="Page.xaml" />
 </object>
</body>
</html>

 

จากนั้นทดสอบแสดงผลใน Browser โดยกดปุ่ม F5  จะได้ดังรูป

คำอธิบาย ในการใส่แท็กของ xaml เข้าไปใน .html เราจะใช้ แท็กที่ชื่อว่า Object  โดยในแท็ก object จะมี attribute ต่างๆดังนี้
type เป็นชนิดของ application ที่ใช้ ซึ่งเราจะใช้ application ของsilverlight ก็เขียนดังนี้ application/x-silverlight
id  เป็น id ของ application นี้
width เป็นขนาดความกว้างของ application นี้เมื่อแสดงใน browser
height เป็นขนาดความสูงของ application นี้เมื่อแสดงใน browser
ส่วนแท็ก param จะมี attrubute  name เพื่อกำหนดชื่อ และ value เพื่อกำหนดไฟล์ .xaml ที่ใช้

 


 



[With great power comes great responsibility]
kokoyull
Posted: Saturday, July 19, 2008 1:47:01 AM
Rank: มือฝึกหัด
Groups: Member

Joined: 4/26/2008
Posts: 8
ดีมากเลยคะที่เอามาสอน
ต้องเริ่มจากพื้นฐานอย่างนี้ค่ะ
สำหรับคนไม่รู้อย่างเราอิอิ
Evil-minded Angel
Posted: Monday, September 08, 2008 5:12:13 PM

Rank: มือฝึกหัด
Groups: Member

Joined: 9/8/2008
Posts: 1
Location: Thailand

ทำไมถึงเรียก New project เป็น WPF ล่ะคะ เพราะนี้ทำ silverlight project เป็น Web App (หรือป่าวค่ะ)

ใน XAML มันจะมี tag <Window></Window> ครอบ <Canvas></Canvas> ไว้ด้วยอ่ะคะ

ทำให้ Run ไม่ผ่าน แอบงงอ่ะ

paedotnet
Posted: Tuesday, September 09, 2008 3:10:25 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 345
Location: bkk

ครับที่จริงแล้วต้องเลือก Silverlight ครับ  ถ้าจะทำเป็น Web App สงสัยผมจะคลิกผิดปุ่มนะครับ
ยังไงต้องขอโทษด้วยนะครับ
 



[With great power comes great responsibility]
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by