I have an application with two Canvas controls. When the application starts, it loads from a DLL a UserControl which is basically Canvas with a bunch of XAML code.
I want to be able to display this control in the two Canvases, however, when I use the ContentPresenter and bind it in both Canvases to the control loaded from the DLL it shows only on one canvas and on the other it doesn’t. I guess it’s something that has to do with the fact that I’m actually using the same resource in two different Canvases, but since I wanted to avoid using too much memory (the control loaded from the DLL is pretty heavy) I didn’t want to create two copies of the same control.
Does anyone have a better approach / solution for this situation?
This is the XAML for showing the loaded control in one of the two canvases (the other canvas uses similar code)
<Canvas Width="{Binding MapModel.MapControl.Bounds.Width}" Height="{Binding MapModel.MapControl.Bounds.Height}">
<Canvas.Background>
<VisualBrush>
<VisualBrush.Visual>
<ContentPresenter Content="{Binding MapModel.MapControl}" />
</VisualBrush.Visual>
</VisualBrush>
</Canvas.Background>
</Canvas>
And loading from the DLL is performed by:
// Load the map library assembly (Using reflection)
Assembly asm = Assembly.LoadFile(m_fileName);
Type[] tlist = asm.GetTypes();
// Find the class that represents the airport XAML drawing in the assembly, if it finds the airport class then
// set the value to be an instance of that class.
foreach (Type t in tlist)
{
if (t.Name == "Map")
{
MapControl = Activator.CreateInstance(t) as UserControl;
break;
}
}
Thanks in advance!
Set attribute
x:Shared="False"to your resource.It is
'true'so wpf creates one resource (for optimize performance) by default. When you set it'false'wpf creates new instance per request.There are sample of this