飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

Xamarin 第一个应用程序

时间:2021-12-20  作者:匿名  

在本章中,我们将看到如何使用 Xamarin 创建一个小型 Android 应用程序。

哈马林你好!应用

首先,启动一个新的 Visual Studio 实例,然后转到File → New → Project。

项目

在出现的菜单对话框中,转到模板 → Visual C# → Android → 空白应用程序 (Android)。

空白应用

为您的应用程序提供一个适当的名称。在我们的例子中,我们将其命名为“helloWorld”并将其保存在提供的默认位置。接下来,单击“确定”按钮以加载新的“helloXamarin”项目。

在解决方案上,打开资源 → 布局 → Main.axml文件。从设计视图切换到源文件并键入以下代码行以构建您的应用程序。

<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" 
   android:orientation = "vertical" 
   android:background = "#d3d3d3" 
   android:layout_width = "fill_parent" 
   android:layout_height = "fill_parent"> 
   <TextView 
      android:text = "@string/HelloXamarin" 
      android:textAppearance = "?android:attr/textAppearanceLarge" 
      android:layout_width = "match_parent" 
      android:layout_height = "wrap_content" 
      android:id = "@+id/textView2" 
      android:textColor = "@android:color/black" /> </LinearLayout>

在上面的代码中,我们创建了一个新的 Android textview。接下来,打开文件夹 values 并双击Strings.xml将其打开。在这里,我们将存储有关上面创建的按钮的信息和值。

<?xml version = "1.0" encoding = "utf-8"?> <resources> 
   <string name = "HelloXamarin">Hello World, I am Xamarin!</string> 
   <string name = "ApplicationName">helloWorld</string> </resources>

打开MainActivity.cs文件并将现有代码替换为以下代码行。

using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS;  namespace HelloXamarin { 
   public class MainActivity : Activity { 
      protected override void OnCreate(Bundle bundle) { 
         base.OnCreate(bundle); 
         SetContentView(Resource.Layout.Main); 
      } 
   } }

保存应用程序。构建并运行它以在 Android 模拟器中显示创建的应用程序。

安卓模拟器

如果您没有 Android 模拟器,请按照下一节中给出的步骤创建一个。

设置 Android 模拟器

在 Visual Studio 菜单上,转到Tools → Android → Android Emulator Manager。在出现的弹出窗口中,单击“创建”按钮。它将显示以下屏幕。

创建新的安卓虚拟设备

在上面的屏幕上,提供所需的AVD 名称。选择一个设备适合于您的显示,例如,的Nexus 4”显示。选择您的目标平台。始终建议在最低目标平台上进行测试,例如 API 10 Android 2.3 (Gingerbread),以确保您的应用程序可在所有 Android 平台上运行。

填写其余字段,然后单击“确定”按钮。您的模拟器现已准备就绪。您可以从现有 Android 虚拟设备列表中选择它,然后单击开始启动它。

模拟器

修改 HelloXamarin 应用程序

在本节中,我们将修改我们的项目并创建一个按钮,该按钮将在单击时显示文本。打开main.axml并切换到源视图。在我们创建的textview之后,我们将添加一个按钮,如下所示。

<Button 
   android:id = "@+id/MyButton" 
   android:layout_width = "fill_parent" 
   android:layout_height = "wrap_content" 
   android:text = "@string/ButtonClick" />

添加按钮后,我们的完整代码将如下所示 -

<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" 
   android:orientation = "vertical" 
   android:layout_width = "fill_parent" 
   android:layout_height = "fill_parent"> 
   <TextView 
      android:text = "@string/HelloXamarin" 
      android:textAppearance = "?android:attr/textAppearanceLarge" 
      android:layout_width = "match_parent" 
      android:layout_height = "wrap_content" 
      android:id = "@+id/textView2" /> 
    
   <Button 
      android:id = "@+id/MyButton" 
      android:layout_width = "fill_parent" 
      android:layout_height = "wrap_content" 
      android:text = "@string/ButtonClick" /> </LinearLayout>

接下来,我们在strings.xml文件中注册我们的按钮值。

<string name = "ButtonClick">Click Me!</string>

在strings.xml文件中添加我们的按钮后,我们将打开MainActivity.cs文件为我们的按钮添加一个点击时的动作,如下面的代码所示。

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 { 
      protected override void OnCreate(Bundle bundle) { 
         base.OnCreate(bundle); 
         SetContentView(Resource.Layout.Main); 
         Button button = FindViewById<Button>(Resource.Id.MyButton); 
         button.Click += delegate { button.Text = "Hello world I am your first App"; }; 
      } 
   } }

接下来,构建并运行您的应用程序。

运行应用程序

单击按钮后,您将获得以下输出 -

输出

湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。