2012-06-07 25 views
6

के साथ संसाधन प्राप्त करें मेरे पास एक WPF विंडो है जो XAML फ़ाइलों से दो वेक्टर छवियों को लोड करना है। (अभिव्यक्ति डिज़ाइन में आसान संशोधन के लिए प्रत्येक एक अलग फ़ाइल में है।)एक संसाधन 0 से एक संसाधन

जब मैं MergedDictionary में XAML फ़ाइलों को शामिल करता हूं, तो यह ठीक काम करता है।

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Images/LCCD logo.xaml" /> 
      <ResourceDictionary Source="Images/LCCD bottom image.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 

और

<Image Source="{Binding Source={StaticResource LCCDlogo}}" /> <!-- Simplified --> 
<Image Source="{Binding Source={StaticResource LCCDbar}}" /> <!-- Simplified --> 

हालांकि, मैं अब खिड़की के संसाधनों के लिए और अधिक जोड़ने की जरूरत: यहाँ कोड मैं का उपयोग करें। नया संसाधन इस विंडो से संबंधित है, इसलिए हम इसे एक फ़ाइल में शामिल फ़ाइल के बजाय एक ही फाइल में रखना चाहते हैं।

जब मैं <Window.Resources> और <ResourceDictionary> के बीच निम्न कोड जोड़ने के लिए, मैं निम्नलिखित त्रुटि हो:

कोड

<Style TargetType="{x:Type tab:FabTabItem}"> 
     <Setter Property="Header" Value="{Binding Path=LabelText}"/> 
     <Setter Property="HeaderTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" Margin="0,0,4,0"> 
         <TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis"/> 
        </StackPanel> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

चेतावनी

The designer does not support loading dictionaries that mix 'ResourceDictionary' items without a key and other items in the same collection. Please ensure that the 'Resources' property does not contain 'ResourceDictionary' items without a key, or that the 'ResourceDictionary' item is the only element in the collection.

तो मैं <ResourceDictionary> बदल इसे टैग करें:

<ResourceDictionary x:Key="Images"> 

हालांकि, मुझे नहीं पता कि अब इस शब्दकोश के अंदर संसाधनों का उपयोग कैसे किया जाए। आप नाम ResourceDictionary के अंदर से संसाधन कैसे प्राप्त करते हैं?


संपादित

कोई बात नहीं। यह संकलित करता है लेकिन नहीं चलता है।

त्रुटि है:

''Resources' property has already been set on 'MainWindow'.

मुझे लगता है कि मैं इसे किसी अन्य तरीके से करना होगा।

उत्तर

8

MergedResourceDictionary's पर एमएसडीएन पृष्ठ के अनुसार यह कानूनी है लेकिन MergedDictionary में निर्दिष्ट संसाधन शब्दकोश में संसाधन को परिभाषित करना आम नहीं है। उपरोक्त पृष्ठ से।

It is legal to define resources within a ResourceDictionary that is specified as a merged dictionary, either as an alternative to specifying Source, or in addition to whatever resources are included from the specified source. However, this is not a common scenario; the main scenario for merged dictionaries is to merge resources from external file locations. If you want to specify resources within the markup for a page, you should typically define these in the main ResourceDictionary and not in the merged dictionaries.

तो यह देखने का प्रयास करें कि यह काम करता है या नहीं।

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Images/LCCD logo.xaml" /> 
      <ResourceDictionary Source="Images/LCCD bottom image.xaml" /> 
      <ResourceDictionary> 
       <Style TargetType="{x:Type tab:FabTabItem}"> 
        <Setter Property="Header" Value="{Binding Path=LabelText}"/> 
        <Setter Property="HeaderTemplate"> 
         <Setter.Value> 
          <DataTemplate> 
           <StackPanel Orientation="Horizontal" Margin="0,0,4,0"> 
            <TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis"/> 
           </StackPanel> 
          </DataTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 
+0

ऐसा लगता है कि यह काम करता है, और यह मेरा एक बेहतर जवाब है। –

+0

@MosheKatz मदद करने के लिए खुशी हुई –

1

मैं उस शैली का उपयोग करने वाले तत्वों के मूल तत्व पर <Style> फ़ाइल में आगे बढ़ने लगा।

यह सबसे अच्छा समाधान नहीं है, लेकिन यह काम करता है।

संबंधित मुद्दे