为什么用户控件加载的事件不会被触发 [英] Why usercontrols loaded event is not fired

查看:131
本文介绍了为什么用户控件加载的事件不会被触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控件。我再次遇到这种情况,但总是可以通过使用 New()解决方案来解决这个问题。但是我仍然想知道我做错了什么,因为加载事件必须在加载控件时触发



这是一些代码: / p>

 < 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 - VideoContentHeight =350Width =525Icon =/ OUTPUT%20-%20VideoContent; component / Images / VideoContent.png>
< Grid x:Name =LayoutRoot>
< Grid x:Name =VideoGrid>
< my:ucVideoPresenter x:Name =VideoPresenter1/>
< TextBlock x:Name =txtInfoVisibility =Collapsed/>
< / Grid>
< / Grid>
< / Window>

在用户控件中,加载事件在WPF或代码上声明无任何成功!



解决方案

这是因为在已加载事件处理程序中抛出异常。由于混合模式汇编或用户处理的某些其他异常,WPF框架捕获它(调试器未知)可能会发生异常。这将导致在Loaded方法中设置断点时调试器不会中断。



确保您可以准确地看到发生了什么错误:


  1. 在VS2010中,去Debug |例外情况

  2. 勾选可能适用于您的例外的抛出异常单选框。

  3. 重新运行应用程序,VS2010应该打破在事件处理程序中抛出的异常。

  4. 根据现在已知的异常进行调试。


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!

解决方案

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:

  1. In VS2010 go to Debug | Exceptions.
  2. Tick "Thrown" exception radio boxes for the exceptions that may be applicable in your case.
  3. Re-run the app and VS2010 should break on the exception that is being thrown in the event handler.
  4. Debug according to a now known exception.

这篇关于为什么用户控件加载的事件不会被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆