Odd wicked craft skills -Flutter call C #

Foreword

As we all know, Xamarin should be a cross-platform development tools under .net. The state is still in charge until 2016, after Microsoft after the acquisition of open source. But it seems that there is a phenomenon, Xamarin development of the open source seems to be some stagnation, and the maintenance Xamarin team and very stubborn unwilling to work. Community repeatedly suggested that the UI layer should be unified graphics engine, rather than mapping the native controls. Xamarin.Forms gives the impression that poor performance, stiff animation, ineffective implementation. That Google's Flutter after hot, Xamarin community a lot of people will move on, you Baidu search Xamarin, the first keyword is xamarin Others use it . Then for C # /. Net developers, the only problem is the interface, there is the open source project Xamarin.Flutter , but not long after the New Year, the project Archived up, adamped amateur alone said he and a few developers time is difficult to get. But technically really feasible because Skia in .net already has binding SkiaSharp , and then use Skia Flutter underlying engine. UIWidgets is achieved Flutter under Unity3D . For that we have .net developer interface is not possible to use Flutter, business logic using C #, the answer is feasible, see below decomposition.

surroundings

In order to be able to use Flutter call C #, need the following tools and environments

Front four does not go into details, please refer to the official documentation to make sure Flutter, Xamarin can run the project, Android ndk use r15c version currently only supports r15c like, others not tested.

Embeddinator-4000 is converted to the C # platform native code each tool can be used to install Nuget

Install-Package Embeddinator-4000 -Version 0.4.0

Add the tool path to the system environment PATH, it proposed to add nuget of global cache, similarC:\Users\Administrator\.nuget\packages\embeddinator-4000\0.4.0\tools

If you use the wrong tool, check sdk, or ndk version and confirm Xamarin set sdk, ndk path is correct. And add the following information into the registry

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Novell]

[HKEY_CURRENT_USER\Software\Novell\Mono for Android]
"AndroidNdkDirectory"="C:\\Program Files (x86)\\Android\\android-sdk\\ndk-bundle"
"AndroidSdkDirectory"="C:\\Program Files (x86)\\Android\\android-sdk"
"JavaSdkDirectory"="C:\\Program Files\\Android\\jdk\\microsoft_dist_openjdk_1.8.0.25\\"`

Because the current registry information visualstudio are opposites file format, which may be BUG tool itself, participate https://github.com/mono/Embeddinator-4000/issues/707

Instructions

1. Create a C # Class Library (here only method to achieve under Android, iOS and so on)

Here we create a class library, we use the Android Class Lib, Android, if not the method, you can create a common library

After compiling get Test4Flutter.dll, run the following command

Embeddinator-4000.exe --gen=java -out=test .\Test4Flutter.dll -p=Android -c

MSBUILD appear: error MSB1008: You can specify only one item. Make sure there are no spaces under the path, or copy the dll to a different path retry

In the test directory we have a file Test4Flutter.aar

2. Create a Flutter plugin

Android was added Module, with particular reference to the document , add the Module into dependency.

![dump_2046261408567477536](C:\Users\Administrator\Desktop\dump_2046261408567477536.png)  public void onMethodCall(MethodCall call, Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    }
    else if (call.method.equals("getHelloString")){
      result.success(test.getHelloString());
    }
    else if(call.method.equals("add")){
      result.success(test.add((int)call.argument("x"),(int)call.argument("y")));
    }
    else {
      result.notImplemented();
    }
  }

  static Future<String> getHelloString() async {
    return await _channel.invokeMethod('getHelloString');
  }

  static Future<int> add(int x,int y) async{
    final int ret = await _channel.invokeMethod('add',<String, dynamic>{
      'x': x,
      'y': y,
    });
    return ret;
  }

Test calls within example

When calling a method console will print as follows

I/mono-stdout( 5946): call net function ->GetHelloString
I/mono-stdout( 5946): call net function ->Add
I/mono-stdout( 5946): call net function ->Add

Knot

Many times we should abandon the language of war, language is not good or bad, you can even fused

Guess you like

Origin www.cnblogs.com/LHCCC/p/11374204.html