Skip to content

Commit

Permalink
Merge pull request #91 from egvijayanand/working
Browse files Browse the repository at this point in the history
.NET MAUI 9 Preview 7 Embedded samples for `Android` and `iOS`
  • Loading branch information
egvijayanand authored Aug 26, 2024
2 parents 184c909 + 6710129 commit f6b26b9
Show file tree
Hide file tree
Showing 60 changed files with 697 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/NET_9/EmbeddedAndroid/AndroidManifest.xml
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>
38 changes: 38 additions & 0 deletions src/NET_9/EmbeddedAndroid/EmbeddedAndroid.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0-android</TargetFramework>
<OutputType>Exe</OutputType>

<!-- Project Options -->
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>EmbeddedAndroid</RootNamespace>

<!-- .NET MAUI -->
<UseMaui>true</UseMaui>
<MauiEnablePlatformUsings>true</MauiEnablePlatformUsings>

<!-- App Identifier -->
<ApplicationId>com.companyname.embeddedandroid</ApplicationId>

<!-- App Version -->
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>

<!-- Target Platform Options -->
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
</PropertyGroup>

<!--
Enable full trimming in Release mode.
To learn more, see: https://learn.microsoft.com/dotnet/core/deploying/trimming/trimming-options#trimming-granularity
-->
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<TrimMode>full</TrimMode>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
</ItemGroup>
</Project>
24 changes: 24 additions & 0 deletions src/NET_9/EmbeddedAndroid/EmbeddedAndroid.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmbeddedAndroid", "EmbeddedAndroid.csproj", "{6080BD5C-A536-4175-A64D-78FFC4642A41}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6080BD5C-A536-4175-A64D-78FFC4642A41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6080BD5C-A536-4175-A64D-78FFC4642A41}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6080BD5C-A536-4175-A64D-78FFC4642A41}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{6080BD5C-A536-4175-A64D-78FFC4642A41}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6080BD5C-A536-4175-A64D-78FFC4642A41}.Release|Any CPU.Build.0 = Release|Any CPU
{6080BD5C-A536-4175-A64D-78FFC4642A41}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions src/NET_9/EmbeddedAndroid/EmbeddedAndroid.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Solution>
<Project Path="EmbeddedAndroid.csproj">
<Deploy />
</Project>
<Properties Name="Visual Studio">
<!-- Support is provisionally available on Visual Studio 17.10 and later versions. -->
<Property Name="OpenWith" Value="Visual Studio Version 17" />
</Properties>
</Solution>
31 changes: 31 additions & 0 deletions src/NET_9/EmbeddedAndroid/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Note: There's a change in namespace.
using Microsoft.Maui.Controls.Embedding;
//using Microsoft.Maui.Embedding;
using Microsoft.Maui.Platform;

namespace EmbeddedAndroid
{
[Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);

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

var mauiApp = MauiApp.CreateBuilder()
.UseMauiEmbeddedApp<MyApp>() // Note: The method name too got updated.
.Build();

var mauiContext = new MauiContext(mauiApp.Services, this);

var mauiPage = new MauiPage();
// Platform-specific extension method
SetContentView(mauiPage.ToContainerView(mauiContext));
// Platform-neutral extension method
//SetContentView(mauiPage.ToPlatform(mauiContext));
}
}
}
17 changes: 17 additions & 0 deletions src/NET_9/EmbeddedAndroid/MauiPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
x:Class="EmbeddedAndroid.MauiPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://schemas.microsoft.com/dotnet/2021/maui/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<ContentPage.Content>
<Grid>
<Label
HorizontalOptions="Center"
Text=".NET MAUI Embedding is uber cool!!!"
VerticalOptions="Center" />
</Grid>
</ContentPage.Content>
</ContentPage>
6 changes: 6 additions & 0 deletions src/NET_9/EmbeddedAndroid/MauiPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace EmbeddedAndroid;

public partial class MauiPage : ContentPage
{
public MauiPage() => InitializeComponent();
}
17 changes: 17 additions & 0 deletions src/NET_9/EmbeddedAndroid/MyApp.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Application
x:Class="EmbeddedAndroid.MyApp"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://schemas.microsoft.com/dotnet/2021/maui/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Application.Resources>
<Color x:Key="Primary">#512BD4</Color>

<Style TargetType="Label">
<Setter Property="FontSize" Value="20" />
<Setter Property="TextColor" Value="{StaticResource Primary}" />
</Style>
</Application.Resources>
</Application>
6 changes: 6 additions & 0 deletions src/NET_9/EmbeddedAndroid/MyApp.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace EmbeddedAndroid;

public partial class MyApp : Microsoft.Maui.Controls.Application
{
public MyApp() => InitializeComponent();
}
44 changes: 44 additions & 0 deletions src/NET_9/EmbeddedAndroid/Resources/AboutResources.txt
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.
13 changes: 13 additions & 0 deletions src/NET_9/EmbeddedAndroid/Resources/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/app_text"
/>
</RelativeLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@mipmap/appicon_background" />
<foreground android:drawable="@mipmap/appicon_foreground" />
</adaptive-icon>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@mipmap/appicon_background" />
<foreground android:drawable="@mipmap/appicon_foreground" />
</adaptive-icon>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ic_launcher_background">#2C3E50</color>
</resources>
4 changes: 4 additions & 0 deletions src/NET_9/EmbeddedAndroid/Resources/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<string name="app_name">EmbeddedAndroid</string>
<string name="app_text">Hello, Android!</string>
</resources>
41 changes: 41 additions & 0 deletions src/NET_9/EmbeddediOS/AppDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Note: There's a change in namespace.
using Microsoft.Maui.Controls.Embedding;
//using Microsoft.Maui.Embedding;
using Microsoft.Maui.Platform;

namespace EmbeddediOS;

[Register(nameof(AppDelegate))]
public class AppDelegate : UIApplicationDelegate
{
public override UIWindow? Window { get; set; }

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
var mauiApp = MauiApp.CreateBuilder()
.UseMauiEmbeddedApp<MyApp>() // Note: The method name too got updated.
.Build();

var mauiContext = new MauiContext(mauiApp.Services);
var mauiPage = new MauiPage();
// Platform-specific extension method
var viewController = mauiPage.ToUIViewController(mauiContext);

// Create a new window instance based on the screen size
Window = new UIWindow(UIScreen.MainScreen.Bounds)
{
RootViewController = viewController
};

// Here MauiView is a type defined using .NET MAUI construct
// Platform-neutral Windowless API
//var nativeView = new MauiView().ToPlatform(mauiContext);
// Window-aware API - platformWindow can be passed as a method parameter
//var nativeView = new MauiView().ToPlatformEmbedded(mauiContext);

// Make the window visible
Window.MakeKeyAndVisible();

return true;
}
}
Loading

0 comments on commit f6b26b9

Please sign in to comment.