2011-11-01 17 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

這似乎已經成功了。非常感謝! –

相關問題