- DOM 요소에 접근할 수 있는 API 제공





Silverlight에서 HTML에 접근하는 것을 실습하기 위해 "RiaHtmlTestPage.html"에서 아래와 같이 소스를 수정하여 실버라이트가 적용되는 영역(사이즈)을 설정한다.






[RiaHtmlTestPage.html]에서 해당부분만 소스코드 수정 및 코딩


#silverlightControlHost {

                      height: 200px; width:300px; border: 1px solid red;

                      text-align:center;

    }

    </style>

 


-------------------------------------------------------------------------------------


<body>

    <form id="form1" runat="server" style="height:100%">

    <h3>실버라이트 영역</h3>

    <div id="silverlightControlHost">

        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">

                                     <param name="source" value="ClientBin/RiaHtml.xap"/>

                                     <param name="onError" value="onSilverlightError" />

                                     <param name="background" value="white" />

                                     <param name="minRuntimeVersion" value="3.0.40818.0" />

                                     <param name="autoUpgrade" value="true" />

                                     <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0" style="text-decoration:none">

                                                      <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>

                                     </a>

                      </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>

    </div>

    <h3>HTML 영역</h3>

    <input type="text" id="txtName" />

    </form>

</body>

 









[MainPage.xaml]


<UserControl x:Class="RiaHtml.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"

    d:DesignHeight="300" d:DesignWidth="400">

 

    <Grid x:Name="LayoutRoot" Background="White">

        <Button x:Name="btnGet" Content="HTML 텍스트박스에서 가져오기" />

    </Grid>

</UserControl>

 

 






[MainPage.xaml.cs]


using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Windows.Browser;

 

namespace RiaHtml

{

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

 

            btnGet.Click += new RoutedEventHandler(btnGet_Click);

        }

 

        void btnGet_Click(object sender, RoutedEventArgs e)

        {

            //[1] HTML 페이지에 대한 정보 얻어오기

            HtmlDocument document = HtmlPage.Document;

            //[!] 예외처리 : 정확한 요소 이름이 기록되었을 때에만 Get/Set

            if (document.GetElementById("txtName") != null)

            {

                //[2] HTML 요소 접근

                HtmlElement txtName = document.GetElementById("txtName");

                //[3] 요소의 읽어오기 : HTML -> Silverlight

                string strName = txtName.GetProperty("value").ToString();

                MessageBox.Show("입력된 : " + strName);

                //[4] Silverlight에서 만들어진 정보를 해당 텍스트박스에 출력 : Silverlight -> HTML

                document.GetElementById("txtName").SetProperty("value", DateTime.Now.ToShortTimeString());

            }

        }

    }

}

 

 







[실행결과]













'.NET프로그래밍 > Silverlight 3.0' 카테고리의 다른 글

Silverlight에서 JavaScript 접근  (0) 2009.12.09
Silverlight에서 CSS 접근  (0) 2009.12.09
로컬 커넥션  (0) 2009.12.08
Navigation Framework 사용  (0) 2009.12.08
Navigation Application  (0) 2009.12.08
Posted by holland14
: