如何使用Xamarin在Visual Studio中创建Android应用

论坛 期权论坛 脚本     
匿名技术用户   2020-12-21 18:02   76   0

Xamarin平台帮助.NET开发者使用C#开发各种平台的应用程序,包括Android,iOS,Mac和Windows。这里介绍下如何使用Xamarin搭建一个hello world应用。

参考原文:

Creating Android Apps with Xamarin in Visual Studio

Xamarin下载和安装

下载Xamarin

联网之后运行Xamarin installer,在这之前确保你已经安装了JDK,而且是32位的,不然会有错误提示。接下来所有的组件都会自动下载 (我这里需要播VPN):

默认情况下,Android SDK是安装在C:\Program Files (x86)\Android\android-sdk。使用SDK Manager.exe下载系统镜像,并用AVD Manager.exe创建虚拟机:

第一个C#开发的Android应用

运行Visual Studio 2013,并创建一个Android工程:

点击Main.axml看一下布局:

打开MainActivity.cs可以看到默认添加的button代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace HelloXamarin
{
[Activity(Label = "HelloXamarin", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };
}
}
}

好了,运行下试试:

发生错误:An error alert: Deployment failed due to an error in FastDev assembly synchronization!

修复的方法:打开Properties -> Android Options,取消Use Fast Development

重新编译之后就可以正常运行了:

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP