2011-11-01 59 views
1

我有一个具有两个Canvas控件的应用程序。当应用程序启动时,它会从DLL中加载一个UserControl,它基本上是Canvas和一堆XAML代码。无法在两个不同的画布中使用相同的资源

我希望能够在两个Canvases中显示此控件,但是,当我使用ContentPresenter并将它同时绑定到从DLL加载的控件时,它只在一个画布上显示,而在另一个画布上则不显示“T。我想这是事实,我实际上在两个不同的画布中使用相同的资源,但因为我想避免使用太多的内存(从DLL加载的控件非常重),我没有不想创建相同控件的两个副本。

有没有人有这种情况下更好的方法/解决方案?

这是示出了在两个画布中的一个加载的控制(另一画布使用类似的代码)

<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> 

而且从DLL加载由执行XAML:

// 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; 
    } 
    } 

提前致谢!

+0

你能告诉我们一些代码吗?我只是用图像知道这个问题。 – blindmeis

+0

我已编辑帖子并发布了一些代码,谢谢! –

+0

MapControl是静态的吗?什么类正在加载程序集?你确定你已经创建了2个实例吗? –

回答

3

将属性x:Shared="False"设置为您的资源。
这是'true'所以wpf默认创建一个资源(用于优化性能)。当你设置它'false' wpf为每个请求创建新的实例。
There are sample of this

+0

这似乎已经成功了。非常感谢! –

相关问题