uwp
UWP (StaticResource / ThemeResource) और रिसोर्सबर्ड में संसाधन
खोज…
परिचय
नए विंडोज 10 एप्लिकेशन में एक्सएएमएल कोड के अंदर या पीछे कोड में एक संसाधन को संदर्भित करने के कई तरीके हैं। सबसे पहले आपको संसाधनों को किसी सुलभ जगह पर घोषित करना होगा। आसान तरीका यह है कि संदर्भ में एक ResourceDictionary को घोषित करें, चलिए वर्तमान पृष्ठ में बताते हैं।
1. संसाधन शब्दकोश
मेनपेज से स्निपेट। xaml
<Page
x:Class="MyNewApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyNewApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<!-- Creates a resource dictionary in the page context -->
<ResourceDictionary>
<!-- This is a solid color brush resource
NOTE: Each resource inside a resource dictionary must have a key -->
<SolidColorBrush x:Key="ColorRed">Red</SolidColorBrush>
</ResourceDictionary>
</Page.Resources>
<!-- Using ThemeResource in here to access a resource already defined -->
<Grid Background="{ThemeResource ColorRed}">
</Grid>
</Page>
2. वैश्विक संसाधन
संसाधन शब्दकोश केवल उस संदर्भ के अंदर पहुंच योग्य हैं जो वे घोषित किए गए थे, इसलिए यदि हम उन संसाधनों का संदर्भ देना चाहते हैं जो एक पृष्ठ के संदर्भ में दूसरे पृष्ठ से घोषित किए जाते हैं तो वे नहीं मिलेंगे। इसलिए यदि हमें वैश्विक संसाधनों की आवश्यकता है, तो इसे उसी तरह परिभाषित किया जाना चाहिए जैसा कि हम उस ढांचे के साथ करते हैं, जो हम App.xaml में करते हैं
App.xaml से स्निपेट
<Application
x:Class="MyNewApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
RequestedTheme="Dark">
<Application.Resources>
<ResourceDictionary>
<SolidColorBrush x:Key="ColorRed">Red</SolidColorBrush>
</ResourceDictionary>
</Application.Resources>
</Application>
इस तरह हम अपने ऐप में ColorRed रंग से ColorRed कलर रिसोर्स को एक्सेस कर सकते हैं। लेकिन रुकिए, हम अपने ऐप के सभी संसाधनों के साथ उस छोटी फ़ाइल को संक्रमित नहीं करना चाहते हैं! तो हम MergedDictionaries करते हैं
3. विलय किए गए शब्दकोश
लगभग आमतौर पर चीजें थोड़ी अधिक जटिल होती हैं और स्केलेबिलिटी का समर्थन करने के लिए हमें चीजों को अलग करना चाहिए। तो हम विभिन्न फ़ाइलों को परिभाषित कर सकते हैं जिसमें विभिन्न संसाधन शब्दकोशों, यूआई नियंत्रणों के विषयों के लिए संसाधन, ग्रंथों के लिए संसाधन और इसी तरह, फिर हम उन सभी को एक साथ App.xaml फ़ाइल में मर्ज करते हैं।
App.xaml से स्निपेट
<Application
x:Class="MyNewApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
RequestedTheme="Dark">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Assets/Themes/GeneralStyles.xaml"/>
<ResourceDictionary Source="/Assets/Themes/TemplatedControls.xaml"/>
<ResourceDictionary Source="/Assets/Strings/Texts.xaml"/>
<ResourceDictionary Source="/Assets/Strings/ErrorTexts.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
आप एसेट फोल्डर पर राइट क्लिक करके एक नई डिक्शनरी फाइल बना सकते हैं [Add -> New Item]
4. संसाधन तक पहुँचना
अब हमें अपने घोषित संसाधनों तक पहुंचने की आवश्यकता है, ताकि XAML कोड से हम {ThemeResource ResourceKey} या {StaticResource ResourceKey}
to be continued later.
