forked from dotnet/maui-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dotnet#339 from dotnet/custom_renderer_multi
Added custom renderer sample
- Loading branch information
Showing
46 changed files
with
8,360 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
Upgrading/CustomRenderer/MultiProject/Entry/Android/Assets/AboutAssets.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Any raw assets you want to be deployed with your application can be placed in | ||
this directory (and child directories) and given a Build Action of "AndroidAsset". | ||
|
||
These files will be deployed with your package and will be accessible using Android's | ||
AssetManager, like this: | ||
|
||
public class ReadAsset : Activity | ||
{ | ||
protected override void OnCreate (Bundle bundle) | ||
{ | ||
base.OnCreate (bundle); | ||
|
||
InputStream input = Assets.Open ("my_asset.txt"); | ||
} | ||
} | ||
|
||
Additionally, some Android functions will automatically load asset files: | ||
|
||
Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf"); |
24 changes: 24 additions & 0 deletions
24
Upgrading/CustomRenderer/MultiProject/Entry/Android/CustomRenderer.Android.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<UseMaui>true</UseMaui> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<ConsolePause>false</ConsolePause> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<ConsolePause>false</ConsolePause> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0-android</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\CustomRenderer\CustomRenderer.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.4.410601"> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
24 changes: 24 additions & 0 deletions
24
Upgrading/CustomRenderer/MultiProject/Entry/Android/MainActivity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
|
||
using Android.App; | ||
using Android.Content; | ||
using Android.Runtime; | ||
using Android.Views; | ||
using Android.Widget; | ||
using Android.OS; | ||
using Android.Content.PM; | ||
using Microsoft.Maui; | ||
using Microsoft.Maui.Controls; | ||
|
||
|
||
namespace CustomRenderer.Android | ||
{ | ||
[Activity (Label = "CustomRenderer.Android.Android", Icon = "@drawable/icon", | ||
Theme = "@style/MainTheme", MainLauncher = true, | ||
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] | ||
public class MainActivity : MauiAppCompatActivity | ||
{ | ||
} | ||
|
||
} | ||
|
19 changes: 19 additions & 0 deletions
19
Upgrading/CustomRenderer/MultiProject/Entry/Android/MainApplication.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using Android.App; | ||
using Android.Runtime; | ||
using Microsoft.Maui; | ||
using Microsoft.Maui.Hosting; | ||
|
||
namespace CustomRenderer.Android | ||
{ | ||
[Application] | ||
public class MainApplication : MauiApplication | ||
{ | ||
public MainApplication(IntPtr handle, JniHandleOwnership ownership) | ||
: base(handle, ownership) | ||
{ | ||
} | ||
|
||
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
Upgrading/CustomRenderer/MultiProject/Entry/Android/MauiProgram.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Microsoft.Maui.Hosting; | ||
using Microsoft.Maui.Handlers; | ||
using Microsoft.Maui.Controls.Compatibility.Hosting; | ||
|
||
namespace CustomRenderer.Android; | ||
|
||
public static class MauiProgram | ||
{ | ||
public static MauiAppBuilder builder; | ||
public static MauiAppBuilder Builder | ||
{ | ||
get | ||
{ | ||
if (builder == null) | ||
{ | ||
builder = MauiApp.CreateBuilder(); | ||
} | ||
|
||
return builder; | ||
} | ||
} | ||
public static MauiApp CreateMauiApp() | ||
{ | ||
var builder = Builder; | ||
builder | ||
.UseMauiCompatibility() | ||
.UseMauiApp<App>(); | ||
|
||
builder.ConfigureMauiHandlers(handlers => { | ||
#if ANDROID | ||
handlers.AddCompatibilityRenderer(typeof(MyEntry), typeof(CustomRenderer.Android.MyEntryRenderer)); | ||
#endif | ||
}); | ||
|
||
return builder.Build(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Upgrading/CustomRenderer/MultiProject/Entry/Android/MyEntryRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using CustomRenderer; | ||
using CustomRenderer.Android; | ||
using Android.Content; | ||
using Microsoft.Maui; | ||
using Microsoft.Maui.Controls; | ||
using Microsoft.Maui.Controls.Compatibility.Platform.Android; | ||
using Microsoft.Maui.Controls.Platform; | ||
|
||
|
||
namespace CustomRenderer.Android | ||
{ | ||
class MyEntryRenderer : EntryRenderer | ||
{ | ||
public MyEntryRenderer(Context context) : base(context) | ||
{ | ||
} | ||
|
||
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) | ||
{ | ||
base.OnElementChanged(e); | ||
|
||
if (Control != null) | ||
{ | ||
Control.SetBackgroundColor(global::Android.Graphics.Color.LightGreen); | ||
} | ||
} | ||
} | ||
} | ||
|
6 changes: 6 additions & 0 deletions
6
Upgrading/CustomRenderer/MultiProject/Entry/Android/Properties/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" android:versionCode="1" android:versionName="1.0" package="CustomRenderer.Android"> | ||
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="29" /> | ||
<application android:label="CustomRenderer.Android"> | ||
</application> | ||
</manifest> |
44 changes: 44 additions & 0 deletions
44
Upgrading/CustomRenderer/MultiProject/Entry/Android/Resources/AboutResources.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.axml), | ||
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.axml | ||
|
||
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 "R" | ||
(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 R class would expose: | ||
|
||
public class R { | ||
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 R.drawable.icon to reference the drawable/icon.png file, or R.layout.main | ||
to reference the layout/main.axml file, or R.strings.first_string to reference the first | ||
string in the dictionary file values/strings.xml. |
Binary file added
BIN
+5.45 KB
Upgrading/CustomRenderer/MultiProject/Entry/Android/Resources/drawable/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions
14
Upgrading/CustomRenderer/MultiProject/Entry/Android/Resources/layout/Main.axml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="vertical" | ||
android:layout_width="fill_parent" | ||
android:layout_height="fill_parent" | ||
> | ||
<Button | ||
android:id="@+id/myButton" | ||
android:layout_width="fill_parent" | ||
android:layout_height="wrap_content" | ||
android:text="@string/hello" | ||
/> | ||
</LinearLayout> | ||
|
5 changes: 5 additions & 0 deletions
5
Upgrading/CustomRenderer/MultiProject/Entry/Android/Resources/values/Strings.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<string name="hello">Hello World, Click Me!</string> | ||
<string name="app_name">CustomRenderer.Android.Android</string> | ||
</resources> |
7 changes: 7 additions & 0 deletions
7
Upgrading/CustomRenderer/MultiProject/Entry/Android/Resources/values/colors.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<color name="launcher_background">#FFFFFF</color> | ||
<color name="colorPrimary">#3F51B5</color> | ||
<color name="colorPrimaryDark">#303F9F</color> | ||
<color name="colorAccent">#FF4081</color> | ||
</resources> |
27 changes: 27 additions & 0 deletions
27
Upgrading/CustomRenderer/MultiProject/Entry/Android/Resources/values/styles.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<style name="MainTheme" parent="MainTheme.Base"> | ||
</style> | ||
<!-- Base theme applied no matter what API --> | ||
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar"> | ||
<!--If you are using revision 22.1 please use just windowNoTitle. Without android:--> | ||
<item name="windowNoTitle">true</item> | ||
<!--We will be using the toolbar so no need to show ActionBar--> | ||
<item name="windowActionBar">false</item> | ||
<!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette --> | ||
<!-- colorPrimary is used for the default action bar background --> | ||
<item name="colorPrimary">#2196F3</item> | ||
<!-- colorPrimaryDark is used for the status bar --> | ||
<item name="colorPrimaryDark">#1976D2</item> | ||
<!-- colorAccent is used as the default value for colorControlActivated | ||
which is used to tint widgets --> | ||
<item name="colorAccent">#FF4081</item> | ||
<!-- You can also set colorControlNormal, colorControlActivated | ||
colorControlHighlight and colorSwitchThumbNormal. --> | ||
<item name="windowActionModeOverlay">true</item> | ||
<item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item> | ||
</style> | ||
<style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog"> | ||
<item name="colorAccent">#FF4081</item> | ||
</style> | ||
</resources> |
Oops, something went wrong.