{"id":1557,"date":"2013-01-14T00:00:00","date_gmt":"2013-01-14T00:00:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/windows-store-app-contracts\/"},"modified":"2021-05-17T18:36:10","modified_gmt":"2021-05-17T18:36:10","slug":"windows-store-app-contracts","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/development\/dotnet-development\/windows-store-app-contracts\/","title":{"rendered":"Windows Store App Contracts"},"content":{"rendered":"<div class=\"article-content\">\n<h1>Introduction<\/h1>\n<p class=\"start\">Amongst the many new features of Windows 8 was the new programming model called Windows Store Application, previously known as &#8216;Metro&#8217;. This introduced a whole new Tile-based application development paradigm where applications are designed to share a universal &#8216;toolbar&#8217; called the &#8216;Charm Bar&#8217;. The Charm bar is activated using Win+Q keys, using a swipe from right bottom to top edge of the screen or using an upward mouse movement from right bottom edge. It contains buttons, called &#8216;Charms&#8217;, for Search, Share, Devices, and Settings. Each of the Charms represents functionality that can be added to your application through what are called contracts. Contracts save the application from having to implement the user-interface to do this, and gives the user a consistent way of searching, printing, and sharing. This Charm Bar is the most obvious and most visible use of contracts, but contracts are of wider value than this. <\/p>\n<p>With contracts, the new unified communication model defines how applications can interact. When you define a contract, you are specifying only the requirements for participating in a communication rather than on how the other application will work with this interface. This ensures only that the applications that are implementing the Contract can communicate with each other irrespective of their internal implementation. <\/p>\n<p>If, for example, you are writing a traditional Windows application that needs to share such data as images, brochures, content or links, you&#8217;d need to write one module for sharing the content in email using the SMTP service, another module for sharing the same content with Facebook and yet another for sharing with SkyDrive. Any module that we created for sharing the data through email would not work with other &#8216;share targets&#8217; such as SkyDrive or Facebook. As shown in figure 1, we would require 6 different modules at minimum for implementing the content sharing through email, Facebook and SkyDrive. The implementation will differ from application to application. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-facf2c9c-a423-4741-9f8d-ea45e7c0285b.png\" alt=\"1716-facf2c9c-a423-4741-9f8d-ea45e7c0285\" \/><\/p>\n<p class=\"caption\">Figure 1: Traditional Share Implementation <\/p>\n<p>But, if the app is a Windows Store (Metro) app, you just need to implement the Share Source Contract which will then be able to share the content with all these three applications. As shown in figure 2, each application will implement the Share Source or Share Target contract in the same way and so will be able to share the content with various applications that do likewise. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-3837ac47-2c15-4f31-a310-ba29ec5d8855.png\" alt=\"1716-3837ac47-2c15-4f31-a310-ba29ec5d885\" \/><\/p>\n<p class=\"caption\">Figure 2: Share Contract Implementation <\/p>\n<h1>Windows 8 Contracts<\/h1>\n<p>Contracts that are defined in Windows 8 are: <\/p>\n<ol>\n<li><strong>Search<\/strong> Contract &#8211; defines that the application is search enabled.  <\/li>\n<li><strong>Share<\/strong> Contract &#8211; defines that the application is ready to share its content with any other application or it is ready to accept specific types of data from any other application.  <\/li>\n<li><strong>Settings<\/strong> Contract &#8211; define the settings specific to the application.  <\/li>\n<li><strong>Play To<\/strong> Contract &#8211; stream the audio, video and images to DLNA-enabled devices.  <\/li>\n<li><strong>File Picker<\/strong> Contract &#8211; defines that the application is used as a file saving location or can be used as a file open location. Files can be opened and saved directly into the application.  <\/li>\n<li><strong>Cached File Updater<\/strong> Contract &#8211; track the file updates and bring the latest version to the user. <\/li>\n<\/ol>\n<p>There are considerable advantages in implementing these contracts. <\/p>\n<ol>\n<li><strong>User Friendly<\/strong>: Users will find it easier to do general tasks such as searching or changing configuration settings. In previous versions of Windows, few applications use a configuration file for defining the configurations and users might be required to use a configuration wizard or an admin screen for setting the configurations, and search functionality is defined in each application differently in terms of placement, look and feel and implementation.  <\/li>\n<li><strong>Easy to Maintain<\/strong>: Because of the standard implementation of contracts, Windows store applications are easier to maintain. Taking content sharing as an example, there will be a single implementation of the share contract and it follows the predefined format.  <\/li>\n<li><strong>Invoke from Outside<\/strong>: Even if the application is not running, users can perform various operations on the application. No longer does the user need to open each application and locate the search feature and do the operation. Now, with the new search contract, we can perform the search across multiple applications, files, settings, and so on without having to open each application.  <\/li>\n<li><strong>Consistency<\/strong>: Windows 8 OS and the Windows Store applications share the same contracts for performing various activities. Search contract is used for searching files and settings in both the OS level and at application level. File picker contracts are used for saving or opening files from system drives and from other applications.  <\/li>\n<li><strong>High<\/strong><strong>ly Adaptive<\/strong>: Contract based Windows Store application can integrate with any new application without any code modification. If, for example, there is a new development or requirement for sharing data with a new system such as LinkedIn or Google+, then the application will work with new systems that implement the same contract without any code modification. <\/li>\n<\/ol>\n<h2>Search Contract<\/h2>\n<p>The Search contract defines the way that the application is ready for search. <\/p>\n<p>Search contracts define a unified search experience in both the OS and applications. Figure 3 shows the default search contract implementation in Windows 8, the &#8216;Charm bar&#8217;. Enabling the Search contract in your application will allow the contents of the app to be searched from anywhere within the system via the Search charm. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-1-ba9fc0b0-e865-4f78-9d4a-f0e7402592e5.png\" alt=\"1716-1-ba9fc0b0-e865-4f78-9d4a-f0e740259\" \/><\/p>\n<p class=\"caption\">Figure 3: Search Contract <\/p>\n<h3>Search Contract Implementation<\/h3>\n<p>We can get the same search experience in our application. First, for this walk-through, we&#8217;ll define a Windows Store application using any of the project templates in VS 2012 and then show the rudiments setting up the various contracts. I have used the Grid App template associated with the Windows Store App <\/p>\n<h4>Step 1: Declare the Search Contract in App Manifest<\/h4>\n<p>Open the <strong>Package.appxmanifest<\/strong> file and locate the Declarations section. Add the Search Contract from the declarations list. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-p1-aebac593-938b-477a-96b0-51f125151073.PNG\" alt=\"1716-p1-aebac593-938b-477a-96b0-51f12515\" \/><\/p>\n<p class=\"caption\">Figure 4: Package manifest <\/p>\n<h4>Step 2: Add the Search Contract item<\/h4>\n<p>Select the &#8216;<em>Add new item<\/em><em>&#8216; <\/em>option and select the &#8216;<em>Search Contract<\/em> &#8216;item. The &#8216;<em>Search Contract<\/em><em>&#8216;<\/em> item will add the default search result page and the <strong>OnSearchActivated<\/strong> method in the <strong>App.xaml.cs<\/strong> file. We will discuss about this method latter. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-p2-a6c9cb25-954c-4b74-9604-1d1ee54513dc.PNG\" alt=\"1716-p2-a6c9cb25-954c-4b74-9604-1d1ee545\" \/><\/p>\n<p class=\"caption\">Figure 5: Search Contract Item <\/p>\n<h4>Step 3: Handle the QuerySubmitted event<\/h4>\n<p>When we invoke the search charm, it will raise the <strong>QuerySubmitted<\/strong> event. Register the <strong>QuerySubmitted<\/strong> event handler in the <strong>OnWindowCreated<\/strong> method of <strong>app.xmal.cs<\/strong> file. When the search query is submitted to the application, opens the Search result page with the result. Retrieve the query text and navigate to the Search result page. <\/p>\n<pre class=\"lang:c# theme:vs2012\">  protected override void OnWindowCreated(WindowCreatedEventArgs args)\n        {\n            base.OnWindowCreated(args);\n            Windows.ApplicationModel.Search.SearchPane.GetForCurrentView().QuerySubmitted += App_QuerySubmitted;\n        }\n\n  void App_QuerySubmitted(Windows.ApplicationModel.Search.SearchPane sender, Windows.ApplicationModel.Search.SearchPaneQuerySubmittedEventArgs args)\n        {\n            var previousContent = Window.Current.Content;\n            var frame = previousContent as Frame;\n            frame.Navigate(typeof(MySearchResult), args.QueryText);\n        }<\/pre>\n<h4>Step 4: Define the Search logic<\/h4>\n<p>Firstly, we must define the custom search logic to retrieve our application-specific data inside the search results page. If we are planning to display any filters on top of the search result page, we need to add them in the <strong>LoadState<\/strong> method of Search result page. <\/p>\n<p>We must implement the search logic inside the <strong>Filter_SelectionChanged<\/strong> method. For our sample implementation, I have defined a <strong>GetItems<\/strong> method inside the <strong>SampleDataSource<\/strong> provided as part of the project template. This method will return all the items with title or content containing the specified query string. <\/p>\n<p>Once the result is available, pass the same to Results view model. <\/p>\n<pre class=\"lang:c# theme:vs2012\">object queryText;\n                this.DefaultViewModel.TryGetValue(\"QueryText\", out queryText);\n\n                var data = SampleDataSource.GetItems(queryText.ToString().Trim('\"'));\n                this.DefaultViewModel[\"Results\"] = data;<\/pre>\n<h4>Step 5: Test Search<\/h4>\n<p>Now our application is search-enabled. Run the application and invoke the Search charm and perform the search. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-1-f4cf00ca-4321-4744-9169-f5cb1b503efe.png\" alt=\"1716-1-f4cf00ca-4321-4744-9169-f5cb1b503\" \/><\/p>\n<p class=\"caption\">Figure 6: Search Contract Result <\/p>\n<h3>Compare with Windows 8 Implementation<\/h3>\n<p>Search is now enabled in our application. We will now run our search contract implementation with the OS level search implementation in the Charm bar. <\/p>\n<h4>1. Invoke by typing<\/h4>\n<p>If we go to the start screen and start typing, this will invoke the Search charm and start entering the text in the search box. You can see that this feature is not working in our application. At the moment, our application will respond to the search only when we explicitly invoke the search charm. We can incorporate the feature of initiating a search on every key press in our application by setting the <strong>ShowOnKeyboardInput<\/strong> property of the SearchPane to true. <\/p>\n<pre class=\"lang:c# theme:vs2012\">protected override void OnWindowCreated(WindowCreatedEventArgs args)\n        {\n            base.OnWindowCreated(args);\n            SearchPane.GetForCurrentView().ShowOnKeyboardInput = true;\n\t\t...........................................................\n}<\/pre>\n<p>Now, when we open the application and start typing on any page, this will invoke the search charm. <\/p>\n<h4>2. Add query suggestions<\/h4>\n<p>Another feature that is missing in our application is the list of search query suggestions, which will appear just below the search box in the search charm depending on the search text that is entered. We can implement this by handling the SearchPane&#8217;s <strong>SuggestionRequested <\/strong>event. <\/p>\n<p>We need to handle this event inside the <strong>OnWindowCreated<\/strong> method. <\/p>\n<pre class=\"lang:c# theme:vs2012\">protected override void OnWindowCreated(WindowCreatedEventArgs args)\n        {\n            base.OnWindowCreated(args);\n            SearchPane.GetForCurrentView().ShowOnKeyboardInput = true;\n            Windows.ApplicationModel.Search.SearchPane.GetForCurrentView().QuerySubmitted += App_QuerySubmitted;\n            SearchPane.GetForCurrentView().SuggestionsRequested += App_SuggestionsRequested;\n        }\n\n        void App_SuggestionsRequested(SearchPane sender, SearchPaneSuggestionsRequestedEventArgs args)\n        {\n            args.Request.SearchSuggestionCollection.AppendQuerySuggestion(args.QueryText + \" 1\");\n            args.Request.SearchSuggestionCollection.AppendQuerySuggestion(args.QueryText + \" 2\");\n            args.Request.SearchSuggestionCollection.AppendQuerySuggestion(args.QueryText + \" 3\");\n            args.Request.SearchSuggestionCollection.AppendQuerySuggestion(args.QueryText + \" 4\");\n            args.Request.SearchSuggestionCollection.AppendQuerySuggestion(args.QueryText + \" 5\");\n\n        }<\/pre>\n<p>Append the query suggestions to the <strong>SearchSuggestionCollection<\/strong>. Note, that o<em>nly 5 query suggestion<\/em><em> can be <\/em><em>displayed on the Search charm.<\/em> <\/p>\n<h4>3. Invoke the app from Start Screen<\/h4>\n<p>As you noticed from Figure 1, we can invoke the search charm from the start screen and perform the search on system resources as well as on any applications. If it is not search-enabled, it will display the message saying that the app is not searchable. <\/p>\n<p>If we invoke the Search charm from the start menu and select our application from the list of applications, we&#8217;ll see that that the search is returning the expected search result, even though the application is not running on the system. When the search invokes the app from outside the application, it will call the <strong>OnSearchActivated<\/strong> method added inside the <strong>App.xmal.cs<\/strong> file. This method implements the App Activation logic, which activates the application. This method will respond to the search query when the app is not the main app on the screen. <\/p>\n<h2>Share Contract<\/h2>\n<p>The Share contract allows your application to share your content from the app with other apps on the system. It also means that you can receive items to share through your app from other apps, though you would then need to design the interface in your app for sharing content from other apps <\/p>\n<p>We can implement our application as Share Source, where the app will share the content with other application or as Share Target, where the app will process the data shared by other apps. We can do one or both. <\/p>\n<p>The Share Contract defines a standardized format for sharing contents that allows for various approaches to sharing. <\/p>\n<h3>Share Source Implementation<\/h3>\n<p>Applications that are working in an integrated environment may require sharing their contents with many other applications. By implementing the &#8216;Share source&#8217;, the application is enabled to share content with any app which satisfies the contract requirements. <\/p>\n<h4>Step 1: Identify the Data format<\/h4>\n<p>The first job is to decide the format of the content you are going to share with another app. We can share data in text format, URL, HTML, files, images and custom defined format. For our walkthrough, we will be defining two different custom formats, where in our application will share a link and associated data, and in a second example it will share an attached file. <\/p>\n<h4>Step 2: Handle the DataRequested Event<\/h4>\n<p>When the Share Charm is invoked, it will fire the <strong>DataRequested<\/strong> event to the current page. We need to handle this event inside the page, from where we are going to share the content. Handle the <strong>DataRequest<\/strong> event inside the <strong>OnNavigateTo()<\/strong> method of the page. <\/p>\n<p>Include the namespace corresponding to the <strong>DataTransfermanager<\/strong> <\/p>\n<pre class=\"lang:c# theme:vs2012\">\tusing Windows.ApplicationModel.DataTransfer;\n<\/pre>\n<p>Add the handler. <\/p>\n<pre class=\"lang:c# theme:vs2012\">private DataTransferManager dataTransferManager;  \n\ndatatransferManager = DataTransferManager.GetForCurrentView();\n  \tdatatransferManager.DataRequested += datatransferManager_DataRequested;<\/pre>\n<p>Implement the data sharing logic inside the <strong>DataRequested <\/strong>event handler. Set the properties to the Data property of the<strong> DataRequest<\/strong>. <\/p>\n<pre class=\"lang:c# theme:vs2012\">void datatransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)\n        {\n            DataRequest req = args.Request;\n            req.Data.Properties.Title = \"Sample Title\";\n            req.Data.Properties.Description = \"Sample Content\";\n            req.Data.SetUri(new Uri(\"http:\/\/www.mnsd.com\"));\n        }<\/pre>\n<p>Invoke the Share Contract from the application and observe the share targets available. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-1-3b9ce4bd-b498-432e-a797-470bca0b21af.png\" alt=\"1716-1-3b9ce4bd-b498-432e-a797-470bca0b2\" \/><\/p>\n<p class=\"caption\">Figure 7: Share Source Contract <\/p>\n<p>We&#8217;ll now select the Mail option and see how our shared content will appear in a mail. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-4b4fa6b4-ec3a-4bb8-a6eb-d35ede6025b4.png\" alt=\"1716-4b4fa6b4-ec3a-4bb8-a6eb-d35ede6025b\" \/><\/p>\n<p class=\"caption\">Figure 8: Share Source Contract with Mail Target <\/p>\n<p>You&#8217;ll see that the title is converted as subject, a description is added in the content and the URI is specified in content. Also, you can observe that the system added a new heading and a thumbnail to our content, which is derived from the URI. <\/p>\n<h4>Step 3: Remove the event handler<\/h4>\n<p>Our application will implement the<strong> share <\/strong>source in different ways in different pages. So, when we move out of a page, unregister the event handle inside the<strong><\/strong><strong>OnNavigatedFrom<\/strong> method. <\/p>\n<pre class=\"lang:c# theme:vs2012\">protected override void OnNavigatedFrom(NavigationEventArgs e)\n  {\n            dataTransferManager.DataRequested -= this.dataTransferManager_DataRequested;\n         }<\/pre>\n<p>The next code snippet is another sample for sharing a file. <\/p>\n<pre class=\"lang:c# theme:vs2012\">async void dataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)\n        {\n            DataRequest request = args.Request;\n            request.Data.Properties.Title = \"Sample Title\";\n            request.Data.Properties.Description = \"Sample Content\";\n\n            DataRequestDeferral deferral = request.GetDeferral();\n\n            try\n            {\n                StorageFile file = null;\n                file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(filePath));\n                if (file != null)\n                {\n                    List storageItems = new List();\n                    storageItems.Add(file);\n                    request.Data.SetStorageItems(storageItems);\n                }\n            }\n            finally\n            {\n                deferral.Complete();\n            }\n        }<\/pre>\n<h3>Share Target Implementation<\/h3>\n<p>In the previous section, we mentioned the share source implementation where our application will share content with another application. In this section, we will discuss the way that our application can consume the data shared by another application. <\/p>\n<h4>Step 1: Add the Share Target Contract<\/h4>\n<p>Open the manifest file <strong>Package.appxmanifest<\/strong><strong><\/strong>and add the Share Target from the <strong>Declarations<\/strong> section in the manifest file. Once you add the Share Target, it will ask for the supported file type and the Data formats. Specify the Supported file types, .docx in our example, and data formats, we&#8217;ll specify Text. We can specify one or more file types and data formats. By using the &#8220;<em>Supports any file type<\/em>&#8221; option, we can specify that our application will accept any type of files. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-1-aae10687-6e7c-4230-8242-7779ac794e8d.png\" alt=\"1716-1-aae10687-6e7c-4230-8242-7779ac794\" \/><\/p>\n<p class=\"caption\">Figure 9: Share Target Contract Declaration <\/p>\n<h4>Step 2: Define the OnShareTargetActivated method <\/h4>\n<p>When we select our application as the share target from any other application, the<strong> OnShareTargetActivated<\/strong> method will be invoked. Inside this method we will activate our application, if it is not already activated, and navigate to the page which will be displayed in the share charm. <\/p>\n<pre class=\"lang:c# theme:vs2012\">using Windows.ApplicationModel.Activation;\n.........................................................................................................\nprotected override void OnShareTargetActivated(ShareTargetActivatedEventArgs args) \n        { \n            var rootFrame = new Frame(); \n            rootFrame.Navigate(typeof(ShareTarget), args.ShareOperation); \n            Window.Current.Content = rootFrame; \n            Window.Current.Activate(); \n        } <\/pre>\n<p>For our sample app, we use the <strong>ShareTarget<\/strong> as the share target page, which will be displayed in the Share charm. <\/p>\n<h4>Step 3: Define the Logic to handle Shared Content <\/h4>\n<p>Define the logic inside the Share target page to handle the shared content. For our sample application, we are handling both the URI and text data shared from any other application. Received content will be displayed back to the user in the share charm. <\/p>\n<pre class=\"lang:c# theme:vs2012\">using Windows.ApplicationModel.DataTransfer; \nusing Windows.ApplicationModel.DataTransfer.ShareTarget; \n..........................................................................................\nprivate ShareOperation shareOperation;\n..........................................................................................\nprotected override async void OnNavigatedTo(NavigationEventArgs e)\n        {\n            StringBuilder sb = new StringBuilder();\n            this.shareOperation = (ShareOperation)e.Parameter;\n            await Task.Factory.StartNew(async () =&gt;\n                {\n                    sb.Append(\" \\nTitle:  \" + this.shareOperation.Data.Properties.Title);\n                    sb.Append(\" \\nDescription: \" + this.shareOperation.Data.Properties.Description);\n                    if (this.shareOperation.Data.Contains(StandardDataFormats.Uri))\n                    {\n                        sb.Append(\" \\nUri: \" + await this.shareOperation.Data.GetUriAsync());\n                    }\n                    else if (this.shareOperation.Data.Contains(StandardDataFormats.Text))\n                    {\n                        sb.Append(\" \\nText:  \" + await this.shareOperation.Data.GetTextAsync());\n                    }\n                });\n\n            msg.Text = sb.ToString();\n        }<\/pre>\n<p>Open another application. For our sample, we will open the Internet Explorer application in Windows 8 and try to share some content from the app. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-1-1b69cddd-cc5c-4044-a612-9d7383219d7b.png\" alt=\"1716-1-1b69cddd-cc5c-4044-a612-9d7383219\" \/><\/p>\n<p class=\"caption\">Figure 10: Share target Contract Implementation <\/p>\n<p>We have two options to share our selected text &#8211; mail and our application -SampleApp. Let&#8217;s select the SampleApp. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-1-239b2397-48ba-472c-b27c-943037a9ee47.png\" alt=\"1716-1-239b2397-48ba-472c-b27c-943037a9e\" \/><\/p>\n<p class=\"caption\">Figure 11: Share Target Implementation &#8211; Text Sharing <\/p>\n<p>Now, let us select the URL of the webpage and try to share using our application. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-1-763c541b-ef83-4573-a5cd-c4f6b88b8443.png\" alt=\"1716-1-763c541b-ef83-4573-a5cd-c4f6b88b8\" \/><\/p>\n<p class=\"caption\">Figure 12: Share Target Implementation &#8211; URL Sharing <\/p>\n<h2>Settings Contract<\/h2>\n<p>Another widely-used Contract is the Settings contract, where we define the application settings. This will define a center location for defining the application configurations and authentication and authorization settings. The Settings contract is used for defining the login screen, account settings, application-specific settings, etc. <\/p>\n<p>We can invoke the Settings charm using Win+I keys or by swiping the mouse from right bottom to top. We will define two settings commands with one event handler for our sample application. <\/p>\n<h2>Step 1: Handle the CommandsRequested event<\/h2>\n<p>Handle the<strong> CommandsRequested<\/strong> event, which will be fired when we invoke the Settings charm from our application in App.xaml.cs file. <\/p>\n<pre class=\"lang:c# theme:vs2012\">\tSettingsPane.GetForCurrentView().CommandsRequested += App_CommandsRequested;\n<\/pre>\n<p>Add the SettingsCommands and the corresponding operations inside the event handler. <\/p>\n<pre class=\"lang:c# theme:vs2012\">void  App_CommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)\n        {\n            args.Request.ApplicationCommands.Add(new SettingsCommand(\"Sample\", \"Sample Config\", (y) =&gt; { }));\n            args.Request.ApplicationCommands.Add(new SettingsCommand(\"Sample\", \"Test Config\",  async(x) =&gt; \n                {\n                    var msgDial = new MessageDialog(\"Called from the Config entry in Settings pane\");\n                    msgDial.Commands.Add(new UICommand(\"OK\"));\n                    await msgDial.ShowAsync();\n                }));\n        }<\/pre>\n<p>Invoke the Settings charm from inside the application and see the new settings commands. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-1-9f98b77b-5807-4466-85d5-263ebeaf51d2.png\" alt=\"1716-1-9f98b77b-5807-4466-85d5-263ebeaf5\" \/><\/p>\n<p class=\"caption\">Figure 13: Settings Contract <\/p>\n<p>Click on the &#8216;<em>Test Config<\/em>&#8216; settings command and observe the fired event handler. <\/p>\n<p class=\"illustration\"><img decoding=\"async\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/imported\/1716-1-1e8cdff8-a0cd-41f5-bca0-9de9626d747f.png\" alt=\"1716-1-1e8cdff8-a0cd-41f5-bca0-9de9626d7\" \/><\/p>\n<p class=\"caption\">Figure 14: Settings Contract &#8211; Action <\/p>\n<h1>Conclusion<\/h1>\n<p>Contracts are the centralized communication model introduced in Windows 8. It is hard to imagine a Windows Store application that doesn&#8217;t need to use them. They make individual Windows Store applications easier to develop since a lot of the interface is delegated to the Metro &#8216;Charm bar&#8217;. They also allow much better cooperation and easier data transfer between applications. They give the user a much more consistent user-interface. Contracts are highly adaptive, easy to maintain and implement. Apps developed using Contracts are able to work with any new applications introduced in the market that conform to the centralized communication model. <\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Windows Store Apps use contracts to declare the interactions that they support with other apps and with the &#8216;Charm Bar&#8217;. They bind your app to other &#8216;Metro&#8217; style apps and to the system user-interface: They provide a great deal of functionality for little programming effort.&hellip;<\/p>\n","protected":false},"author":8305,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143538],"tags":[4143,4229,4178,5792,5791,5789,5790],"coauthors":[],"class_list":["post-1557","post","type-post","status-publish","format-standard","hentry","category-dotnet-development","tag-net","tag-net-framework","tag-bi","tag-charm-bar","tag-contracts","tag-unified-communication-model","tag-windows-store"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/8305"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=1557"}],"version-history":[{"count":5,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1557\/revisions"}],"predecessor-version":[{"id":91098,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/1557\/revisions\/91098"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=1557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=1557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=1557"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=1557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}