none
Silverlight + wcf遇见的问题 RRS feed

  • 问题

  •  

    我的与语言描述能力有限,希望大家能看明白。

    就是在Silverlight中调用wcf服务的时候,为什么Completed会被执行多次?下面我把代码帖上来。

    这是wcf提供的服务,很简单就是验证一下客户端传来的值

    Code Snippet

    [OperationContract]
            public string DoWork(string strNum)
            {
                // Add your operation implementation here
                DataClassesTestDataContext db = new DataClassesTestDataContext();
                var vt = from t in db.temp
                         where t.num == strNum
                         select t;
                if (vt.Count() > 0)
                {
                    var v = vt.First();
                    return v.contents;
                }
                else
                {
                    return "-1";
                }
            }

     

     

    下面是page.xaml内容

    Code Snippet
     <Grid x:Name="LayoutRoot" Background="White">
            <Grid.RowDefinitions >
                <RowDefinition Height="100"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <TextBox x:Name="TextBox1" Grid.Row="1" Width="200" Height="30" FontSize="18"></TextBox>
            <Button x:Name="ButtonSubmit" Grid.Row="2" Content="提 交" Width="80" Height="30" Click="ButtonSubmit_Click"></Button>
        </Grid>
    </UserControl>

     


    下面是page.xaml.cs的内容

    Code Snippet

    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 SilverlightApplication2
    {
        public partial class Page : UserControl
        {
            Service.ServiceTestClient ServiceClient;
            public Page()
            {
                InitializeComponent();
                ServiceClient = new SilverlightApplication2.Service.ServiceTestClient();
            }

            private void ButtonSubmit_Click(object sender, RoutedEventArgs e)
            {
                ServiceClient.DoWorkCompleted += new EventHandler 

                                                  (ServiceClient_DoWorkCompleted);
                ServiceClient.DoWorkAsync(TextBox1.Text.ToString());
            }

            void ServiceClient_DoWorkCompleted(object sender, SilverlightApplication2.Service.DoWorkCompletedEventArgs e)
            {
                if (e.Error == null)
                {
                    if (e.Result.Equals("-1"))
                    {
                        HtmlPage.Window.Alert("数据库中无相关信息!");
                    }
                    else
                    {
                        HtmlPage.Window.Alert(e.Result);
                    }
                }
            }
        }
    }

     

     

    就是void ServiceClient_DoWorkCompleted(object sender, SilverlightApplication2.Service.DoWorkCompletedEventArgs e)
    这个函数,在页面的一次执行过程中,如果第一次点击提交,这个函数执行一次,第二次再点击提交按钮的时候,这个函数自动执行2次,依次类推,如果第n次点击就要被执行n次,不知道为什么,怎么解决这个问题呢?
    2008年9月1日 13:53

答案