2009-08-26 5 views
73

मैं कई WPF उपयोगकर्ता नियंत्रण लिख रहा हूं जिनके लिए साझा और व्यक्तिगत संसाधन दोनों की आवश्यकता है।WPF उपयोगकर्ता नियंत्रण में आयातित और स्थानीय संसाधनों को कैसे गठबंधन करें

मैं एक अलग संसाधन फ़ाइल से लोड करने वाले संसाधनों का वाक्य विन्यास पता लगा है:

<UserControl.Resources> 
    <ResourceDictionary Source="ViewResources.xaml" /> 
</UserControl.Resources> 

हालांकि, जब मैं यह कर, मैं भी स्थानीय स्तर पर संसाधन नहीं जोड़ सकते हैं, जैसे:

<UserControl.Resources> 
    <ResourceDictionary Source="ViewResources.xaml" /> 
    <!-- Doesn't work: --> 
    <ControlTemplate x:Key="validationTemplate"> 
     ... 
    </ControlTemplate> 
    <style x:key="textBoxWithError" TargetType="{x:Type TextBox}"> 
     ... 
    </style> 
    ... 
</UserControl.Resources> 

मैं मैंने रिसोर्स डिक्शनरी पर एक नज़र डाली है। मर्ज किए गए शब्दकोश, लेकिन यह केवल मुझे एक से अधिक बाहरी शब्दकोशों को मर्ज करने देता है, स्थानीय संसाधनों को स्थानीय रूप से परिभाषित नहीं करता है।

मुझे कुछ तुच्छ याद आना चाहिए?

यह उल्लेख किया जाना चाहिए: मैं WinForms प्रोजेक्ट में अपने उपयोगकर्ता नियंत्रण होस्ट कर रहा हूं, इसलिए App.xaml में साझा संसाधनों को वास्तव में एक विकल्प नहीं है।

उत्तर

144

मैं यह पता लगा। समाधान MergedDictionaries शामिल है, लेकिन बारीकियों सिर्फ सही, इस तरह होना चाहिए:

<UserControl.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="ViewResources.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
     <!-- This works: --> 
     <ControlTemplate x:Key="validationTemplate"> 
      ... 
     </ControlTemplate> 
     <style x:key="textBoxWithError" TargetType="{x:Type TextBox}"> 
      ... 
     </style> 
     ... 
    </ResourceDictionary> 
</UserControl.Resources> 

है, स्थानीय संसाधनों ResourceDictionary टैग के भीतर नेस्ट किया जाना चाहिए। तो उदाहरण here गलत है।

4

MergedDictionaries का उपयोग करें।

मैं here.

से निम्न उदाहरण मिल गया File1

<ResourceDictionary 
    xmlns=" http://schemas.microsoft.com/winfx/2006/xaml/presentation " 
    xmlns:x=" http://schemas.microsoft.com/winfx/2006/xaml " > 
    <Style TargetType="{x:Type TextBlock}" x:Key="TextStyle"> 
    <Setter Property="FontFamily" Value="Lucida Sans" /> 
    <Setter Property="FontSize" Value="22" /> 
    <Setter Property="Foreground" Value="#58290A" /> 
    </Style> 
</ResourceDictionary> 

फ़ाइल 2

<ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="TextStyle.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
+0

धन्यवाद, लेकिन कोई किस्मत। उनका उदाहरण सही लगता है, लेकिन वास्तव में काम नहीं करता है। मुझे संदेश मिलता है "संपत्ति 'संसाधन' एक से अधिक बार सेट है"। –

+0

मैं मर्ज किए गए शब्दकोश के बारे में जानता हूं। लेकिन वे मुझे स्थानीय रूप से परिभाषित संसाधनों के साथ बाहरी शब्दकोश संदर्भ को गठबंधन करने की अनुमति नहीं देते हैं। जैसा कि पहले से कहा गया है, आपके द्वारा संदर्भित पृष्ठ पर एक उदाहरण है, लेकिन यह काम नहीं करता है। –

+0

आह क्षमा करें। महसूस नहीं किया। –

4

आप MergedDictionaries खंड के अंदर स्थानीय संसाधनों को परिभाषित कर सकते हैं:

<UserControl.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <!-- import resources from external files --> 
      <ResourceDictionary Source="ViewResources.xaml" /> 

      <ResourceDictionary> 
       <!-- put local resources here --> 
       <Style x:key="textBoxWithError" TargetType="{x:Type TextBox}"> 
        ... 
       </Style> 
       ... 
      </ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</UserControl.Resources> 
संबंधित मुद्दे