Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Make it easier to get to the Xamarin.Forms control from the Blazor component #45

Merged
merged 1 commit into from
Jan 17, 2020

Conversation

Eilon
Copy link
Member

@Eilon Eilon commented Jan 17, 2020

You can now do this to access the native Xamarin.Forms control

<StackLayout>
    <Label Text="@("You have pressed " + counter + " times")" />
    <Button @ref="incrementButton" Text="+1" OnClick="HandleClick" />
</StackLayout>

@code {
    Microsoft.MobileBlazorBindings.Elements.Button incrementButton;

    int counter;

    async Task HandleClick()
    {
        counter++;

        await incrementButton.NativeControl.RotateTo(360);
        incrementButton.NativeControl.Rotation = 0; // Reset rotation
    }
}

The NativeControl property on a given Blazor component is strongly-typed to its target Xamarin.Forms type. You can then call methods, set properties, etc.

A word of caution: Changing state in this manner is not tracked by Blazor, which can cause problems. For example, if you set the Text of a Label control in this manner, it will mess up Blazor's behavior. Use this only when you need to alter some native UI state, such as controlling an animation, or calling methods that affect some more global state.

Fixes #39

@Eilon
Copy link
Member Author

Eilon commented Jan 17, 2020

cc @davidortinau @jamesmontemagno

@Eilon Eilon merged commit 84e86c4 into master Jan 17, 2020
@Eilon Eilon deleted the eilon/nativecontrol-access branch January 24, 2020 00:55
@Eilon
Copy link
Member Author

Eilon commented May 8, 2020

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make it easier to get to the Xamarin.Forms control from the Blazor component
1 participant