Skip to content

Commit

Permalink
Add quick and dirty WcfSample.Android application.
Browse files Browse the repository at this point in the history
We have to wait for dotnet/sdk#491 (as per http://www.natemcmaster.com/blog/2017/03/09/vs2015-to-vs2017-upgrade/) until we get better support for Xamarin.Android projects in dotnet/sdk. For now the project is excluded from the build. Enthusiasts may reactivate it in VS - Build - Configuration Manager.
  • Loading branch information
azabluda committed Oct 20, 2017
1 parent 0afeb08 commit 7704f85
Show file tree
Hide file tree
Showing 16 changed files with 591 additions and 3 deletions.
11 changes: 8 additions & 3 deletions InfoCarrier.Core.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.16
VisualStudioVersion = 15.0.27004.2005
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InfoCarrier.Core", "src\InfoCarrier.Core\InfoCarrier.Core.csproj", "{6B24F793-C93B-4869-A99B-AC51EE2C82AF}"
EndProject
Expand All @@ -15,9 +15,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{4A57BB
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WcfSample.Shared", "sample\WcfSample.Shared\WcfSample.Shared.csproj", "{4AF12B6E-9979-4146-86BC-F59F6D00E4A0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WcfSample.Server", "sample\WcfSample.Server\WcfSample.Server.csproj", "{C43F891C-7548-4E22-B0C6-A5E931BEC278}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WcfSample.Server", "sample\WcfSample.Server\WcfSample.Server.csproj", "{C43F891C-7548-4E22-B0C6-A5E931BEC278}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WcfSample.Client", "sample\WcfSample.Client\WcfSample.Client.csproj", "{27B10A37-901E-4A3E-A2E3-4B48C62F0EB9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WcfSample.Client", "sample\WcfSample.Client\WcfSample.Client.csproj", "{27B10A37-901E-4A3E-A2E3-4B48C62F0EB9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WcfSample.Android", "sample\WcfSample.Android\WcfSample.Android.csproj", "{A26F1176-705E-490A-ADE3-F8B12DE1D970}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -45,6 +47,8 @@ Global
{27B10A37-901E-4A3E-A2E3-4B48C62F0EB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{27B10A37-901E-4A3E-A2E3-4B48C62F0EB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{27B10A37-901E-4A3E-A2E3-4B48C62F0EB9}.Release|Any CPU.Build.0 = Release|Any CPU
{A26F1176-705E-490A-ADE3-F8B12DE1D970}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A26F1176-705E-490A-ADE3-F8B12DE1D970}.Release|Any CPU.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -55,6 +59,7 @@ Global
{4AF12B6E-9979-4146-86BC-F59F6D00E4A0} = {4A57BB06-6B50-4CF8-A601-E5572247FB5F}
{C43F891C-7548-4E22-B0C6-A5E931BEC278} = {4A57BB06-6B50-4CF8-A601-E5572247FB5F}
{27B10A37-901E-4A3E-A2E3-4B48C62F0EB9} = {4A57BB06-6B50-4CF8-A601-E5572247FB5F}
{A26F1176-705E-490A-ADE3-F8B12DE1D970} = {4A57BB06-6B50-4CF8-A601-E5572247FB5F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {ADB840D9-C641-42AF-907B-8FE9280E9A8D}
Expand Down
19 changes: 19 additions & 0 deletions sample/WcfSample.Android/Assets/AboutAssets.txt
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 you 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");
92 changes: 92 additions & 0 deletions sample/WcfSample.Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) on/off it-solutions gmbh. All rights reserved.
// Licensed under the MIT license. See license.txt file in the project root for license information.

namespace WcfSample
{
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using global::Android.App;
using global::Android.OS;
using global::Android.Widget;
using InfoCarrier.Core.Client;
using InfoCarrier.Core.Common;
using Microsoft.EntityFrameworkCore;

[Activity(Label = "WcfSample.Android", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

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

// Get our button from the layout resource,
// and attach an event to it
Button button = this.FindViewById<Button>(Resource.Id.myButton);

button.Click += this.Button_Click;
}

private async void Button_Click(object sender, EventArgs e)
{
var consoleTextView = this.FindViewById<TextView>(Resource.Id.textConsole);
void ConsoleWriteLine(string line)
{
consoleTextView.Text += $"{line}\n";
}

consoleTextView.Text = "";

try
{
string serverUrl = this.FindViewById<TextView>(Resource.Id.editServerUrl).Text;
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseInfoCarrierBackend(new WcfBackendXamarinImpl(serverUrl));
var options = optionsBuilder.Options;

// Select and update
using (var context = new BloggingContext(options))
{
Post myBlogPost = await (
from blog in context.Blogs
from post in blog.Posts
join owner in context.Users on blog.OwnerId equals owner.Id
where owner.Name == "hi-its-me"
where post.Title == "my-blog-post"
select post).SingleAsync();

ConsoleWriteLine($@"Blog post '{myBlogPost.Title}' is retrieved.");

myBlogPost.CreationDate = DateTime.Now;
await context.SaveChangesAsync();
ConsoleWriteLine($@"CreationDate is set to '{myBlogPost.CreationDate}'.");
}
}
catch (Exception exception)
{
ConsoleWriteLine(exception.ToString());
}
}

// Xamarin.Android implementation of asynchronous WCF method calls is broken in a very
// weird way (throws no exception but just never executes the continuation after await).
// We have to simulate asynchrony by calling synchronous methods on the thread pool.
private class WcfBackendXamarinImpl : WcfBackendImpl
{
public WcfBackendXamarinImpl(string serverUrl)
: base (serverUrl)
{
}

public override async Task<QueryDataResult> QueryDataAsync(QueryDataRequest request, DbContext dbContext, CancellationToken cancellationToken)
=> await Task.Run(() => this.QueryData(request, dbContext), cancellationToken);

public override async Task<SaveChangesResult> SaveChangesAsync(SaveChangesRequest request, CancellationToken cancellationToken)
=> await Task.Run(() => this.SaveChanges(request), cancellationToken);
}
}
}
9 changes: 9 additions & 0 deletions sample/WcfSample.Android/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?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="WcfSample.Android.WcfSample.Android">
<uses-sdk android:minSdkVersion="21" />
<application android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name">
</application>
</manifest>
31 changes: 31 additions & 0 deletions sample/WcfSample.Android/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) on/off it-solutions gmbh. All rights reserved.
// Licensed under the MIT license. See license.txt file in the project root for license information.

using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WcfSample.Android")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WcfSample.Android")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
44 changes: 44 additions & 0 deletions sample/WcfSample.Android/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.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.
118 changes: 118 additions & 0 deletions sample/WcfSample.Android/Resources/Resource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions sample/WcfSample.Android/Resources/layout/Main.axml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Server URL"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:text="10.0.2.2:8080"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editServerUrl" />
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/textConsole"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:enabled="false" />
</LinearLayout>
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.
5 changes: 5 additions & 0 deletions sample/WcfSample.Android/Resources/values/Strings.xml
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">WcfSample.Android</string>
</resources>
Loading

0 comments on commit 7704f85

Please sign in to comment.