- 실버라이트를 통해서 자바스크립트에서 .NET 개체에 접근할 수 있다.
    - 클래스에 [ScriptableType] 특성 부여
    - 멤버에 [ScriptableMember] 특성 부여
    - 생성자에서 HtmlPage.RegisterScriptableObject() 호출
        - HtmlPage.RegisterScriptableObject("bridge", this);
    - 자바스크립트에서 닷넷 개체 사용






여기서는 "MainPage.xaml"은 사용하지 않고, 바로 코드비하인드 페이지인 "MainPage.xaml.cs"에 코드를 작성한다. 





[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 RiaScriptableType

{

    //[1] 닷넷(C#) 메서드를 HTML에서 호출하고자한다면...

    [ScriptableType]

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

            //[3] 자바스크립트에서 사용 가능한 개체로 등록

            HtmlPage.RegisterScriptableObject("RedPlus", this);

        }

 

        //[2] 자바스크립트에 노출시킬 메서드는 ScriptableMember 특성부여

        [ScriptableMember]

        public string GetTime()

        {

            return DateTime.Now.ToShortTimeString();

        }

    }

}

 

 






[RiaScriptableTypeTestPage.html]소스코드 수정


#silverlightControlHost {

                      height: 50px; width:50px; border:1px solid red;

                      text-align:center;

    }

 


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


</script>

    <script type="text/javascript">

        function ShowTime() {

            // 실버라이트를 통해서 C# GetTime() 메서드를 호출하려면...

            var plugin = document.getElementById("sl");

            var now = plugin.content.RedPlus.GetTime();

            alert(now);

        }

    </script>

</head>

 


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


<body>

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

    <div id="silverlightControlHost">

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

                                     <param name="source" value="ClientBin/RiaScriptableType.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>현재 시간을 출력 : C# 메서드로부터 : 자바스크립트에서 C# 메서드를 호출</h3>

    <input type="button" value="현재시간" onclick="ShowTime()" />

    </form>

</body>

</html>

 

















Posted by holland14
: