committed by
GitHub
14 changed files with 80 additions and 74 deletions
@ -1,35 +1,55 @@ |
|||||
using Android.App; |
using Android.App; |
||||
using Android.OS; |
using Android.OS; |
||||
using Android.Views; |
using Android.Views; |
||||
|
using Android.Content.PM; |
||||
|
using AndroidX.AppCompat.App; |
||||
|
using Android.Content.Res; |
||||
|
using AndroidX.Lifecycle; |
||||
|
|
||||
namespace Avalonia.Android |
namespace Avalonia.Android |
||||
{ |
{ |
||||
public abstract class AvaloniaActivity : Activity |
public abstract class AvaloniaActivity : AppCompatActivity |
||||
{ |
{ |
||||
internal AvaloniaView View; |
internal AvaloniaView View; |
||||
object _content; |
internal AvaloniaViewModel _viewModel; |
||||
|
|
||||
protected override void OnCreate(Bundle savedInstanceState) |
protected override void OnCreate(Bundle savedInstanceState) |
||||
{ |
{ |
||||
View = new AvaloniaView(this); |
View = new AvaloniaView(this); |
||||
if (_content != null) |
|
||||
View.Content = _content; |
|
||||
SetContentView(View); |
SetContentView(View); |
||||
|
|
||||
|
_viewModel = new ViewModelProvider(this).Get(Java.Lang.Class.FromType(typeof(AvaloniaViewModel))) as AvaloniaViewModel; |
||||
|
|
||||
|
if (_viewModel.Content != null) |
||||
|
{ |
||||
|
View.Content = _viewModel.Content; |
||||
|
} |
||||
|
|
||||
base.OnCreate(savedInstanceState); |
base.OnCreate(savedInstanceState); |
||||
} |
} |
||||
|
|
||||
public object Content |
public object Content |
||||
{ |
{ |
||||
get |
get |
||||
{ |
{ |
||||
return _content; |
return _viewModel.Content; |
||||
} |
} |
||||
set |
set |
||||
{ |
{ |
||||
_content = value; |
_viewModel.Content = value; |
||||
if (View != null) |
if (View != null) |
||||
View.Content = value; |
View.Content = value; |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
|
public override void OnConfigurationChanged(Configuration newConfig) |
||||
|
{ |
||||
|
base.OnConfigurationChanged(newConfig); |
||||
|
} |
||||
|
|
||||
|
protected override void OnDestroy() |
||||
|
{ |
||||
|
View.Content = null; |
||||
|
|
||||
|
base.OnDestroy(); |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,11 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Avalonia.Android |
||||
|
{ |
||||
|
internal class AvaloniaViewModel : AndroidX.Lifecycle.ViewModel |
||||
|
{ |
||||
|
public object Content { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -1,50 +0,0 @@ |
|||||
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-hdpi/ |
|
||||
icon.png |
|
||||
|
|
||||
drawable-ldpi/ |
|
||||
icon.png |
|
||||
|
|
||||
drawable-mdpi/ |
|
||||
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" 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 R.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. |
|
||||
@ -1,6 +0,0 @@ |
|||||
<?xml version="1.0" encoding="utf-8"?> |
|
||||
|
|
||||
<resources> |
|
||||
<string name="Hello">Hello World, Click Me!</string> |
|
||||
<string name="ApplicationName">$projectname$</string> |
|
||||
</resources> |
|
||||
Loading…
Reference in new issue