This article describes how to implement search contract and share contract based on XAML + C. We usually search in Windows 8 (using the shortcut key windows icon + q). Next we will introduce the implementation steps one by one.
1. First, we will introduce the implementation of the search function.
1) Search contract statement:
Open the sample program mentioned above, open the package. appxmanifest file, select the Declaration tab, such as (select in the red rectangle), find the selection search declaration, and then add:
Then we deploy the application on the computer. After the deployment is complete, press the Windows + q key combination. We can see that our sample program is already in the search panel. If no search declaration is added here, it cannot be seen in the search panel.
2) Search Activation
Open the app. xml. CS file and add the following method to it:
ProtectedAsyncOverride void onsearchactivated (searchactivatedeventargs ARGs)
{
String data = args. querytext;
// Add your own method, and upload the entered keywords to the application for search
If (ARGs. previusexecutionstate = applicationexecutionstate. notrunning)
{
Await pagestart (ARGs, data); // Private method defined in APP. xml. cs. The following describes
Return;
}
Pageupdate (data); // Private method defined in APP. xml. cs. The following describes
}
Note: In the original method, there is no red mark. This is because asynchronous (await) is used, so you need to add it.
This method is called when the application is activated for the purpose of displaying search results. That is, you can enter the content from the search panel and press enter to activate the method.
2. Implementation of sharing functions
1) ShareContract statement:
Open the sample program mentioned above, open the package. appxmanifest file, select the Declaration tab, such as (select in the red rectangle), find the selection sharing declaration, and then add:
Note that the preceding Red Cross is caused by the error because we have not set the shared data type. Here, my application needs to use the text type, so I chose the text type, as shown in:
Now we can open the sharing panel for sharing, but before that, we need to activate sharing.
2) share Activation
Open the app. xml. CS file and add the following method to it:
ProtectedAsyncOverride void onsharetargetactivated (sharetargetactivatedeventargs ARGs)
{
String data = await args. Operation. Data. gettextasync ();
If (ARGs. previusexecutionstate = applicationexecutionstate. notrunning)
{
Await pagestart (ARGs, data); // custom method to perform operations on applications in different States
Return;
}
Pageupdate (data); // custom method to perform operations on applications in different States
}
Note: custom methods used in APP. XAML. CS
/// <Summary>
/// Initialization page when the program starts
/// </Summary>
/// <Param name = "ARGs"> </param>
/// <Returns> </returns>
Private async task pagestart (iactivatedeventargs ARGs, string data)
{
Frame rootframe = Window. Current. content as frame;
If (rootframe = NULL)
{
Rootframe = new frame ();
If (ARGs. previusexecutionstate = applicationexecutionstate. Terminated)
{
// Todo: Load status from the previously suspended application
}
}
If (window. Current. content = NULL)
{
Rootframe = new frame ();
Rootframe. navigate (typeof (mainpage ));
Window. Current. content = rootframe;
}
Window. Current. Activate ();
}
Private void pageupdate (string data)
{
Frame rootframe = Window. Current. content as frame;
If (rootframe = NULL)
{
Rootframe = new frame ();
}
If (rootframe. content = NULL)
{
If (! Rootframe. navigate (typeof (mainpage), data ))
{
Throw new exception ("failed to create initial page ");
}
}
Else
{
Mainpage page = rootframe. content as mainpage;
If (page = NULL)
{
Page = new mainpage ();
Rootframe. content = page;
}
Page. searchkey (data); // This method is public on the my dictionary page. When it is used, the incoming words are queried and displayed. This method is primarily used to query words when sharing
}
Window. Current. content = rootframe;
Window. Current. Activate ();
}
}
The search and sharing functions are implemented. The custom methods in the program are modified based on different programs.
For more information, see:
Bytes
Http://www.silverlightchina.net/html/windows8/study/2012/0621/16882.html
Http://www.silverlightchina.net/html/windows8/study/2012/0621/16880.html