1.Invoke和InvokeSelf

[c-sharp] view plaincopy

  1. public partial class CreateJSDemo : UserControl
  2. {
  3. public CreateJSDemo()
  4. {
  5. InitializeComponent();
  6. string jsText = @"function callJs(msg){alert(msg);}"; //function可以是在此处创建,也可以是已经在页面存在的function
  7. HtmlElement element = HtmlPage.Document.CreateElement("Script");
  8. element.SetAttribute("type", "text/javascript");
  9. element.SetProperty("text", jsText);
  10. HtmlPage.Document.Body.AppendChild(element);
  11. }
  12. private void btn_Invoke_Click(object sender, RoutedEventArgs e)
  13. {
  14. HtmlPage.Window.Invoke("callJs", "Haha");
  15. }
  16. private void btn_InvokeSefy_Click(object sender, RoutedEventArgs e)
  17. {
  18. ScriptObject script = HtmlPage.Window.GetProperty("callJs") as ScriptObject;
  19. script.InvokeSelf("God bless you!");
  20. }
  21. }

2.CreateInstance

[c-sharp] view plaincopy

  1. public partial class CallJsByCreateInstance : UserControl
  2. {
  3. public CallJsByCreateInstance()
  4. {
  5. InitializeComponent();
  6. string jsText = @"
  7. jsObject = funciton callJs(msg)
  8. {
  9. this.Msg=msg;
  10. }
  11. jsObject.property.show=function
  12. {
  13. alert(this.Msg);
  14. }
  15. ";
  16. HtmlElement element = HtmlPage.Document.CreateElement("Script");
  17. element.SetAttribute("type", "text/javascript");
  18. element.SetProperty("text", jsText);
  19. HtmlPage.Document.Body.AppendChild(element);
  20. }
  21. private void btn_CreateInstance_Click(object sender, RoutedEventArgs e)
  22. {
  23. ScriptObject script = HtmlPage.Window.CreateInstance("jsObject", "Haha");
  24. script.Invoke("show");
  25. }
  26. }

以上是转载 于 http://blog.csdn.net/zfyong/article/details/6317772  博客

本人的例子:

silverlight页面图表有个checkbox选择是否收藏,后台传参给wcf 进行增加,删除操作(收藏,取消收藏),wcf返回结果给silverlight,silverlight根据不同结果调用js弹出提示框

一、silverlight页面

<navigation:Page x:Class="SilverlightApplication2.BeginHome" 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" xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" mc:Ignorable="d" d:DesignWidth="1080" d:DesignHeight="1350" Title="主页" Style="{StaticResource PageStyle}" xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" Loaded="Page_Loaded">
    <StackPanel Height="1350" Name="stackPanel1" Width="1080">

<toolkit:WrapPanel Height="1350" Name="wrapPanel1" Width="1040">
            <Grid Height="320" Name="grid1" Width="510" Margin="1,4,4,1">
                <!--<Button Name="b" Margin="500,26,0,264" Content="收藏" FontSize="10"></Button>-->
                <TextBlock Name="t1" Text="收藏" Margin="454,302,30,0" Height="20"></TextBlock>
                <CheckBox Background="Blue" Name="c1" Height="17" Width="16" Margin="484,302,10,0" Click="c1_Click"></CheckBox>
            </Grid>

</toolkit:WrapPanel>
    </StackPanel>
</navigation:Page>

二、silverlight后台

(1)Click方法

private void c1_Click(object sender, RoutedEventArgs e)//图表一
        {
            DBServiceClient svc = new DBServiceClient();

if (c1.IsChecked == true) //勾选
            {
                svc.InsertStoreClickCompleted += new EventHandler<InsertStoreClickCompletedEventArgs>(svc_InsertStoreClickCompleted);
                svc.InsertStoreClickAsync("XXX柱状图");//收藏
            }

else//取消勾选
            {
                svc.DeleteClickCompleted += new EventHandler<DeleteClickCompletedEventArgs>(svc_DeleteClickCompleted);
                svc.DeleteClickAsync("XXX柱状图");//取消收藏
            }
        }

(2)获取wcf 返回结果方法 , 调用js并弹出相应内容的提示框

private void svc_InsertStoreClickCompleted(object sender, InsertStoreClickCompletedEventArgs e)
        {
            int n = e.Result;
            //CreateJSDemo();
            if (n == 0)
            {
                HtmlPage.Window.Invoke("callJs", "收藏失败");
            }
            else
            {
                HtmlPage.Window.Invoke("callJs", "收藏成功");
            }
        }

private void svc_DeleteClickCompleted(object sender, DeleteClickCompletedEventArgs e)
        {
            int n = e.Result;
            //CreateJSDemo();
            if (n == 0)
            {
                HtmlPage.Window.Invoke("callJs", "取消收藏失败");
            }
            else
            {
                HtmlPage.Window.Invoke("callJs", "取消收藏成功");
            }
        }

(3)创建js方法

public void CreateJSDemo() //        {
            //InitializeComponent();
            string jsText = @"function callJs(msg){alert(msg);}"; //function可以是在此处创建,也可以是已经在页面存在的function 
            HtmlElement element = HtmlPage.Document.CreateElement("Script");
            element.SetAttribute("type", "text/javascript");
            element.SetProperty("text", jsText);
            HtmlPage.Document.Body.AppendChild(element);
        }

CreateJSDemo()此方法可以在 本页面初始时调用即

public BeginHome()
        {
            InitializeComponent();
            CreateJSDemo();

……

    ……

}

三、wcf  方法

(1)增加方法

[OperationContract]
        [ServiceKnownType(typeof(ListItem))]
        public int InsertStoreClick(string sChartName)
        {
            IDataBase oDB = DBFactory.GetDBInstance();
            try
            {
                strUserid = "123456" ;

strAppid = "……";

if (strAppid == null || strUserid == null || sChartName == null)
                {
                    return 0;
                }
                else
                {
                    DataTable dt;
                    string sSQL = string.Format("select 1 from  表 where  userid='{1}'and chartname='{2}'",
                        strUserid, sChartName);
                    dt = oDB.GetDataTable(sSQL);
                    if (dt.Rows.Count == 0)
                    {
                        oDB.BeginTran();
                        string insertSql = string.Format("insert into 表 (id,  userid, chartname, createtime) values('{0}','{1}','{2}',SYSDATE)",
                        Guid.NewGuid(),  strUserid, sChartName);
                        oDB.ExecuteNonQueryInTran(insertSql);
                        oDB.CommitTran();
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }

}
            }
            catch (Exception ex)
            {
                oDB.EndTran();
                throw new Exception("错误信息:" + ex.Message);
            }
        }

SilverLight 页面后台方法XX.xaml.cs 创建JS,调用JS ,弹出提示框的更多相关文章

  1. [转] 在Asp.net前台和后台弹出提示框

    一.在前台弹出提示框 1.点击"A"标记或者"控件按钮"弹出提示框 <asp:LinkButton ID="lbtnDel" runa ...

  2. PHP弹出提示框并跳转到新页面即重定向到新页面

    本文为大家介绍下使用PHP弹出提示框并跳转到新页面,也就是大家所认为的重定向,下面的示例大家可以参考下   这两天写一个demo,需要用到提示并跳转,主要页面要求不高,觉得没必要使用AJAX,JS等, ...

  3. iOS bug 之 H5 页面没有弹出提示框

    描述:在安卓上有提示框,但是在iOS上没有提示框. step 1: 失误,是我没有在正确的位置设置网址. step 2: 修改之后,测试页能弹出提示框,但是正式的页面没有提示框. step 3: 我输 ...

  4. WebForm跨页面传值取值、C#服务端跳转页面、 Button的OnClientClick属性和超链接点击弹出警示框

    一.跨页面传值和取值: 1.QueryString - url传值,地址传值 优缺点:不占用服务器内存:保密性差,传递长度有限. 通过跳转页面路径进行传值方式: href="地址?key=v ...

  5. silverlight visifire控件图表制作——silverlight 后台方法ControlChart.xaml.cs

    一.构造方法ControlChart 1.前台页面控件赋值 //时间下拉框赋值,下拉框赋选定值                for (int ii = DateTime.Today.Year; ii ...

  6. 【转载】asp.net 后台弹出提示框

    感觉这种最好用: public void showMessage(string str_Message) { ClientScript.RegisterStartupScript(this.GetTy ...

  7. asp.net 后台弹出提示框

    1.后台弹出提示信息方法 Response.Write("<scripttype="text/javascript">alert('你所查询的数据不存在!') ...

  8. 最完美解决方案:js弹出窗口关闭当前页面,而不弹出提示框

    该功能主要用于业务系统中的登录操作,登录后弹出全屏的业务系统主界面,而不需要工具栏.地址栏.菜单等功能. 之前针对不同浏览器找了无数种方法,包括网上能查到的所有方法,弹出的结果都不理想.结果有的IE6 ...

  9. 有关js弹出提示框几种方法

    1直接提示只有确定功能的提示框 只显示提示信息 alert(“提示信息”); alert ();的参数只有一个就是提示信息,无返回值 2 弹出输入框让你输入内容 prompt() ; 有两个参数:第一 ...

随机推荐

  1. iOS9新特性之UIStackView

    同iOS以往每个迭代一样,iOS 9带来了很多新特性.UIKit框架每个版本都在改变,而在iOS 9比较特别的是UIStackView,它将从根本上改变开发者在iOS上创建用户界面的方式.本文将带你学 ...

  2. Cookie[1]

    1.什么是Cookie Cookie是一小段文本类型的数据,由服务器发送,并保留在客户端的计算机上. 2.Cookie的作用 服务器可以利用Cookie包含的信息来筛选并经常维护这些信息,以判断在Ht ...

  3. (原)anaconda 的安装与在pycharm中的版本切换

    参考网页: http://continuum.io/blog/anaconda-python-3 http://conda.pydata.org/docs/intro.html#creating-py ...

  4. 使用pcs api往免费的百度网盘上传下载文件

    百度个人云盘空间大,完全免费,而且提供了pcs api供调用操作文件,在平时的项目里往里面保存一些文件是很实用的. 环境准备: 开通读写网盘的权限及获取access_token:http://blog ...

  5. IE,火狐,谷歌浏览器下js判断滚动条是否已拉到页面最底部

    E/FF/Chrome下document.documentElement和document.body的 scrollHeight/scrollTop/clientHeight 以及判断滚动条是否已拉到 ...

  6. 关于Linux平台malloc的写时拷贝(延迟分配)【转】

    Linux内核定义了“零页面”(内容全为0的一个物理页,且物理地址固定),应用层的内存分配请求,如栈扩展.堆分配.静态分配等,分配线性地址后,就将页表项条目指向“零页面”(指定初始值的情况除外),这样 ...

  7. git新手碰到的各种奇葩问题之一

    git  操作错误: <1>.情景描述:当在git commit --amend 更新上一次提交时,而此时提交日志会跳转到别人的日志中.,会出现错误:如下 弥补操作: 1.git fetc ...

  8. udp之nat穿透的困惑

    nat穿透实现:[A]内网地址[内A]192.168.1.176:25789通过stun服务器查询映射到的外网地址为外网地址[外A]212.10.55.124:26559UDPsocketA绑定到[内 ...

  9. poj 2229 Ultra-QuickSort(树状数组求逆序数)

    题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...

  10. spring加载properties配置文件

    public static void main(String[] args){    String path="E:/workspace/bocMarketData/src/config/P ...