In WPF applications, XAML code is often loaded and processed during runtime. For example, DynamicResource. However, during compilation or running, the compiled XAML code may be incorrect. In this case, The XAML code parser usually throws an exception called XamlParseException. However, the information provided by the XamlParseException thrown is very simple or inaccurate. In this case, we can obtain more exception information through the work und:
As we know, The XAML code in the WPF application is parsed in the InitializeComponent method. This method is usually located in the constructor of the window object. Therefore, we use the. NET exception mechanism to call the InitializeComponent method:
1 public partial class Window1: System. Windows. Window
2 {
3 public Window1 ()
4 {
5 try
6 {
7 InitializeComponent ();
8}
9 catch (Exception ex)
10 {
11 // InnerExceptions)
12}
13}
14}
Calling InitializeComponent can help us obtain more accurate exception information during debugging.
In addition to the exception capture mechanism above, another solution is to use the exception dialog box of Visual Studio. In the Exception (Exception...) dialog box under menu debugging (Debug), add System. Markup. XamlParseException to the Exception capture list through the Add button in the lower right. Select the Thrown check box. In this way, you can obtain more information about XamlParseException.