views:

203

answers:

2

I've got an application that reads in XAML files dynamically like this:

StreamReader sr = new StreamReader(pathAndFileName);
this.Content = XamlReader.Load(sr.BaseStream);

In one of those XAML files that gets loaded in (they all have had their code behind removed), this works:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DynamicXaml123">
    <StackPanel Margin="10" HorizontalAlignment="Left">
        <TextBox Height="23" Width="100" Text="{Binding FirstName}" />
        <TextBox Height="23" Width="100" Text="{Binding LastName}" />
        <TextBox Height="23" Width="100" Text="{Binding Age}" />
        <local:FieldEmailView></local:FieldEmailView>
    </StackPanel>
</UserControl>

But this give the error "The tag 'FieldEmailView' does not exist in XML namespace 'clr-namespace:DynamicXaml123;assembly=DynamicXaml123'".

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DynamicXaml123;assembly=DynamicXaml123">
    <StackPanel Margin="10" HorizontalAlignment="Left">
        <TextBox Height="23" Width="100" Text="{Binding FirstName}" />
        <TextBox Height="23" Width="100" Text="{Binding LastName}" />
        <TextBox Height="23" Width="100" Text="{Binding Age}" />
        <local:FieldEmailView></local:FieldEmailView>
    </StackPanel>
</UserControl>

If I leave out the assembly reference then it gets the error

Message=""XmlNamespace", "Assembly" oder "ClrNamespace"

when reading in the XAML.

Why can't I include the Assembly reference here, what do I have to change/check to get this to work?

A: 

When you load this in, you need to be able to resolve the assembly references. In other words, this assembly must be available for the application to reference against - one way to do this would be to deploy DynamicXaml123 to the GAC.

Pete OHanlon
Is there any other way to do it? I have another similar application which I know is not deployed to the GAC which works with this assembly reference. How could that be, is there another way?
Edward Tanguay
I don't think you need to put the assembly in the GAC... it will probably work if you just load the assembly into the AppDomain
Thomas Levesque
@Thomas - That's true, I deliberately used the phrasing "one way" to try to avoid giving the impression that it was the only way.@Edward - if your model is already loaded into the app domain, you don't need to pick it up from the GAC. Just make sure that you load the referenced assembly before you load the XAML.
Pete OHanlon
A: 

I'd suggest getting out process monitor and seeing where your app is looking for dynamicxaml123. Check the fusion log as well. I'd guess the behavior in the XamlSerializer is different if you include the namespace than when you don't, and that change is affecting where the runtime is poking around for your assembly.

Note, there may be some lag between setting up fuslogvw and when it actually starts to log.

Will
Also, what you believe is happening may not actually be what's happening... best to double check yourself and not assume anything.
Will