.NET 5

Vi är på episod 5, vad passar bättre än att då prata om .NET 5. Microsoft jobbar mot ett .NET efter att .NET började bli spretigt, med .NET Framework, Mono och .NET Core. Daniel och Johan pratar om statusen på ihopslagningen och vad som är nytt .NET 5.


Identiteter, autentisering och säkerhet

I den här episoden prata Daniel och Johan om något som är nödvändigt i nästan alla applikationer, säkerhet. De pratar om att hantera identiteter, autentisering och säkerhet i allmänhet. Områden som de pratar om är bland annat Azure AD B2C, autentisering i .NET Core, autentisering i Xamarin appar och App Service Environments i Azure.

Azure AD B2C: https://azure.microsoft.com/sv-se/services/active-directory/external-identities/b2c/
Identiteter och säkerhet i .NET Core: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-3.1&tabs=visual-studio
MSAL: https://github.com/AzureAD/microsoft-authentication-library-for-dotnet
Web Authenticator: https://docs.microsoft.com/en-us/xamarin/essentials/web-authenticator?tabs=android
App Service Environment, https://docs.microsoft.com/sv-se/azure/app-service/environment/intro

Webutveckling med C# och Blazor med Jimmy Engström

I den här episoden bjöd Johan och Daniel in Jimmy Engström för att prata om Blazor.

Jimmy Engstöm:
Blog: http://engstromjimmy.se/
Twitter: https://twitter.com/engstromjimmy
Blazm: https://github.com/EngstromJimmy/Blazm.Components
Blazm Bluetooth: https://github.com/EngstromJimmy/Blazm.Bluetooth
ZXSpectrum: https://zxspectrum.azurewebsites.net/
Twitch: https://www.twitch.tv/codingafterwork
Podcast: http://www.codingafterwork.se/

You only need a OneTime binding

When creating a binding in Xamarin.Forms, the default is that you create an OneWay binding (except for input elements that have TwoWay bindings), this means that it updates the view from the ViewModel whenever the value in the ViewModel changes. But often you don't need a OneWay binding, you only need a OneTime binding, but you use OneWay just because it is the default. A OneTime binding only reads the value once, if the property changes after that, the UI is not updated. That means that the UI doesn't need to listen to changes in the ViewModel, and that can affect performance in a good way.

If you are familiar with UWP development and compiled bindings with x:Bind you may know that there is OneTime default and the reason for that is that it gives you a more performant app. So why not use it with Xamarin.Formas as well.

To use OneTime binding one tip from the Jason Smith, one of the founders of Xamarin.Forms, according to this tweet:

In this post, I will show some scenarios where you can use OneTime binding. But I recommend you to use it whenever possible.

When using CollectionView
You maybe will change the data source for a CollectionView (read below for a static data example), so you still need a TwoWay binding there, and often you will set the value of the property after that you have fetched data from a data source. But inside your DataTemplate, you can often use OneTime bindings, and if you have some elements that need to listen for changes, you can use OneTime bindings just for those elements.

When using Commands
When using commands in ViewModels, you often write to them like below, and you never change the value of the property.

In this case, there is no need to use the default OneWay binding because you know that the value of the property will never change.

When having static data
Sometimes you need to bind an element to a collection of static data, for example when you are using a CarouselView or a CollectionView where you specify the data in a ViewModel. The reason that you want to do that is that you can't add items to those elements directly (you can specify ItemSource in XAML if you want).

The property in the ViewModel:

A CarouselView that binds to the property:

Building a styleable “Content Button” using PancakeView

Sometimes you need a more flexible button than the one that is shipped with Xamarin.Forms. That was the case for me with the app I am working with right now. I wanted to be able to have more flexible content and I also wanted to style it more than I was able to when I used the default Button. So I decided to create my own Button control. I decided to use PancakeView by Steven Thewissen as the base class for my control. PancakeView is a great library if you want to have views and be more flexible when we are talking about corner radius, shadows, and gradients.

To make a button controls of PancakeView you need to add code to handle when a user taps the control. I added bindable properties for Command and CommandParameter as Xamarin.Forms.Button has, so that the user can bind a command to it.

In the constructor of the control, I am adding some default styling, that could be overridden by setting the properties in the view that you are using the control in. I also add a TapGestureRecognizer and an event handler for it in the constructor.

I wanted some feedback for the user when the button is tapped. So I added code to scale the control down and up again when a user tapped the control, to be able to easily set how much it should scale I added a property with the name AnimationScale.

In the event handler for the TapGestureRecognizer I do the animations and execute the Command if it is set. I execute the command before I animate the scale back to its original value. This because I think the delay is too long to wait for both animations. But it is nice to animate the value back, in case you not navigating when the users are pressing the button. But here you can write whatever feedback that fit your needs.

Then you can use it like this in yout views: