|
2 | 2 | title: Tutorial--Create a .NET MAUI app integrating with the Microsoft Graph SDK
|
3 | 3 | description: Get hands-on with .NET MAUI by building a cross-platform app on Windows that leverages the Microsoft Graph SDK to display user data.
|
4 | 4 | ms.topic: tutorial
|
5 |
| -ms.date: 02/22/2023 |
| 5 | +ms.date: 07/13/2023 |
6 | 6 | keywords: windows win32, desktop development, Windows App SDK, .net maui
|
7 | 7 | ms.localizationpriority: medium
|
8 | 8 | ---
|
@@ -245,90 +245,89 @@ After the authentication completes, you will see your user's **DisplayName** in
|
245 | 245 |
|
246 | 246 | Now that the `GraphService` is returning user information, let's update the user interface to display some additional user profile information.
|
247 | 247 |
|
248 |
| -Start by adding some new observable properties to the `MainViewModel` class to store the `DisplayName`, `GivenName`, `Surname`, and `UserPrincipalName` fields from the Graph API response: |
249 |
| - |
250 |
| -``` csharp |
251 |
| -[ObservableProperty] |
252 |
| -private string userName = ""; |
253 |
| - |
254 |
| -[ObservableProperty] |
255 |
| -private string userGivenName = ""; |
256 |
| - |
257 |
| -[ObservableProperty] |
258 |
| -private string userSurname = ""; |
259 |
| - |
260 |
| -[ObservableProperty] |
261 |
| -private string userPrincipalName = ""; |
262 |
| -``` |
263 |
| - |
264 |
| -In MainPage.xaml.cs, set the values of the new properties in the `GetUserInfoBtn_Clicked` event handler with properties of the Graph User object: |
265 |
| - |
266 |
| -``` csharp |
267 |
| -[RelayCommand] |
268 |
| -private async Task LoadUserInformation() |
269 |
| -{ |
270 |
| - var service = new GraphService(); |
271 |
| - Microsoft.Graph.Models.User user = await service.GetMyDetailsAsync(); |
272 |
| - helloMessage = $"Hello, {user.DisplayName}"; |
273 |
| - |
274 |
| - UserName = user.DisplayName; |
275 |
| - UserGivenName = user.GivenName; |
276 |
| - UserSurname = user.Surname; |
277 |
| - UserPrincipalName = user.UserPrincipalName; |
278 |
| -} |
279 |
| -``` |
280 |
| - |
281 |
| -Finally, in MainPage.xaml, update the contents of the `VerticalStackLayout`, removing the existing welcome label and `Image` control and adding four new labels to display the user information. The contents of the `VerticalStackLayout` should now look like this: |
| 248 | +In MainPage.xaml, start by updating the contents of the `VerticalStackLayout`, removing the existing welcome label and `Image` control and adding four new labels to display the user information. Each label that will be updated is named, and we've provided some placeholder text until the data is loaded from the Graph query. The contents of the `VerticalStackLayout` should now look like this: |
282 | 249 |
|
283 | 250 | ``` xaml
|
284 | 251 | <VerticalStackLayout
|
285 | 252 | Spacing="25"
|
286 | 253 | Padding="30,0"
|
287 | 254 | VerticalOptions="Center">
|
| 255 | + |
288 | 256 | <Label
|
289 |
| - Text="{Binding Path=HelloMessage, Mode=OneWay}" |
| 257 | + x:Name="HelloLabel" |
| 258 | + Text="Hello, World!" |
290 | 259 | SemanticProperties.Description="Displays a welcome message for the user"
|
291 | 260 | SemanticProperties.HeadingLevel="Level1"
|
292 | 261 | FontSize="32"
|
293 | 262 | HorizontalOptions="Center" />
|
| 263 | + |
294 | 264 | <Button
|
295 | 265 | x:Name="CounterBtn"
|
296 | 266 | Text="Click me"
|
297 | 267 | SemanticProperties.Hint="Counts the number of times you click"
|
298 |
| - Command="{Binding Path=IncrementCounterCommand}" |
| 268 | + Clicked="CounterBtn_Clicked" |
299 | 269 | HorizontalOptions="Center" />
|
| 270 | + |
300 | 271 | <Button
|
301 | 272 | Text="Load User Info"
|
302 | 273 | SemanticProperties.Hint="Loads user information from Microsoft Graph"
|
303 |
| - Command="{Binding Path=LoadUserInformationCommand}" |
| 274 | + Clicked="GetUserInfoBtn_Clicked" |
304 | 275 | HorizontalOptions="Center" />
|
| 276 | + |
305 | 277 | <Label
|
306 |
| - Text="{Binding Path=UserName, Mode=OneWay}" |
| 278 | + x:Name="DisplayNameLabel" |
| 279 | + Text="Display name" |
307 | 280 | SemanticProperties.Description="Displays the user's display name from Microsoft Graph."
|
308 | 281 | SemanticProperties.HeadingLevel="Level2"
|
309 | 282 | FontSize="18"
|
310 | 283 | HorizontalOptions="Center" />
|
| 284 | + |
311 | 285 | <Label
|
312 |
| - Text="{Binding Path=UserGivenName, Mode=OneWay}" |
| 286 | + x:Name="UserFirstNameLabel" |
| 287 | + Text="First name" |
313 | 288 | SemanticProperties.Description="Displays the user's first name info from Microsoft Graph"
|
314 | 289 | SemanticProperties.HeadingLevel="Level2"
|
315 | 290 | FontSize="18"
|
316 | 291 | HorizontalOptions="Center" />
|
| 292 | + |
317 | 293 | <Label
|
318 |
| - Text="{Binding Path=UserSurname, Mode=OneWay}" |
| 294 | + x:Name="UserLastNameLabel" |
| 295 | + Text="Last name" |
319 | 296 | SemanticProperties.Description="Displays the user's last name from Microsoft Graph"
|
320 | 297 | SemanticProperties.HeadingLevel="Level2"
|
321 | 298 | FontSize="18"
|
322 | 299 | HorizontalOptions="Center" />
|
| 300 | + |
323 | 301 | <Label
|
324 |
| - Text="{Binding Path=UserPrincipalName, Mode=OneWay}" |
| 302 | + x:Name="UserPrincipalNameLabel" |
| 303 | + Text="User Principal Name" |
325 | 304 | SemanticProperties.Description="Displays the user principal name from Microsoft Graph"
|
326 | 305 | SemanticProperties.HeadingLevel="Level2"
|
327 | 306 | FontSize="18"
|
328 | 307 | HorizontalOptions="Center" />
|
| 308 | + |
329 | 309 | </VerticalStackLayout>
|
330 | 310 | ```
|
331 | 311 |
|
| 312 | +Finally, in MainPage.xaml.cs, update the UI elements with the values of the new properties in the `GetUserInfoBtn_Clicked` event handler using the properties of the Graph User object: |
| 313 | + |
| 314 | +``` csharp |
| 315 | +private async void GetUserInfoBtn_Clicked(object sender, EventArgs e) |
| 316 | +{ |
| 317 | + if (graphService == null) |
| 318 | + { |
| 319 | + graphService = new GraphService(); |
| 320 | + } |
| 321 | + user = await graphService.GetMyDetailsAsync(); |
| 322 | + HelloLabel.Text = $"Hello, {user.DisplayName}!"; |
| 323 | + |
| 324 | + DisplayNameLabel.Text = user.DisplayName; |
| 325 | + UserFirstNameLabel.Text = user.GivenName; |
| 326 | + UserLastNameLabel.Text = user.Surname; |
| 327 | + UserPrincipalNameLabel.Text = user.UserPrincipalName; |
| 328 | +} |
| 329 | +``` |
| 330 | + |
332 | 331 | Run the app again and click the **Load User Info** button. You should see your user information displayed in the app after authenticating:
|
333 | 332 |
|
334 | 333 | 
|
|
0 commit comments