Skip to content

Commit

Permalink
.NET 9 native embedding sample (#511)
Browse files Browse the repository at this point in the history
* Sample update (WIP).

* More Android implementation.

* Make the Android app work.

* Support window context on Android.

* Update min MacCat version.
  • Loading branch information
davidbritch authored Sep 24, 2024
1 parent da77f61 commit e203d28
Show file tree
Hide file tree
Showing 141 changed files with 3,547 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:label="@string/app_name" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true">
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Android.Runtime;
using Android.Views;
using AndroidX.Navigation.Fragment;
using Microsoft.Maui.Controls.Embedding;
using static Android.Views.ViewGroup.LayoutParams;
using Button = Android.Widget.Button;
using Fragment = AndroidX.Fragment.App.Fragment;
using View = Android.Views.View;

namespace NativeEmbeddingDemo.Droid;

[Register("com.companyname.nativeembeddingdemo." + nameof(FirstFragment))]
public class FirstFragment : Fragment
{
Activity? _window;
IMauiContext? _windowContext;
MyMauiContent? _mauiView;
Android.Views.View? _nativeView;

public IMauiContext WindowContext =>
_windowContext ??= MyEmbeddedMauiApp.Shared.CreateEmbeddedWindowContext(_window ?? throw new InvalidOperationException());

public override View? OnCreateView(LayoutInflater inflater, ViewGroup? container, Bundle? savedInstanceState) =>
inflater.Inflate(Resource.Layout.fragment_first, container, false);

public override void OnViewCreated(View view, Bundle? savedInstanceState)
{
base.OnViewCreated(view, savedInstanceState);
_window ??= Activity;

// Create Android button
var androidButton = view.FindViewById<Button>(Resource.Id.button_first)!;
androidButton.Click += (s, e) =>
{
NavHostFragment.FindNavController(this).Navigate(Resource.Id.action_FirstFragment_to_SecondFragment);
};

var animateButton = view.FindViewById<Button>(Resource.Id.button_animate)!;
animateButton.Click += OnAndroidButtonClicked;

//// App context
//// Ensure .NET MAUI app is built before creating .NET MAUI views
//var mauiApp = MauiProgram.CreateMauiApp();

//// Create .NET MAUI context
//var mauiContext = new MauiContext(mauiApp.Services, Activity);

//// Create .NET MAUI content
//_mauiView = new MyMauiContent();

//// Create native view
//_nativeView = _mauiView.ToPlatformEmbedded(mauiContext);

// Window context
// Create MAUI embedded window context
var context = WindowContext;

// Create .NET MAUI content
_mauiView = new MyMauiContent();

// Create native view
_nativeView = _mauiView.ToPlatformEmbedded(context);

// Add native view to layout
var rootLayout = view.FindViewById<LinearLayout>(Resource.Id.layout_first)!;
rootLayout.AddView(_nativeView, 1, new LinearLayout.LayoutParams(MatchParent, WrapContent));
}

public override void OnDestroyView()
{
base.OnDestroyView();

// Remove the view from the UI
var rootLayout = View!.FindViewById<LinearLayout>(Resource.Id.layout_first)!;
rootLayout.RemoveView(_nativeView);

// Cleanup any Window
if (_mauiView?.Window is IWindow window)
window.Destroying();

base.OnStop();
}

async void OnAndroidButtonClicked(object? sender, EventArgs e)
{
if (_mauiView?.DotNetBot is not Image bot)
return;

await bot.RotateTo(360, 1000);
bot.Rotation = 0;

bot.HeightRequest = 90;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Android.Views;
using AndroidX.AppCompat.App;
using AndroidX.Navigation;
using AndroidX.Navigation.UI;
using Google.Android.Material.FloatingActionButton;
using Google.Android.Material.Snackbar;
using Toolbar = AndroidX.AppCompat.Widget.Toolbar;

namespace NativeEmbeddingDemo.Droid
{
[Activity(Label = "@string/app_name", MainLauncher = true, Theme = "@style/Theme.MyApplication")]
public class MainActivity : AppCompatActivity
{
AppBarConfiguration? appBarConfiguration;

protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Set the view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);

var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);

var navController = Navigation.FindNavController(this, Resource.Id.nav_host_fragment_content_main);
appBarConfiguration = new AppBarConfiguration.Builder(navController.Graph).Build();
NavigationUI.SetupActionBarWithNavController(this, navController, appBarConfiguration);

var fab = FindViewById<FloatingActionButton>(Resource.Id.fab)!;
fab.Click += (s, e) =>
{
var snackBar = Snackbar.Make(fab, "Replace with your own action", Snackbar.LengthLong);
snackBar.SetAnchorView(Resource.Id.fab);
snackBar.SetAction("Action", _ => { });
snackBar.Show();
};
}

public override bool OnCreateOptionsMenu(IMenu? menu)
{
MenuInflater.Inflate(Resource.Menu.menu_main, menu);
return true;
}

public override bool OnOptionsItemSelected(IMenuItem item)
{
var id = item.ItemId;
if (id == Resource.Id.SettingsFragment)
{
var navController = Navigation.FindNavController(this, Resource.Id.nav_host_fragment_content_main);
if (NavigationUI.OnNavDestinationSelected(item, navController))
return true;
}
return base.OnOptionsItemSelected(item);
}

public override bool OnSupportNavigateUp()
{
var navController = Navigation.FindNavController(this, Resource.Id.nav_host_fragment_content_main);
return NavigationUI.NavigateUp(navController, appBarConfiguration!) || base.OnSupportNavigateUp();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace NativeEmbeddingDemo.Droid;

public static class MyEmbeddedMauiApp
{
static MauiApp? _shared;

public static MauiApp Shared =>
_shared ??= MauiProgram.CreateMauiApp();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0-android</TargetFramework>
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>

<UseMaui>true</UseMaui>
<MauiEnablePlatformUsings>true</MauiEnablePlatformUsings>

<!-- Visual Studio doesn't support Hot Reload in non-MAUI apps -->
<EnableHotReload>false</EnableHotReload>

<ApplicationId>com.companyname.nativeembeddingdemo</ApplicationId>
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
</PropertyGroup>
<ItemGroup>
<None Remove="Resources\values\dimens.xml" />
<None Remove="Resources\values\themes.xml" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NativeEmbeddingDemo\NativeEmbeddingDemo.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Images, layout descriptions, binary blobs and string dictionaries can be included
in your application as resource files. Various Android APIs are designed to
operate on the resource IDs instead of dealing with images, strings or binary blobs
directly.

For example, a sample Android app that contains a user interface layout (main.xml),
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
would keep its resources in the "Resources" directory of the application:

Resources/
drawable/
icon.png

layout/
main.xml

values/
strings.xml

In order to get the build system to recognize Android resources, set the build action to
"AndroidResource". The native Android APIs do not operate directly with filenames, but
instead operate on resource IDs. When you compile an Android application that uses resources,
the build system will package the resources for distribution and generate a class called "Resource"
(this is an Android convention) that contains the tokens for each one of the resources
included. For example, for the above Resources layout, this is what the Resource class would expose:

public class Resource {
public class Drawable {
public const int icon = 0x123;
}

public class Layout {
public const int main = 0x456;
}

public class Strings {
public const int first_string = 0xabc;
public const int second_string = 0xbcd;
}
}

You would then use Resource.Drawable.icon to reference the drawable/icon.png file, or
Resource.Layout.main to reference the layout/main.xml file, or Resource.Strings.first_string
to reference the first string in the dictionary file values/strings.xml.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />

</com.google.android.material.appbar.AppBarLayout>

<include layout="@layout/content_main" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="@dimen/fab_margin"
android:layout_marginBottom="16dp"
app:srcCompat="@android:drawable/ic_dialog_email" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<fragment
android:id="@+id/nav_host_fragment_content_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<Button
android:id="@+id/button_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next"
app:layout_constraintBottom_toTopOf="@id/layout_first"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/button_first"
android:orientation="vertical"
android:id="@+id/layout_first"
android:padding="8dp">

<Button
android:id="@+id/button_animate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Android button above .NET MAUI controls" />

<!-- .NET MAUI content will go here. -->

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>
Loading

0 comments on commit e203d28

Please sign in to comment.