2012-08-14 42 views
0

在我的項目(ABC),我有MainWindow.xaml,我已經定義了一個工具欄,並此工具欄在 項目下的資源/視圖定義/ ApplicationToolbar.xmal如下:如何在WPF中的單獨ResourceDictionary中引用資源?

我在我的MainWindow.xaml中引用它爲xmlns:local="clr-namespace:ABC",一旦我運行它,我得到一個 錯誤,指示View123找不到。

的更多信息:(EDIT)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:ABC"> 

    <StackPanel x:Key="View1"> 
     <Button Margin="3" Content="Test1"></Button> 
     <Button Margin="3" Content="Test2"></Button> 
    </StackPanel> 

</ResourceDictionary> 
在MainWindow.xmal

現在有:

<Window x:Class="ABC.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:ABC" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid Margin="10"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="10"/> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="10"/> 
     </Grid.RowDefinitions> 

     <!-- FOLLOWING LINE CAUSING ERROR--> 
     <ContentControl Name="Toolbar" Content="{StaticResource View1 }"></ContentControl> 

    </Grid> 
</Window> 

我缺少什麼?

感謝, 阿米特

+1

您能告訴我們您實際參考控件的位置嗎?這裏沒有足夠的信息來提出解決方案。 – 2012-08-14 17:13:54

+0

@Jeff謝謝。我更新了我的問題 – 2012-08-14 17:21:34

+0

您需要在您的MainWindow.xaml – 2012-08-14 17:23:28

回答

0

感謝所有。好的,我嘗試了這裏提出的建議,並且我非常感謝邁克,這很接近解決方案。什麼工作對我來說是添加以下標記的App.xaml

<Application.Resources> 
    <ResourceDictionary Source="Resources/views/ApplicationToolbar.xaml"/> 
</Application.Resources> 

感謝。 Amit

4

由於View1在一個單獨的ResourceDictionary你需要把它合併到ResourceDictionary這個Window引用。

嘗試添加此的代碼只是你Window聲明中:

<Window.Resources> 
    <ResourceDictionary.MergedDictionaries> 
    <ResourceDictionary Source="Resource/Views/ApplicationToolbar.xaml"/> 
    </ResourceDictionary.MergedDictionaries> 
</Window.Resources> 

此時你Window應該能夠引用View1

注意:我沒有在IDE中完全測試過,所以可能會出現輕微的語法錯誤或路徑問題。您可能必須將字典網址的格式設置爲Pack URI才能正確解析引用。 WPF中的資源路徑往往有點棘手。

相關問題