tags:

views:

141

answers:

1

I've got a WPF application that consists of Pages in a NavigationWindow. Within App.Xaml I create all my pages so that I can reference them when using navigation commands. It goes a little something like this:

<Application ...>
 <Application.Resources>
  <FirstPage x:Key="first"/>
  <SecondPage x:Key="second"/>
 </Application.Resources>
</Application>

The "circular" reference (can't figure out what else to call this) happens when FirstPage references "second" or vise versa. Let's just say FirstPage references "second" in this example.

<FirstPage ...>
  ...
  <Button
    CommandParameter="{StaticResource second}"
  </Button>
</FirstPage>

(Before you start answering, I'll get to the order in which FirstPage and SecondPage are added to the application resources in a moment!)

This situation results in some strange, useless errors. First, in App.xaml, I get the error

Could not create an instance of type FirstPage

So I roll over to FirstPage and try to load it in the designer. In the xaml, the line where I reference "second" is highlighted and this extremely helpful error message is displayed:

The application XAML failed to load. Fix errors in the application XAML before trying to load other XAML files

Which came first, the chicken or the egg? App.xaml can't load because it can't create FirstPage, and I can't fix FirstPage because its actually app.xaml that's the issue.

Things I have tried to fix this:
1) I tried changing the order of items added to Application.Resources. I figured that if FirstPage needs to get SecondPage, then SecondPage must be added and available before FirstPage appears. This doesn't work.
2) I tried using a DynamicResource instead of a StaticResource. No help.
3) I tried adding the Page instances in code. This works, but is not satisfying.

Note, the application compiles and runs fine; the issue is why this breaks at design time.


What can I do to fix this? I need to access my pages all over the place to use as navigation parameters, which is why I put them in the application's resources. Is there an alternate way to do this? I only want a single instance of each page in my application if I can help it.

+1  A: 

You could use the resource name as the parameter rather than the resource itself. Then your command could do the lookup of the actual resource for you.

HTH, Kent

Kent Boogaart
I'm doing something similar right now. I'm adding the single instance in code rather than xaml and it works as expected now. I'm still interested in what is going on, however.
Will