Skip to content

Commit 2127833

Browse files
Merge pull request #3673 from MicrosoftDocs/alvinashcraft-main-update-maui-graph-api-tutorial
Update MAUI Graph tutorial to remove MVVM references
2 parents 0639e7f + e452f2a commit 2127833

File tree

1 file changed

+41
-42
lines changed

1 file changed

+41
-42
lines changed

hub/apps/windows-dotnet-maui/tutorial-graph-api.md

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Tutorial--Create a .NET MAUI app integrating with the Microsoft Graph SDK
33
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.
44
ms.topic: tutorial
5-
ms.date: 02/22/2023
5+
ms.date: 07/13/2023
66
keywords: windows win32, desktop development, Windows App SDK, .net maui
77
ms.localizationpriority: medium
88
---
@@ -245,90 +245,89 @@ After the authentication completes, you will see your user's **DisplayName** in
245245

246246
Now that the `GraphService` is returning user information, let's update the user interface to display some additional user profile information.
247247

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:
282249

283250
``` xaml
284251
<VerticalStackLayout
285252
Spacing="25"
286253
Padding="30,0"
287254
VerticalOptions="Center">
255+
288256
<Label
289-
Text="{Binding Path=HelloMessage, Mode=OneWay}"
257+
x:Name="HelloLabel"
258+
Text="Hello, World!"
290259
SemanticProperties.Description="Displays a welcome message for the user"
291260
SemanticProperties.HeadingLevel="Level1"
292261
FontSize="32"
293262
HorizontalOptions="Center" />
263+
294264
<Button
295265
x:Name="CounterBtn"
296266
Text="Click me"
297267
SemanticProperties.Hint="Counts the number of times you click"
298-
Command="{Binding Path=IncrementCounterCommand}"
268+
Clicked="CounterBtn_Clicked"
299269
HorizontalOptions="Center" />
270+
300271
<Button
301272
Text="Load User Info"
302273
SemanticProperties.Hint="Loads user information from Microsoft Graph"
303-
Command="{Binding Path=LoadUserInformationCommand}"
274+
Clicked="GetUserInfoBtn_Clicked"
304275
HorizontalOptions="Center" />
276+
305277
<Label
306-
Text="{Binding Path=UserName, Mode=OneWay}"
278+
x:Name="DisplayNameLabel"
279+
Text="Display name"
307280
SemanticProperties.Description="Displays the user's display name from Microsoft Graph."
308281
SemanticProperties.HeadingLevel="Level2"
309282
FontSize="18"
310283
HorizontalOptions="Center" />
284+
311285
<Label
312-
Text="{Binding Path=UserGivenName, Mode=OneWay}"
286+
x:Name="UserFirstNameLabel"
287+
Text="First name"
313288
SemanticProperties.Description="Displays the user's first name info from Microsoft Graph"
314289
SemanticProperties.HeadingLevel="Level2"
315290
FontSize="18"
316291
HorizontalOptions="Center" />
292+
317293
<Label
318-
Text="{Binding Path=UserSurname, Mode=OneWay}"
294+
x:Name="UserLastNameLabel"
295+
Text="Last name"
319296
SemanticProperties.Description="Displays the user's last name from Microsoft Graph"
320297
SemanticProperties.HeadingLevel="Level2"
321298
FontSize="18"
322299
HorizontalOptions="Center" />
300+
323301
<Label
324-
Text="{Binding Path=UserPrincipalName, Mode=OneWay}"
302+
x:Name="UserPrincipalNameLabel"
303+
Text="User Principal Name"
325304
SemanticProperties.Description="Displays the user principal name from Microsoft Graph"
326305
SemanticProperties.HeadingLevel="Level2"
327306
FontSize="18"
328307
HorizontalOptions="Center" />
308+
329309
</VerticalStackLayout>
330310
```
331311

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+
332331
Run the app again and click the **Load User Info** button. You should see your user information displayed in the app after authenticating:
333332

334333
![Additional user info has been loaded in labels.](images/maui-graph-additional-user-info-loaded.png)

0 commit comments

Comments
 (0)