Skip to content

Commit de46499

Browse files
Merge pull request #3599 from MicrosoftDocs/main638207071016367475sync_temp
For protected CLA branch, push strategy should use PR and merge to target branch method to work around git push error
2 parents bc8aa65 + 328da1e commit de46499

File tree

1 file changed

+71
-7
lines changed

1 file changed

+71
-7
lines changed

uwp/launch-resume/how-to-launch-an-app-for-results.md

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ ms.date: 02/08/2017
66
ms.topic: article
77
keywords: windows 10, uwp
88
ms.localizationpriority: medium
9+
dev_langs:
10+
- csharp
11+
- cppwinrt
912
---
1013
# Launch an app for results
1114

@@ -59,7 +62,7 @@ If this method does not already exist in the launched app, create it within the
5962

6063
In an app that lets you pick your friends in a social network, this function could be where you open the people-picker page. In this next example, a page named **LaunchedForResultsPage** is displayed when the app is activated for results. Ensure that the **using** statement is included at the top of the file.
6164

62-
```cs
65+
```csharp
6366
using Windows.ApplicationModel.Activation;
6467
...
6568
protected override void OnActivated(IActivatedEventArgs args)
@@ -82,31 +85,68 @@ protected override void OnActivated(IActivatedEventArgs args)
8285
}
8386
```
8487

88+
```cppwinrt
89+
using namespace winrt::Windows::ApplicationModel::Activation;
90+
...
91+
protected override void OnActivated(IActivatedEventArgs args)
92+
{
93+
// Window management
94+
Frame rootFrame{ nullptr };
95+
auto content = Window::Current().Content();
96+
if (content)
97+
{
98+
rootFrame = content.try_as<Frame>();
99+
}
100+
101+
if (rootFrame == null)
102+
{
103+
rootFrame = Frame();
104+
Window::Current().Content(rootFrame);
105+
}
106+
107+
// Code specific to launch for results
108+
auto protocolForResultsEventArgs{ args.as<ProtocolForResultsActivatedEventArgs>() };
109+
// Open the page that we created to handle activation for results.
110+
rootFrame.Navigate(xaml_typename<LaunchedForResultsPage>(), protocolForResultsArgs);
111+
112+
// Ensure the current window is active.
113+
Window::Current().Activate();
114+
}
115+
```
116+
85117
Because the protocol extension in the Package.appxmanifest file specifies **ReturnResults** as **always**, the code just shown can cast `args` directly to [**ProtocolForResultsActivatedEventArgs**](/uwp/api/Windows.ApplicationModel.Activation.ProtocolForResultsActivatedEventArgs) with confidence that only **ProtocolForResultsActivatedEventArgs** will be sent to **OnActivated** for this app. If your app can be activated in ways other than launching for results, you can check whether [**IActivatedEventArgs.Kind**](/uwp/api/windows.applicationmodel.activation.iactivatedeventargs.kind) property returns [**ActivationKind.ProtocolForResults**](/uwp/api/Windows.ApplicationModel.Activation.ActivationKind) to tell whether the app was launched for results.
86118

87119
## Step 3: Add a ProtocolForResultsOperation field to the app you launch for results
88120

89121

90-
```cs
122+
```csharp
91123
private Windows.System.ProtocolForResultsOperation _operation = null;
92124
```
93125

126+
```cppwinrt
127+
Windows::System::ProtocolForResultsOperation _operation = nullptr;
128+
```
129+
94130
You'll use the [**ProtocolForResultsOperation**](/uwp/api/windows.applicationmodel.activation.protocolforresultsactivatedeventargs.protocolforresultsoperation) field to signal when the launched app is ready to return the result to the calling app. In this example, the field is added to the **LaunchedForResultsPage** class because you'll complete the launch-for-results operation from that page and will need access to it.
95131

96132
## Step 4: Override OnNavigatedTo() in the app you launch for results
97133

98134

99135
Override the [**OnNavigatedTo**](/uwp/api/windows.ui.xaml.controls.page.onnavigatedto) method on the page that you'll display when your app is launched for results. If this method does not already exist, create it within the class for the page defined in &lt;pagename&gt;.xaml.cs. Ensure that the following **using** statement is included at the top of the file:
100136

101-
```cs
137+
```csharp
102138
using Windows.ApplicationModel.Activation
103139
```
104140

141+
```cppwinrt
142+
using namespace winrt::Windows::ApplicationModel::Activation;
143+
```
144+
105145
The [**NavigationEventArgs**](/uwp/api/Windows.UI.Xaml.Navigation.NavigationEventArgs) object in the [**OnNavigatedTo**](/uwp/api/windows.ui.xaml.controls.page.onnavigatedto) method contains the data passed from the calling app. The data may not exceed 100KB and is stored in a [**ValueSet**](/uwp/api/Windows.Foundation.Collections.ValueSet) object.
106146

107147
In this example code, the launched app expects the data sent from the calling app to be in a [**ValueSet**](/uwp/api/Windows.Foundation.Collections.ValueSet) under a key named **TestData**, because that's what the example's calling app is coded to send.
108148

109-
```cs
149+
```csharp
110150
using Windows.ApplicationModel.Activation;
111151
...
112152
protected override void OnNavigatedTo(NavigationEventArgs e)
@@ -124,23 +164,47 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
124164
private Windows.System.ProtocolForResultsOperation _operation = null;
125165
```
126166

167+
```cppwinrt
168+
using namespace winrt::Windows::ApplicationModel::Activation;
169+
...
170+
protected override void OnNavigatedTo(NavigationEventArgs e)
171+
{
172+
auto protocolForResultsArgs = e.Parameter().try_as<ProtocolForResultsActivatedEventArgs>();
173+
// Set the ProtocolForResultsOperation field.
174+
_operation = protocolForResultsArgs.ProtocolForResultsOperation();
175+
176+
if (protocolForResultsArgs.Data().HasKey("TestData"))
177+
{
178+
string dataFromCaller{ unbox_value<hstring>(protocolForResultsArgs.Data().Lookup("TestData")) };
179+
}
180+
}
181+
...
182+
Windows::System::ProtocolForResultsOperation _operation = nullptr;
183+
```
184+
127185
## Step 5: Write code to return data to the calling app
128186

129187

130188
In the launched app, use [**ProtocolForResultsOperation**](/uwp/api/windows.applicationmodel.activation.protocolforresultsactivatedeventargs.protocolforresultsoperation) to return data to the calling app. In this example code, a [**ValueSet**](/uwp/api/Windows.Foundation.Collections.ValueSet) object is created that contains the value to return to the calling app. The **ProtocolForResultsOperation** field is then used to send the value to the calling app.
131189

132-
```cs
190+
```csharp
133191
ValueSet result = new ValueSet();
134192
result["ReturnedData"] = "The returned result";
135193
_operation.ReportCompleted(result);
136194
```
137195

196+
```cppwinrt
197+
ValueSet result;
198+
result.Insert("ReturnedData", "The returned result");
199+
_operation.ReportCompleted(result);
200+
```
201+
138202
## Step 6: Write code to launch the app for results and get the returned data
139203

140204

141205
Launch the app from within an async method in your calling app as shown in this example code. Note the **using** statements, which are necessary for the code to compile:
142206

143-
```cs
207+
```csharp
144208
using System.Threading.Tasks;
145209
using Windows.System;
146210
...
@@ -197,4 +261,4 @@ Then pass it to the launched app via **LaunchUriForResultsAsync**.
197261

198262

199263

200-
 
264+
 

0 commit comments

Comments
 (0)