बाइंडिंग के साथ एक ApplicationBar स्थानीयकरण

जब मैंने MVVM के आधार पर एप्लिकेशन विकसित करना सीखा, तो मुझे वास्तव में यह पसंद नहीं आया, जो कई लेखों में दिखाता है कि ViewModel में ApplicationBar कैसे बनाया जाता है। वहां, बटन और मेनू आइटम स्थानीयकृत लाइनों से भरे हुए हैं। मैंने लंबे समय तक समाधान खोजा, और यह पाया गया।
चेतावनी! लेख केवल आवेदन पैनल को स्थानीय बनाने की समस्या के समाधान का वर्णन करता है और तात्पर्य है कि पाठक XAML, MVVM, डेटा बाइंडिंग और अनुप्रयोग स्थानीयकरण की मूल बातें से परिचित हैं।

एप्लिकेशन बार को स्थानीय करने के लिए लाइब्रेरी से मिलें: http://appbarutils.codeplex.com/
इसे काम में आजमाने के लिए, एक एप्लिकेशन पेज बनाएं और उसमें सामान्य एप्लिकेशन पैनल जोड़ें, बटन के साथ और, यदि आवश्यक हो, मेनू आइटम। उदाहरण के लिए, यह:
<shell:ApplicationBar> <shell:ApplicationBar.MenuItems> <shell:ApplicationBarMenuItem IsEnabled="True" Text="lookscreen"/> </shell:ApplicationBar.MenuItems> <shell:ApplicationBarIconButton IconUri="/Assets/AppBar/save.png" IsEnabled="True" Text="save"/> </shell:ApplicationBar> 

कृपया ध्यान दें: पाठ गुण रिक्त स्थान के बिना होना चाहिए। मैंने सिरिलिक वर्णमाला की उपस्थिति में प्रदर्शन की जांच नहीं की।
AppBarUtils पैकेज को इंस्टाल-पैकेज AppBarUtils कमांड का उपयोग करके या पैकेज मैनेजर के माध्यम से जोड़ें। इस पैकेज के असेंबली के लिंक को पेज लेआउट में जोड़ें:
 xmlns:AppBarUtils="clr-namespace:AppBarUtils;assembly=AppBarUtils" 

और हमें अभी भी व्यवहार निर्धारित करने के लिए एक विधानसभा की आवश्यकता है:
 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 

इन तैयारियों के बाद, हम उस पृष्ठ के लिए व्यवहार सेट करते हैं जिस पर एप्लिकेशन पैनल स्थित है। मुझे यह इस तरह मिला:
 <i:Interaction.Behaviors> <AppBarUtils:AppBarItemCommand Id="save" Type="Button" Command="{Binding SaveCommand}" Text="{Binding LocalizedResources.SaveButton, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/> <AppBarUtils:AppBarItemCommand Id="lookscreen" Type="MenuItem" Command="{Binding LookScreenCommand}" Text="{Binding LocalizedResources.ScreenItem, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/> </i:Interaction.Behaviors> 

व्यवहार उस तत्व से जुड़ा होता है जिसे यह दिया जाता है, आईडी द्वारा मेनू आइटम या बटन के पाठ गुण के बराबर मूल्य के साथ। इसके अलावा, मेनू आइटम का वह प्रकार जिसके लिए व्यवहार सेट किया गया है: बटन के लिए - बटन, मेनू आइटम के लिए - MenuItem। शेष गुणों का मूल्य, मुझे लगता है, आवश्यक नहीं है।
सभी जोड़तोड़ के बाद, मुझे एक असली एप्लिकेशन में एक पेज मिला, जो विंडोज़ फोन स्टोर में प्रकाशित हुआ है:
XAML
 <phone:PhoneApplicationPage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:Command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8" xmlns:AppBarUtils="clr-namespace:AppBarUtils;assembly=AppBarUtils" x:Class="CatDay.MainPage" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations ="PortraitOrLandscape" shell:SystemTray.IsVisible="True" DataContext="{Binding Main, Mode=OneWay, Source={StaticResource Locator}}"> <shell:SystemTray.ProgressIndicator> <shell:ProgressIndicator IsIndeterminate="true" IsVisible="{Binding ProgressBarValue}" Text="{Binding ProgressBarText}" /> </shell:SystemTray.ProgressIndicator> <phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar> <shell:ApplicationBar.MenuItems> <shell:ApplicationBarMenuItem IsEnabled="True" Text="donate"/> <shell:ApplicationBarMenuItem IsEnabled="True" Text="lookscreen"/> <shell:ApplicationBarMenuItem IsEnabled="True" Text="skydrive"/> <shell:ApplicationBarMenuItem IsEnabled="True" Text="feedback"/> </shell:ApplicationBar.MenuItems> <shell:ApplicationBarIconButton IconUri="/Assets/AppBar/transport.rew.png" IsEnabled="True" Text="previus"/> <shell:ApplicationBarIconButton IconUri="/Assets/AppBar/save.png" IsEnabled="True" Text="save"/> <shell:ApplicationBarIconButton IconUri="/Assets/AppBar/share.png" IsEnabled="True" Text="share"/> <shell:ApplicationBarIconButton IconUri="/Assets/AppBar/transport.ff.png" IsEnabled="True" Text="next"/> </shell:ApplicationBar> </phone:PhoneApplicationPage.ApplicationBar> <i:Interaction.Behaviors> <AppBarUtils:AppBarItemCommand Id="next" Type="Button" Text="{Binding LocalizedResources.NextButton, Mode=OneWay, Source={StaticResource LocalizedStrings}}" Command="{Binding NextImageCommand}"/> <AppBarUtils:AppBarItemCommand Id="share" Type="Button" Command="{Binding ShareCommand}" Text="{Binding LocalizedResources.ShareButton, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/> <AppBarUtils:AppBarItemCommand Id="previus" Type="Button" Command="{Binding PreviusImageCommand}" Text="{Binding LocalizedResources.PreviusButton, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/> <AppBarUtils:AppBarItemCommand Id="save" Type="Button" Command="{Binding SaveCommand}" Text="{Binding LocalizedResources.SaveButton, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/> <AppBarUtils:AppBarItemCommand Id="lookscreen" Type="MenuItem" Command="{Binding LookScreenCommand}" Text="{Binding LocalizedResources.ScreenItem, Mode=OneWay, Source={StaticResource LocalizedStrings}}"/> <AppBarUtils:AppBarItemCommand Id="skydrive" Type="MenuItem" Command="{Binding SkyDriveCommand}" Text="{Binding LocalizedResources.SkydriveItem, Mode=OneWay, Source={StaticResource LocalizedStrings}}" /> <AppBarUtils:AppBarItemCommand Id="donate" Type="MenuItem" Text="{Binding LocalizedResources.DonateMenuItem, Mode=OneWay, Source={StaticResource LocalizedStrings}}" Command="{Binding DonateCommand }"/> <AppBarUtils:AppBarItemCommand Id="feedback" Type="MenuItem" Text="{Binding LocalizedResources.FeedbackAppBarButton, Mode=OneWay, Source={StaticResource LocalizedStrings}}" Command="{Binding FeedbackCommand}"/> </i:Interaction.Behaviors> <!--LayoutRoot   ,     --> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock Text="{Binding LocalizedResources.ApplicationTitle, Mode=OneWay, Source={StaticResource LocalizedStrings}}" Style="{StaticResource PhoneTextGroupHeaderStyle}" Margin="12,0"/> </StackPanel> </Grid> </phone:PhoneApplicationPage> 



लेख के अंत में मैं एक बार फिर AppBarUtils परियोजना पृष्ठ के लिंक को इंगित करूंगा, इस पुस्तकालय का उपयोग करने के उदाहरणों के लिए कई लिंक भी हैं, स्रोत निर्धारित नहीं हैं।

मुझे उम्मीद है कि यह छोटा लेख नौसिखिया डेवलपर्स को अनुप्रयोगों को स्थानीय बनाने में मदद करेगा, और अनुभवी डेवलपर्स को आम गलतफहमी से छुटकारा मिलेगा कि डेटा पैनल का उपयोग करके आवेदन पैनल को स्थानीयकृत नहीं किया जा सकता है।


Source: https://habr.com/ru/post/In206962/


All Articles