首页 新闻 会员 周边 捐助

为什么我的异步调用结果会执行多次呢?

0
悬赏园豆:50 [已解决问题] 解决于 2009-11-13 14:27

主页调用ChildWinow新增数据,第一次新增时正常、但第二次新增会现两次“新增成功”对话框,然后会递增出现更多次数

是什么原因造成的呢?下面是调用的代码。

<controls:ChildWindow
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
                      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input" x:Class="SystemSL.View.DeptDetail"
                      Width="240" Height="300"
                      Title="部门信息" mc:Ignorable="d">
    <Grid x:Name="LayoutRoot" Margin="2">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Button x:Name="CancelButton" Content="取消" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1"/>
        <Button x:Name="OKButton" Content="确定" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1"/>
     <TextBlock HorizontalAlignment="Left" Margin="12,15,0,0" VerticalAlignment="Top" FontFamily="/SystemSL;Component/Fonts/Fonts.zip#宋体" FontSize="12" Text="部门编号" TextWrapping="Wrap"/>
     <TextBox x:Name="txtDeptCode" Margin="69,11,17,0" VerticalAlignment="Top" TextWrapping="Wrap"/>
     <TextBlock HorizontalAlignment="Left" Margin="12,60,0,0" VerticalAlignment="Top" FontSize="12" Text="部门编号" TextWrapping="Wrap" FontFamily="/SystemSL;Component/Fonts/Fonts.zip#宋体"/>
     <TextBox x:Name="txtDeptName" Margin="69,55,17,0" VerticalAlignment="Top" TextWrapping="Wrap"/>
     <TextBlock HorizontalAlignment="Left" Margin="12,103,0,106" FontSize="12" Text="上级部门" TextWrapping="Wrap" FontFamily="/SystemSL;Component/Fonts/Fonts.zip#宋体" d:LayoutOverrides="Height"/>
     <input:AutoCompleteBox x:Name="txtParentName" Margin="69,98,17,104" Height="24" IsTextCompletionEnabled="True" ValueMemberPath="DeptName">
            <input:AutoCompleteBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding DeptCode}"/>
                        <TextBlock Text="  "/>
                        <TextBlock Text="{Binding DeptName}"/>
                    </StackPanel>
                </DataTemplate>
            </input:AutoCompleteBox.ItemTemplate>
        </input:AutoCompleteBox>
        <TextBox x:Name="txtHide" Width="0" Height="0"/>
    </Grid>
</controls:ChildWindow>

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.Collections.ObjectModel;

namespace SystemSL.View
{
    public partial class DeptDetail : ChildWindow
    {
        ObservableCollection<ServiceSL.DeptInfo> deptInfo = new ObservableCollection<SystemSL.ServiceSL.DeptInfo>();
        ServiceSL.SlServiceClient proxy = new SystemSL.ServiceSL.SlServiceClient();

        public ServiceSL.DeptInfo CallDept
        {
            get;
            set;
        }

        public DeptDetail()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(DeptDetail_Loaded);
        }

        void DeptDetail_Loaded(object sender, RoutedEventArgs e)
        {
            proxy.getDeptInfoCompleted += new EventHandler<SystemSL.ServiceSL.getDeptInfoCompletedEventArgs>(proxy_getDeptInfoCompleted);
            proxy.getDeptInfoAsync();

               }

        void proxy_getDeptInfoCompleted(object sender, SystemSL.ServiceSL.getDeptInfoCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                deptInfo = e.Result;
                this.txtParentName.ItemsSource = deptInfo;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            ServiceSL.DeptInfo infoDept = new SystemSL.ServiceSL.DeptInfo();
            infoDept.DeptCode = this.txtDeptCode.Text;
            infoDept.DeptName = this.txtDeptName.Text;
            ServiceSL.DeptInfo dept = new SystemSL.ServiceSL.DeptInfo();
            if (this.txtParentName.SelectedItem != null)
            {
                dept = (ServiceSL.DeptInfo)this.txtParentName.SelectedItem;
              
            }

                infoDept.ParentCode = dept.DeptCode;
                proxy.InsertDeptInfoCompleted += new EventHandler<SystemSL.ServiceSL.InsertDeptInfoCompletedEventArgs>(proxy_InsertDeptInfoCompleted);
                proxy.InsertDeptInfoAsync(infoDept);

                     this.txtDeptCode.Text = "";
                this.txtDeptName.Text = "";
                this.txtParentName.Text = "";
                this.txtHide.Text = "";
            this.DialogResult = true;
        }

        void proxy_InsertDeptInfoCompleted(object sender, SystemSL.ServiceSL.InsertDeptInfoCompletedEventArgs e)
        {
            if (e.Result > 0)
            {
                MessageBox.Show("新增成功");
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

 

励箭的主页 励箭 | 初学一级 | 园豆:47
提问于:2009-11-06 17:02
< >
分享
最佳答案
0

下班没时间看了,初步估计问题在那几个“+=”上,“+=”前清空一下,不然有累加效果!

收获园豆:50
dege301 | 老鸟四级 |园豆:2825 | 2009-11-06 17:21
dege301 请问怎么清空呢?
励箭 | 园豆:47 (初学一级) | 2009-11-06 17:42
事件在初始化前注册一次就可以了,想this.Loaded += new RoutedEventHandler(DeptDetail_Loaded);这样. 如果用清空的模式,就是在回调函数结束时,用 -= 注销事件
Launcher | 园豆:45050 (高人七级) | 2009-11-06 18:07
dege301 初始化前注册一次,如何调用呢?
励箭 | 园豆:47 (初学一级) | 2009-11-07 12:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册