棒棒牛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 880|回复: 6

[问题] 用户控件被加载两次 [复制链接]

V1

xiaoya

Rank: 1

游币
97
金币
67
经验
19
积分
24
帖子
8
发表于 2010-8-27 11:38:14 |显示全部楼层
本帖最后由 xiaochunmi 于 2010-8-27 11:39 编辑

我在窗体里放一个Tabcontrol,在TabControl中又放了一个用户控件,可为什么用户控件会被加载两次呢(UserControl的Loaded事件执行两次)?怎么解决呢?


谁知道是为什么啊?

V1

xiaoya

Rank: 1

游币
97
金币
67
经验
19
积分
24
帖子
8
发表于 2010-8-27 17:36:08 |显示全部楼层
有没有人回复啊?    怎么可以不执行呢

版主

WPF_1.5_ray

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

游币
259
金币
543
经验
1690
积分
2175
帖子
428

2011 年度优秀版主 2010 年度优秀版主

发表于 2010-8-28 13:05:33 |显示全部楼层
放个Demo来看看
What is real?

V1

xiaoya

Rank: 1

游币
97
金币
67
经验
19
积分
24
帖子
8
发表于 2010-8-30 11:52:41 |显示全部楼层
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;

  14. namespace WpfApplication2
  15. {
  16.     /// <summary>
  17.     /// MainWindow.xaml 的交互逻辑
  18.     /// </summary>
  19.     public partial class MainWindow : Window
  20.     {
  21.         delegate void WillHide();

  22.         private WillHide willHide;


  23.         public MainWindow()
  24.         {
  25.             InitializeComponent();
  26.         }

  27.         private void Window_Loaded(object sender, RoutedEventArgs e)
  28.         {
  29.             MessageBox.Show("Main");
  30.         }

  31.         private void button1_Click(object sender, RoutedEventArgs e)
  32.         {
  33.             TabItem ti = new TabItem();
  34.             ti.Header = "aaaa";

  35.             UserControl5 u5 = new UserControl5();
  36.             ti.Content = u5;

  37.             tabControl1.Items.Add(ti);
  38.             tabControl1.SelectedItem = ti;
  39.         }
  40.     }
  41. }
复制代码
这个是主窗体的cs文件
  1. <Window x:Class="WpfApplication2.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="MainWindow" Height="418" Width="639" xmlns:my="clr-namespace:WpfApplication2" Loaded="Window_Loaded" ShowInTaskbar="False">
  5.     <Grid>
  6.         <TabControl Height="287" HorizontalAlignment="Left" Margin="22,41,0,0" Name="tabControl1" VerticalAlignment="Top" Width="485">
  7.             <TabItem Header="第一" Name="tabItem1" DataContext="{Binding}">
  8.                 <Grid></Grid>
  9.             </TabItem>
  10.         </TabControl>
  11.         <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="22,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
  12.     </Grid>
  13. </Window>
复制代码
这个是主窗体的xaml文件

V1

xiaoya

Rank: 1

游币
97
金币
67
经验
19
积分
24
帖子
8
发表于 2010-8-30 11:54:18 |显示全部楼层
这个是UserControl的cs文件
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;

  14. namespace WpfApplication2
  15. {
  16.     /// <summary>
  17.     /// UserControl5.xaml 的交互逻辑
  18.     /// </summary>
  19.     public partial class UserControl5 : UserControl
  20.     {
  21.         public UserControl5()
  22.         {
  23.             InitializeComponent();
  24.         }

  25.         private void UserControl_Loaded(object sender, RoutedEventArgs e)
  26.         {
  27.             MessageBox.Show("U5");
  28.         }
  29.     }
  30. }
复制代码
这个是xaml文件
  1. <UserControl x:Class="WpfApplication2.UserControl5"
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6.              mc:Ignorable="d"
  7.              d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded">
  8.     <Grid>
  9.         <Button Content="I am u5" Height="37" HorizontalAlignment="Left" Margin="24,85,0,0" Name="button1" VerticalAlignment="Top" Width="115" />
  10.     </Grid>
  11. </UserControl>
复制代码

V1

xiaoya

Rank: 1

游币
97
金币
67
经验
19
积分
24
帖子
8
发表于 2010-8-30 11:55:47 |显示全部楼层
UserControl的Loaded事件就会执行两次,而且好像如果写MessageBox.Show()
的时候执行顺序好像有点问题

版主

WPF_1.5_ray

Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7Rank: 7

游币
259
金币
543
经验
1690
积分
2175
帖子
428

2011 年度优秀版主 2010 年度优秀版主

发表于 2010-8-31 11:31:04 |显示全部楼层
第一次发生的原因是:
tabControl1.Items.Add(ti);
第二次发生的原因是:
tabControl1.SelectedItem = ti;
这两次Loaded事件的触发是异步的,看callstack就可以看到(有个类似于FirePendingLoadedEventsCallback之类的,要在Debug中关闭Just My Code)
而代码中写在一起,所以调试的话还看不出来,你写成这种的:
            var control = new MyControl();
            var flag = true;
            control.Loaded += (s, args) =>
            {
                if (flag)
                {
                    flag = false;
                    //tc_Container.SelectedItem = tabItem;
                }
            };
            tabItem.Content = control;
            tc_Container.Items.Add(tabItem);
把//tc_Container.SelectedItem = tabItem;注释去掉和加上分别调试看看那就知道了。

至于MessageBox,会起一个nested的消息循环,可能会影响一些东西。
What is real?
您需要登录后才可以回帖 登录 | 注册

bbniu.com (湘ICP备06008909号)  

GMT+8, 2012-2-6 10:10

Copyright © 2009-2011 bbniu.com. All Rights Reserved.

回顶部