BlazorBindings.Maui - Preview 9
Tags: dotnetmaui, dotnet, maui, blazor
Published: 2022-09-23 13:50:01

Another [BlazorBindings.Maui](https://github.com/Dreamescaper/BlazorBindings.Maui) update, which adds component generator, shadows, and other improvements. Take a look at [Get Started](https://dreamescaper.github.io/MobileBlazorBindingsDocs/maui-blazor-bindings/get-started.html) page to check it out!

## About this project

[This project](https://github.com/Dreamescaper/BlazorBindings.Maui) is a fork of MobileBlazorBindings - experimental project by Microsoft to allow to use Blazor syntax for native controls instead of XAML. Here is a sample Counter component that renders native UI, which may look familiar to Blazor developers, that increments a value on each button press:
```xml
<StackLayout>
    <Label FontSize="30">You pressed @count times </Label>
    <Button Text="+1" OnClick="@HandleClick" />
</StackLayout>

@code {
    int count;

    void HandleClick()
    {
        count++;
    }
}
```
![Android emulator](https://camo.githubusercontent.com/554d00d915708b2059da912c31662b22f3ea93d72431c30e278c3d39d3d02ed9/68747470733a2f2f646576626c6f67732e6d6963726f736f66742e636f6d2f6173706e65742f77702d636f6e74656e742f75706c6f6164732f73697465732f31362f323032302f30312f626c617a6f722d616e64726f69642d636f756e7465722d322e676966)

### Previous release notes
- [Preview 6](https://dreamescaper.github.io/MobileBlazorBindingsDocs/maui-blazor-bindings/release-notes/preview-6.html)
- [Preview 7](https://dreamescaper.github.io/MobileBlazorBindingsDocs/maui-blazor-bindings/release-notes/preview-7.html)
- [Preview 8](https://dreamescaper.github.io/MobileBlazorBindingsDocs/maui-blazor-bindings/release-notes/preview-8.html)

## Component Generator

This release adds the ability to generate Blazor components for third party controls. Originally it was planned to implement it as a [source generator](https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview), but, unfortunately, source generators don't play well with razor files. Therefore, generator is packed as a dotnet tool. In order to generate Blazor component for a control, you need to add an assembly attribute with control info, e.g.

```csharp
[assembly: GenerateComponent(typeof(CommunityToolkit.Maui.Views.AvatarView))]
```

Then, you need to install and run generator tool in project folder:

```
dotnet tool install --global BlazorBindings.Maui.ComponentGenerator --version 0.9.2-preview

dotnet generate-maui-blazor-components
```

You take a look at [a sample project](https://github.com/Dreamescaper/BlazorBindings.Maui/blob/main/samples/ThirdPartyControlsSample/Properties/Elements.cs) with generated components for `AlohaKit` and `CommunityToolkit` libraries.

Keep in mind that it doesn't support some parameter types (like Shapes or Templates) so far.

## Shadows

Shadows are supported with Blazor components now:
```xml
<Image Source=@("dotnet_bot.png")
       WidthRequest="250"
       HeightRequest="310">
    <Shadow>
        <Shadow Color="Colors.Black"
                Offset="new (20, 20)"
                Radius="50"
                Opacity="0.8f" />
    </Shadow>
</Image>
```

![Shadows](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nqsjo6ud1vr9gejymui2.png)

## Gesture events

In order to add, for instance, tap event handler for some arbitrary control, you use Maui Gesture Recognizers. And that is already possible with Maui Blazor Bindings as well. However, the syntax is quite verbose, so this release adds events for the most used gestures directly to component (they are still implemented as gesture recognizers underneath):
```xml
<Frame OnTap="OnTap"
       OnDoubleTap="OnDoubleTap"
       OnPanUpdate="OnPanUpdate">

    <Label>@Item - @status</Label>
</Frame>

@code {
    [Parameter] public int Item { get; set; }

    string status = "Pending";

    void OnTap() => status = "Tapped";
    void OnDoubleTap() => status = "Double Tapped";

    async Task OnPanUpdate(PanUpdatedEventArgs args)
    {
        // ...
    }
}
```

![Gesture events](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jj3928glkds5wv0ohqgt.gif)

## Breaking changes

### Significant internal changes

In Preview 9 element wrappers implementation is changed significantly in order to simplify it and to improve performance. Blazor components API is unchanged, so it won't affect most users. But if you have created any custom bindings (e.g. wrapping a third party control), they will probably not work with Preview 9. Hopefully, that woundn't be a huge issue thanks to a component generator.

### TimePicker / DatePicker updates

`TimePicker` is updated to use recently-added `TimeOnly` type instead of `TimeSpan`, as this type is much more suitable to represent time of day. Same, `DatePicker` is updated to use `DateOnly` instead of `DateTime`.

## What next?

- Further ComponentGenerator improvements ([#45](https://github.com/Dreamescaper/BlazorBindings.Maui/issues/45)).
- Navigation service overhaul ([#44](https://github.com/Dreamescaper/BlazorBindings.Maui/issues/44)).

If you have any suggestions or ideas, you are welcome log [issues](https://github.com/Dreamescaper/BlazorBindings.Maui/issues)!