SilverLight 页面后台方法XX.xaml.cs 创建JS,调用JS ,弹出提示框

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);
            }
        }

上一篇:C++中内存泄漏的几种情况


下一篇:Windows 远程栈溢出挖掘与利用