質問編集履歴
2
詳細の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -31,4 +31,133 @@
|
|
31
31
|
|
32
32
|
~~~~
|
33
33
|
|
34
|
-
FormsからDependencyServiceを使い、Android側でXamnarin.Androidライブラリを使うのであれば動くとは思うのですが、.NET Standard libraryから利用する、または、.NET Standard library内で完結するような作りというのは無理でしょうか。
|
34
|
+
FormsからDependencyServiceを使い、Android側でXamnarin.Androidライブラリを使うのであれば動くとは思うのですが、.NET Standard libraryから利用する、または、.NET Standard library内で完結するような作りというのは無理でしょうか。
|
35
|
+
|
36
|
+
---
|
37
|
+
|
38
|
+
## 追記 20190301
|
39
|
+
|
40
|
+
* Xamarin.Fomrsプロジェクトを作成
|
41
|
+
* Cross-Platform .NET Standard Plugin Templates を追加
|
42
|
+
* Xamarin.Formsプロジェクトからライブラリプロジェクトを参照
|
43
|
+
* Xamarin.Androidプロジェクトからライブラリプロジェクトを参照
|
44
|
+
* Xamarin.iOSプロジェクトはアンロード
|
45
|
+
* CrossTestPlugin.shared.csにてTestPluginImplementationをインスタンス化しようとした時にNotImplementedExceptionが吐かれる
|
46
|
+
|
47
|
+
```
|
48
|
+
CrossTestPlugin.shared.cs
|
49
|
+
|
50
|
+
using System;
|
51
|
+
using Plugin.TestPlugin;
|
52
|
+
|
53
|
+
namespace Plugin.TestPlugin
|
54
|
+
{
|
55
|
+
/// <summary>
|
56
|
+
/// Cross TestPlugin
|
57
|
+
/// </summary>
|
58
|
+
public static class CrossTestPlugin
|
59
|
+
{
|
60
|
+
static Lazy<ITestPlugin> implementation = new Lazy<ITestPlugin>(() => CreateTestPlugin(), System.Threading.LazyThreadSafetyMode.PublicationOnly);
|
61
|
+
|
62
|
+
/// <summary>
|
63
|
+
/// Gets if the plugin is supported on the current platform.
|
64
|
+
/// </summary>
|
65
|
+
public static bool IsSupported => implementation.Value == null ? false : true;
|
66
|
+
|
67
|
+
/// <summary>
|
68
|
+
/// Current plugin implementation to use
|
69
|
+
/// </summary>
|
70
|
+
public static ITestPlugin Current
|
71
|
+
{
|
72
|
+
get
|
73
|
+
{
|
74
|
+
ITestPlugin ret = implementation.Value;
|
75
|
+
if (ret == null)
|
76
|
+
{
|
77
|
+
throw NotImplementedInReferenceAssembly();
|
78
|
+
}
|
79
|
+
return ret;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
static ITestPlugin CreateTestPlugin()
|
84
|
+
{
|
85
|
+
#if NETSTANDARD1_0 || NETSTANDARD2_0
|
86
|
+
return null;
|
87
|
+
#else
|
88
|
+
#pragma warning disable IDE0022 // Use expression body for methods
|
89
|
+
return new TestPluginImplementation(); // ここでNotImplementedException が出る
|
90
|
+
#pragma warning restore IDE0022 // Use expression body for methods
|
91
|
+
#endif
|
92
|
+
}
|
93
|
+
|
94
|
+
internal static Exception NotImplementedInReferenceAssembly() =>
|
95
|
+
new NotImplementedException("This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.");
|
96
|
+
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
```
|
101
|
+
|
102
|
+
```
|
103
|
+
ITestPlugin.shared.cs
|
104
|
+
|
105
|
+
using System;
|
106
|
+
using System.Collections.Generic;
|
107
|
+
using System.Text;
|
108
|
+
|
109
|
+
namespace Plugin.TestPlugin
|
110
|
+
{
|
111
|
+
public interface ITestPlugin
|
112
|
+
{
|
113
|
+
string GetVersionName();
|
114
|
+
}
|
115
|
+
}
|
116
|
+
```
|
117
|
+
|
118
|
+
```
|
119
|
+
TestPlugin.android.cs
|
120
|
+
|
121
|
+
using Android.Content;
|
122
|
+
using System;
|
123
|
+
using System.Collections.Generic;
|
124
|
+
using System.Text;
|
125
|
+
|
126
|
+
namespace Plugin.TestPlugin
|
127
|
+
{
|
128
|
+
/// <summary>
|
129
|
+
/// Interface for TestPlugin
|
130
|
+
/// </summary>
|
131
|
+
public class TestPluginImplementation : ITestPlugin
|
132
|
+
{
|
133
|
+
public string GetVersionName()
|
134
|
+
{
|
135
|
+
Context context = Android.App.Application.Context; //Android.App.Application.Context;
|
136
|
+
var name = context.PackageManager.GetPackageInfo(context.PackageName, 0).VersionName;
|
137
|
+
return name;
|
138
|
+
}
|
139
|
+
}
|
140
|
+
}
|
141
|
+
```
|
142
|
+
|
143
|
+
```
|
144
|
+
TestPlugin.apple.cs
|
145
|
+
|
146
|
+
using System;
|
147
|
+
using System.Collections.Generic;
|
148
|
+
using System.Text;
|
149
|
+
|
150
|
+
namespace Plugin.TestPlugin
|
151
|
+
{
|
152
|
+
/// <summary>
|
153
|
+
/// Interface for TestPlugin
|
154
|
+
/// </summary>
|
155
|
+
public class TestPluginImplementation : ITestPlugin
|
156
|
+
{
|
157
|
+
public string GetVersionName()
|
158
|
+
{
|
159
|
+
return "ios";
|
160
|
+
}
|
161
|
+
}
|
162
|
+
}
|
163
|
+
```
|
1
title
CHANGED
File without changes
|
body
CHANGED
@@ -25,4 +25,10 @@
|
|
25
25
|
|
26
26
|
---
|
27
27
|
|
28
|
+
環境
|
29
|
+
win10 VS2017
|
30
|
+
Mac VS for Mac
|
31
|
+
|
32
|
+
~~~~
|
33
|
+
|
28
34
|
FormsからDependencyServiceを使い、Android側でXamnarin.Androidライブラリを使うのであれば動くとは思うのですが、.NET Standard libraryから利用する、または、.NET Standard library内で完結するような作りというのは無理でしょうか。
|