none
Try/Catch in the xaml?? RRS feed

  • Question

  • My app includes a binding to an XML file.   The firs time the app is started, this file may not exist.

    In my codebehind I'm testing for the existence of this file with appropriate try/catch blocks but the app generates an exception:

    System.Windows.Markup.XamlParseException was unhandled
      Message="Cannot create instance of 'Window1' defined in assembly 'MyApp, Version=1.0.0.7, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation.  Error in markup file 'Main.xaml' Line 1 Position 9."
      Source="PresentationFramework"
      LineNumber=1
      LinePosition=9

    before it even begins executing the c# code.

    So how can I handle this situation gracefully? 
    Tuesday, August 5, 2008 10:56 PM

All replies

  • -> So how can I handle this situation gracefully?

    When you encounter this type of exception, you'd better find out the cause of this exception, and fix it before moving on, could you please post your XAML file here to help us understand what type of XAML parsing issue you have here?

    Thanks
    Friday, August 8, 2008 7:04 AM
  • Well, here it is (ther relevant portion anyway):

            <Grid>
                <Grid.Resources>
                    <!-- Data for the grid -->
                    <xcdg:DataGridCollectionViewSource
                    x:Key="cvs_data"
                    Source="{Binding}"/>

                    <!-- Container list for the Insertion Row -->
                    <XmlDataProvider
                        x:Key="container_data"
                        Source="c:\program files\MyApp\users.xml"   <== if this file doesn't exist, I get the exception
                        XPath="/user">
                    </XmlDataProvider>



    Thanks.

    • Edited by purpleflash Friday, August 8, 2008 4:04 PM clarified
    Friday, August 8, 2008 4:04 PM
  • Try to specify a valid XML file for XmlDataProvider, if it doesn't exist, it's the right behaviour for XAML parser to throw an exception.

    Thanks
    • Marked as answer by Marco Zhou Tuesday, August 12, 2008 10:15 AM
    • Unmarked as answer by Marco Zhou Tuesday, August 26, 2008 2:49 AM
    Monday, August 11, 2008 5:54 AM
  • Yeah, but how do I handle this if it's the first time the app is launched and the users.xml file doesn't exist?

    In code I plan to check for this, and if the file doesn't exist I will create it.

    But the xaml blows up before the c# code has a chance to test for it...
    Monday, August 25, 2008 11:26 PM
  • Well, I think that you'd better instantiate the XmlDataProvider at codebehind, and make sure that its Source property points to a valid/existent XML file. You know, it's hard to express the "try catch" semantics in XAML, so you'd better do it at code behind instead.

    Hope this clears things up a little bit.
    • Proposed as answer by Ilya Margolin Sunday, September 7, 2008 1:56 PM
    Tuesday, August 26, 2008 3:03 AM
  • hi purpleflash

    i have the same problem, i'll "try" (hehe) solve it with this example:

    good luck ;)

    Tuesday, October 13, 2009 9:25 AM