Coder Social home page Coder Social logo

sterasaki / avaloniamauihybrid Goto Github PK

View Code? Open in Web Editor NEW

This project forked from avaloniaui/avaloniamauihybrid

0.0 1.0 0.0 400 KB

Brings templated controls, pixel perfect rendering, animations, styling and composition to Maui

License: MIT License

C# 100.00%

avaloniamauihybrid's Introduction

Hybrid_Header

Avalonia.Maui Hybrid

This repository contains source code for the integration of Avalonia and MAUI frameworks, supporting the following scenarios:

  • Embedding Avalonia controls inside MAUI pages.
  • Embedding MAUI controls inside Avalonia views.
  • Calling MAUI Essentials APIs from Avalonia.

Supported OS: iOS and Android only.

  • For Windows support, please refer to the open issue #7.
  • For macOS support, please refer to the open issue #10.
  • Note that MAUI doesn't support the Browser platform. For Blazor integration, see Avalonia.Browser.Blazor.
  • Future support for Tizen backend will be possible with Avalonia 11.1.
  • You can make an solution that compiles avalonia maui hybrid for mobile targets and compiles without maui hybrid for other platforms. See AvaloniaUI#12

Quick Start

Embedding Avalonia Controls Inside MAUI

Demo project to try: https://github.com/AvaloniaUI/AvaloniaMauiHybrid/tree/main/MauiSample

  1. Start with a normal MAUI project.
  2. Install https://www.nuget.org/packages/Avalonia.Maui nuget package to your project.
  3. You need to have both Avalonia and MAUI Application classes created. You can copy AvaloniaApp.axaml + AvaloniaApp.axaml.cs or reuse your own application with properties.
  4. Update your MAUI app builder to include UseAvalonia call:
     builder
       .UseMauiApp<App>() // MAUI Application
       .UseAvalonia<AvaloniaApp>() // Avalonia Application
  5. If you need to modify Avalonia application builder, you can pass a lambda to the UseAvalonia method.
  6. Now, you can use Avalonia controls from the MAUI XAML:
    <StackLayout Orientation="Horizontal"  HorizontalOptions="FillAndExpand" VerticalOptions="Start">
         <maui:AvaloniaView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
             <ava:Button Content="Avalonia Button"/>
         </maui:AvaloniaView>
         <Button Text="Maui Button"/>
     </StackLayout>
    Don't forget to add Avalonia xmlns namespaces to your MAUI XAML file xmlns:maui="clr-namespace:Avalonia.Maui.Controls;assembly=Avalonia.Maui" and xmlns:ava="clr-namespace:Avalonia.Controls;assembly=Avalonia.Controls", if IDE didn't include it automatically.

Embedding MAUI controls inside of the Avalonia app

Demo project to try: https://github.com/AvaloniaUI/AvaloniaMauiHybrid/tree/main/AvaloniaSample

  1. Start with avalonia.xplat template (see https://github.com/AvaloniaUI/avalonia-dotnet-templates). We will only use Android, iOS and shared projects.
  2. Install https://www.nuget.org/packages/Avalonia.Maui nuget package to your project.
  3. You need to have both Avalonia and MAUI Application classes created. For MAUI Application, you need to inherit Microsoft.Maui.Controls.Application, for example - MauiApplication.cs.
  4. Add <UseMaui>true</UseMaui> to every project (shared and platform-specific) from where you will use MAUI APIs. For example, here and here.
  5. Update both MainActivity (Android project) and AppDelegate (iOS project) app builders to include .UseMaui()
     protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
     {
       return base.CustomizeAppBuilder(builder)
           .UseMaui<AvaloniaSample.Maui.MauiApplication>(this);
     }
  6. If you need to modify MAUI application builder, you can pass a lambda to the UseMaui method. For example, we enable third-party MAUI controls this way.
  7. Not, you can use MAUI controls from the Avalonia XAML:
     <UniformGrid Columns="2" Height="40">
       <Button Content="Avalonia Button" />
       <controls:MauiControlHost>
         <mauiControls:Button Text="MAUI Button" />
       </controls:MauiControlHost>
     </UniformGrid>
    Don't forget about xmlns namespaces here as well: xmlns:controls="using:Avalonia.Maui.Controls" and xmlns:mauiControls="using:Microsoft.Maui.Controls"

Using MAUI Essentials inside of the Avalonia app

  1. Follow the same steps as in Embedding MAUI controls inside of the Avalonia app except for the last one.
  2. Depending on MAUI version, you might need to add <UseMauiEssentials>true</UseMauiEssentials> to your csproj file as well.
  3. Now you can call Essentials API from Avalonia code:
private async void Button_OnClick(object? sender, RoutedEventArgs e)
{
    var location = await Geolocation.GetLocationAsync();
    Console.WriteLine(location.ToString());
}

Don't forget about enabling specific permissions. In the case of Geolocation class, you can follow this documentation from Microsoft.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.