• 使用Javascript调用Silverlight


    一、 在Silverlight注册一个Javascript脚本对象,并将C#方法暴露出来,就可以了。

    注册脚本对象:

    HtmlPage.RegisterScriptableObject("Builder",this);

    定义脚本成员:

    [ScriptableMember]
    public void CreateRect(int width,int height)
    {
       Rectangle rect = new Rectangle();
       rect.width = width;
       rect.height = height; 
       rect.Fill = new SolidColorBrush(Colors.Blue);
       LayoutRoot.Children.Claer();
       LayoutRoot.Children.Add(rect);
    }
    

    二、 在Html中定义一个Silverlight对象,通过javascript获取此对象,用此对象来调用CreateRect方法创建矩形。

    创建Silverlight对象:

    <Object id="XamlObject" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="400" height="300">
    <param name="source" value="ClientBin/Sample.xap"/>
    <param name="onerror" value="onSilverlightError"/>
    <param name="background" value="white"/>
    <param name="minRuntimeVersion" value="2.0.31005.0"/>
    <param name="autoUpgrade" value="true"/>
    </Object>

    通过点击按钮,调用该对象的方法:

    <!--html-->
    <input id="button1" type="button" value="生成矩形" onclick="createRectangle();">
    <!--javascript-->
    function createRectange(){
       var xamlObject = document.all('xamlObject');
       xamlObject.content.Builder.CreateRect(100,200);
    }
    
  • 相关阅读:
    第三章 套接字编程简介
    Effective STL 学习关于容器
    C++单例模板
    第六章 IO复用:select和poll函数
    第五章 TCP客户服务器程序示例
    八、基本UDP套接字编程
    第四章 基本TCP套接字编程
    测试 LaTeX
    如何在不支持 LaTeX 的环境使用 LaTeX 命令输入公式?
    Ubuntu 下 Wine 安装微软 Office
  • 原文地址:https://www.cnblogs.com/rentianlong/p/2552598.html
Copyright © 2020-2023  润新知