Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, my name is Alex.
I'm currently learning after a book about how to convert xaml code into objects during runtime.

I have the following code:

C#
try
           {
               ctrl = XamlReader.Load(xaml) as UserControl;
           }
           catch (Exception exc)
           {
               OnXamlResult(new XamlCruncherEventArgs(exc.Message)); return;
           }



The code is not mine, I took it from the book.

The problem is that try-catch does not work properly, or at least as I know till now.
During debugging the program stops when it reaches the line :
ctrl = XamlReader.Load(xaml) as UserControl;

without catching the exception.

What am I doing wrong or how can I solve this problem?

Thanks in advance!


Sorry for missing some things.
xaml is a string. It is taken form a textbox and if the xaml is correctly typed by the user the program should convert the xaml code into object otherwise it should display the corresponding error.

About how the program reacts, i can say that debug stops and it is not behaving like going into a infinite cycle.
Posted
Updated 19-Aug-11 2:31am
v2

0) We don't know what the xaml variable represents. Show us how it was initialized.

1) If your xaml (XmlReader) parameter isn't null, you won't get an exception.
 
Share this answer
 
Comments
Anil Honey 206 19-Aug-11 8:24am    
Its Correct.
What do you mean with 'program stops'? Does it really stop?
If the program just hangs then it has nothing to do with the try/catch block: probably the Load operation causes it (without throwing).
 
Share this answer
 
With Out Any Error Try Catch Block Will Not Catch the Expection.In your Code there is No Error.
 
Share this answer
 
I will suggest that you set a break point to see if
ctrl = XamlReader.Load(xaml) as UserControl;
really perform its function. With this I think you can see if there is any exception to catch and if the try-catch block is/is not performing its function.
 
Share this answer
 
v2
You have to check the parameter, have nothing else to do with the code, and as the documentation reads the 'xaml' parameter should be a stream containing the actual xaml.

http://msdn.microsoft.com/en-us/library/ms590398.aspx

for your code to work save the xaml string in a file, then read the contents or the text to a stream then pass it on to Load(), may be you should check with a different xaml file for testing!

Regards
Vallarasu S.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900