Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net WPF UserControl未触发的初始化事件_.net_Wpf_.net 3.5 - Fatal编程技术网

.net WPF UserControl未触发的初始化事件

.net WPF UserControl未触发的初始化事件,.net,wpf,.net-3.5,.net,Wpf,.net 3.5,我有一个非常简单的WPF用户控件,如下所示: namespace MyUserControl { /// <summary> /// Interaction logic for UserControl1.xaml /// </summary> public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponen

我有一个非常简单的WPF用户控件,如下所示:

namespace MyUserControl
{
  /// <summary>
  /// Interaction logic for UserControl1.xaml
  /// </summary>
  public partial class UserControl1 : UserControl
  {
    public UserControl1()
    {
      InitializeComponent();
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
      Rect rect = new Rect(RenderSize);
      drawingContext.DrawRectangle(Brushes.AliceBlue, new Pen(Brushes.Red, 1), rect);
      base.OnRender(drawingContext);
    }
  }
}
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="clr-namespace:MyUserControl;assembly=MyUserControl"
    Title="Window1" Height="351" Width="496">
    <Grid>
    <mc:UserControl1 Margin="0,0,0,0" Name="uControl1" Initialized="uControl1_Initialized"/>
  </Grid>
</Window>
public UserControl1()
{
    this.Initialized += delegate
    {
        MessageBox.Show("Hello!");
    };
    InitializeComponent();
}
不幸的是,初始化的事件从未被触发。谁能告诉我为什么

非常感谢

每当 EndInit或OnVisualParentChanged 方法被调用。打电话给 方法可能来自 应用程序代码,或通过 可扩展应用程序标记语言 (XAML)当XAML 页面已被处理

我可以在这里重现你的问题。我建议改为使用
Loaded
事件,该事件在控件布局和呈现后激发。如果确实需要初始化
事件,请遵循此建议,在调用
InitializeComponent()
之前,在
用户控件的构造函数中附加一个事件处理程序,如下所示:

namespace MyUserControl
{
  /// <summary>
  /// Interaction logic for UserControl1.xaml
  /// </summary>
  public partial class UserControl1 : UserControl
  {
    public UserControl1()
    {
      InitializeComponent();
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
      Rect rect = new Rect(RenderSize);
      drawingContext.DrawRectangle(Brushes.AliceBlue, new Pen(Brushes.Red, 1), rect);
      base.OnRender(drawingContext);
    }
  }
}
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="clr-namespace:MyUserControl;assembly=MyUserControl"
    Title="Window1" Height="351" Width="496">
    <Grid>
    <mc:UserControl1 Margin="0,0,0,0" Name="uControl1" Initialized="uControl1_Initialized"/>
  </Grid>
</Window>
public UserControl1()
{
    this.Initialized += delegate
    {
        MessageBox.Show("Hello!");
    };
    InitializeComponent();
}

有关加载的
与初始化的
的详细说明,请参阅。

谢谢David。你会认为这是WPF缺陷吗?不,阅读文档更彻底,我可以看到这是广告。