Page 14 - MSDN Magazine, March 2018
P. 14

the properly formatted JsonValue, I can send it to the CallCooke­ BingeFunctionAsync method along with the name of the function, StoreScores. Notice that the type I expect to be returned is a string, which will be a notification from the StoreScores Azure Function of the function’s success or failure.
Wiring up the UI to the CloudService
With the CloudService methods in place, I can finally make sure the UI interacts with them. Recall that when a Binge is saved, the code in MainPage.xaml.cs calls a method in BingeService that stores that data to the local database. That same method, shown in Figure 3, now also sends the binge data to the CloudService to store it in the cloud via the StoreScores Azure Function.
What began as an exercise to try out the latest version of
EF Core 2 on Windows-based mobile devices turned into quite an adventure for me.
Both of the other methods that interact with the Azure Functions return lists of ScoreViewModel objects.
To display the scores stored in the cloud, as shown in Figure 1, I added a method to MainWindow.xaml.cs that calls the Cloud­
Figure 3 The Pre-Existing RecordBinge Method Now Sends the Binge to the Cloud
Service methods to retrieve the scores and then binds them to the relevant ListViews on the page. I named this method ReloadScores because it’s also called by the refresh button on the same page:
private async Task ReloadScores() {
using (var cloudService = new BingeCloudService()) {
YourScoresList.ItemsSource = await cloudService.GetUserTopScores(); GlobalScoresList.ItemsSource =
await cloudService.GetTopGlobalScores();
The UI then displays the score data based on the templates defined for each list on the page. For example, Figure 4 shows XAML for displaying the GlobalScores in the UI.
Wrapping Up This Four-Part Series
What began as an exercise to try out the latest version of EF Core 2 on Windows­based mobile devices turned into quite an adven­ ture for me, and I hope it was a fun, interesting and educational journey for you, as well. Working with the new .NET Standard 2.0­based UWP, especially in its early pre­release days, was cer­ tainly challenging for this back­end developer. But I loved the idea of being able to store data both locally and in the cloud and gaining new skills along the way.
The second and third column in the series were my very first experiences working with Azure Functions and I’m so happy I had an excuse to do so because I’m now a huge fan of this technology and have done much more with it since those first steps. I certainly hope you’ve been equally inspired!
As you saw in this article, interacting with those functions from the UWP app isn’t as straightforward as my earlier experience making Web calls from other platforms. I personally got great sat­ isfaction figuring out the workflow.
If you check out the download, you’ll see the other addition I made to the application—all of the logic for registering to cloud storage, saving the Azure­generated UserId, as well as a name for the device, and registering additional devices and then accessing the UserId and device name for use in the StoreScores and Get­ UserScores methods. I’ve downloaded the entire Azure Function App into a .NET project so you can see and interact with all of the functions that support the app. I spent a surprising amount of time puzzling through the identity workflow and became some­ what obsessed with the entertainment of working it out. Perhaps I’ll write about that someday, as well. n
Julie lerman is a Microsoft Regional Director, Microsoft MVP, software team coach and consultant who lives in the hills of Vermont. You can find her presenting on data access and other topics at user groups and conferences around the world. She blogs at the thedatafarm.com/blog and is the author of “Programming Entity Framework,” as well as a Code First and a DbContext edition, all from O’Reilly Media. Follow her on Twitter: @julielerman and see her Pluralsight courses at juliel.me/PS-Videos.
Thanks to the following technical expert for reviewing this article: Ginny Caughey (Carolina Software Inc.)
Figure 4 XAML Data Binding the Score Data Returned from an Azure Function
} }
public static void RecordBinge(int count, bool worthIt) {
var binge = new CookieBinge{HowMany = count, WorthIt = worthIt, TimeOccurred = DateTime.Now};
using (var context = new BingeContext(options)) {
context.Binges.Add(binge);
context.SaveChanges(); }
using (var cloudService = new BingeCloudService()) {
cloudService.SendBingeToCloudAsync(count, worthIt, binge.TimeOccurred); }
}
<ListView x:Name="GlobalScoresList" > <ListView.ItemTemplate>
<DataTemplate >
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="24" Text="{Binding score}" VerticalAlignment="Center"/>
<TextBlock FontSize="16" Text="{Binding displayGlobalScore}" VerticalAlignment="Center" />
</StackPanel> </DataTemplate>
</ListView.ItemTemplate> </ListView>
10 msdn magazine
Data Points


































































































   12   13   14   15   16