Xamarin Android bound to WeChat SDK, xamarinandroid

Source: Internet
Author: User

Xamarin Android binding SDK, xamarinandroid

Almost all apps are now integrated with Weibo and other functions shared by social platforms. These social platforms also provide sdks for developers. for Android and IOS platforms, you only need to download official sdks and integrate them according to official instructions.

But for Xamarin, there is no official SDK. In this case, we need to manually bind the SDK, starting from the Android layer, let's implement how to integrate the SDK into your own APP to implement the sharing Function step by step.

1. In the open platform official website https://open.weixin.qq.com/apply for a developer account, fill in the company APP information, pass the review. The Applied APPID is used in the project. Then download the official Android_SDK

2. Create an Android binding Library Project

3. Place the downloaded Jar package from the official SDK In the Jars folder of the project.

Remember to change the attribute of the Jar package to an embedded Jar package. Otherwise, an error will be reported during compilation in the real project.

4. at this time, an error will be reported during compilation, but do not be afraid to locate the error. It turns out that the compiler has duplicate names when converting the Jar package to C # code, this class has two errcodes. This may be because the original Java code attribute name is errCode. When it is converted to C # code, the compiler automatically converts the first letter to uppercase, so it conflicts with the class ErrCode.

 

5. You can rename the project file Metadata. xml. This file is the configuration file when the Jar package is converted to C # code. You can perform operations such as removing classes, removing methods, and modifying field names, the specific functions are not detailed here. If you are interested, you can study the official documents of Xamarin.

6. Add the following code. During the conversion, the name will be changed to the name we specified.

<metadata><!--This sample removes the class: android.support.v4.content.AsyncTaskLoader.LoadTask:<remove-node path="/api/package[@name='android.support.v4.content']/class[@name='AsyncTaskLoader.LoadTask']" />This sample removes the method: android.support.v4.content.CursorLoader.loadInBackground:<remove-node path="/api/package[@name='android.support.v4.content']/class[@name='CursorLoader']/method[@name='loadInBackground']" />--><attr path="/api/package[@name='com.tencent.mm.opensdk.modelmsg']/class[@name='WXMediaMessage']/field[@name='mediaObject']"      name="managedName">MyMediaObject</attr><attr path="/api/package[@name='com.tencent.mm.opensdk.modelbase']/class[@name='BaseResp']/field[@name='errCode']"      name="managedName">MyErrCode</attr></metadata>

Compile it again. OK has successfully generated the Android binding library. Next, create an Android project and try it out.

7. Add and reference the Android-bound library just now, and then modify the MainActivity. cs code.

 

Using Android. app; using Android. widget; using Android. OS; using Com. tencent. MM. opensdk. openapi; using Com. tencent. MM. opensdk. modelbase; using Com. tencent. MM. opensdk. modelmsg; using System; using Android. graphics; using System. IO; namespace WeChat. android. samples {[Activity (Label = "WeChat. android. samples ", MainLauncher = true, Icon =" @ drawable/icon ")] public class MainActivity: Activity, IWXAPIEventHandler {// IWXAPI is a third-party app and communication openapi interface private IWXAPI; // replace APP_ID with the valid appId public const string APP_ID = "wxd930ea5d5a258f4f" applied by your application from the official website; // The minimum supported version private const int TIMELINE_SUPPORTED_VERSION = 0x21020001; public void OnReq (BaseReq p0) {throw new NotImplementedException ();} public void OnResp (BaseResp p0) {throw new handle ();} protected override void OnCreate (Bundle bundle) {base. onCreate (bundle); // Set our view from the "main" layout resource SetContentView (Resource. layout. main); // obtain the IWXAPI instance api = wxapifacloud through the wxapifacloud factory. createWXAPI (this, APP_ID, false); Button btnRegister = FindViewById <Button> (Resource. id. btnRegister); btnRegister. click + = BtnRegister_Click; Button btnText = FindViewById <Button> (Resource. id. btnText); btnText. click + = BtnText_Click; Button btnHtml = FindViewById <Button> (Resource. id. btnHtml); btnHtml. click + = BtnHtml_Click; Button btnOpenWeChat = FindViewById <Button> (Resource. id. btnOpenWeChat); btnOpenWeChat. click + = BtnOpenWeChat_Click; Button btnIsMoments = FindViewById <Button> (Resource. id. btnIsMoments); btnIsMoments. click + = BtnIsMoments_Click;} // whether the private void BtnIsMoments_Click (object sender, EventArgs e) {int wxSdkVersion = api is supported. WXAppSupportAPI; if (wxSdkVersion> = TIMELINE_SUPPORTED_VERSION) {Toast. makeText (this, "wxSdkVersion =" + wxSdkVersion + "\ n support", ToastLength. long ). show ();} else {Toast. makeText (this, "wxSdkVersion =" + wxSdkVersion + "\ n not supported", ToastLength. long ). show () ;}// open APP private void BtnOpenWeChat_Click (object sender, EventArgs e) {Toast. makeText (this, "launch result =" + api. openWXApp (), ToastLength. long ). show () ;}// share private void BtnHtml_Click (object sender, EventArgs e) {WXWebpageObject webObj = new WXWebpageObject (); webObj. webpageUrl = "https://www.xamarin.com/"; WXMediaMessage msg = new WXMediaMessage (webObj); msg. title = "Xamarin Official Website"; msg. description = "Official Website Description"; // share the thumbnail Bitmap thumb = BitmapFactory. decodeResource (this. resources, Resource. drawable. icon); MemoryStream MS = new MemoryStream (); thumb. compress (Bitmap. compressFormat. png, 0, ms); byte [] bytes = ms. toArray (); // construct a Req request SendMessageToWX. req req = new SendMessageToWX. req (); // unique request flag req. transaction = System. guid. newGuid (). toString (); req. message = msg; req. scene = SendMessageToWX. req. WXSceneTimeline; // send data api. sendReq (req);} // share the text type private void BtnText_Click (object sender, EventArgs e) {// initialize a WXTextObject object, enter the shared text content WXTextObject textObj = new WXTextObject (); textObj. text = "Hello Xamarin"; // use the WXTextObject object to initialize a WXMediaMessage object WXMediaMessage msg = new WXMediaMessage (); msg. myMediaObject = textObj; msg. description = "Hello World"; // construct a Req request SendMessageToWX. req req = new SendMessageToWX. req (); // unique request flag req. transaction = System. guid. newGuid (). toString (); req. message = msg; req. scene = SendMessageToWX. req. WXSceneSession; // send data api. sendReq (req);} // register the app to private void BtnRegister_Click (object sender, EventArgs e) {var result = api. registerApp (APP_ID); Toast. makeText (this, "registration result:" + result, ToastLength. short ). show ();}}}

Main. axml

<? 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"> <Button android: id = "@ + id/btnRegister" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "register an app"/> <Button android: id = "@ + id/btnText" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "send text"/> <Button android: id = "@ + id/btnHtml" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "Send webpage"/> <Button android: id = "@ + id/btnOpenWeChat" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "open"/> <Button android: id = "@ + id/btnIsMoments" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "circle of friends supported"/> </LinearLayout>

8. The work has been completed till now. Now let's debug it. If there is a crash during the sharing, you only need to change the APP_ID to the one you registered on the open platform.

The final attachment Github Source Code address: https://github.com/vilyo/WeChatDemo

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.