为什么用户控件加载事件没有被触发
我有一个用户控件。我有时再次遇到这种情况,但总是可以通过使用“New()
构造函数”来修复它。但我仍然想知道我做错了什么,因为如果加载控件,则必须触发加载事件!
这是一些代码:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:OUTPUT___VideoContent"
Title="OUTPUT - VideoContent" Height="350" Width="525" Icon="/OUTPUT%20-%20VideoContent;component/Images/VideoContent.png">
<Grid x:Name="LayoutRoot">
<Grid x:Name="VideoGrid">
<my:ucVideoPresenter x:Name="VideoPresenter1"/>
<TextBlock x:Name="txtInfo" Visibility="Collapsed" />
</Grid>
</Grid>
</Window>
在用户控件中,在 WPF 或代码行为上声明加载事件,但没有成功!
I have a user control. I had this situations again some times but could always fix it by using the "New()
contructor". But I still wonder what I am doing wrong because the load event has to be fired if control was loaded!
Here is some code:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:OUTPUT___VideoContent"
Title="OUTPUT - VideoContent" Height="350" Width="525" Icon="/OUTPUT%20-%20VideoContent;component/Images/VideoContent.png">
<Grid x:Name="LayoutRoot">
<Grid x:Name="VideoGrid">
<my:ucVideoPresenter x:Name="VideoPresenter1"/>
<TextBlock x:Name="txtInfo" Visibility="Collapsed" />
</Grid>
</Grid>
</Window>
and in the usercontrol, the load event is declared on WPF or codebehing without any success!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为“Loaded”事件处理程序中抛出了异常。该异常可能是由于混合模式程序集或“用户处理”的某些其他异常而发生的,并且 WPF 框架正在捕获它(调试器未知)。这会导致在 Loaded 方法中设置断点时调试器不会中断。
为了确保您可以准确地看到发生了什么错误:
This is because an exception is being thrown in the 'Loaded' eventhandler. The exception may be occurring as a result of a mixed mode assembly or some other exception that is "user handled", and the WPF framework is catching it (unknown to the debugger). This causes the debugger not to break when a breakpoint is set within the Loaded method.
To make sure you can see exactly what error is occurring:
您的 UserControl 构造函数是否仍会调用
InitializeComponent()
,如果没有此调用,它将不会构建其视觉效果,并且Loaded
事件可能不会触发。Does your UserControl constructor still make a call
InitializeComponent()
, without this, it will not build up its visuals and theLoaded
event may not fire.