002.001 C#也能开发Android应用

C#语言入门详解(002.001)——C#也能开发Android应用.mp4 (29.07MB)

准备工作

打开 Visual Studio Installer 安装移动开发套件使用.net的移动开发。
image.png
image.png
安装好套件后打开 VS 的 Tools - Android SDK Manager 确认 SDK Platform 和 Emulator 安装成功
(第一次使用时要进入Android Device Manager设置机型初始化安卓Emulator)
image.png


体验 Android 模拟器

Android SDK 更新完毕后,可以在 Tools - Android SDK Manager - Android Device Manager 直接体验 Android 模拟器(一瞥 8.1)。
image.png

补充: 建议选用X86_64 核心减少调试卡顿
image.png


开发一个简单的计算器 App

开发安卓应用可以通过创建移动端跨平台项目,也可以通过创建单纯的安卓项目。考虑到 IOS 和 UWP 项目较难调试和发布视频里就只创建了 Android XAML App(Xamarin.Forms) 项目。

下面开发一个简单的加法应用。

  • VS2019新建项目(与视频内VS2017界面略微不同)image.png

    1. ![image.png](https://cdn.nlark.com/yuque/0/2019/png/302433/1559189941483-fe18e31f-68c8-478c-a0ab-6a957135bdb8.png#align=left&display=inline&height=359&name=image.png&originHeight=594&originWidth=1100&size=41795&status=done&width=665)

事例代码:

前端 XAML:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:local="clr-namespace:HelloMobile.Client"
  5. x:Class="HelloMobile.Client.MainPage">
  6. <Grid>
  7. <Grid.RowDefinitions>
  8. <RowDefinition Height="Auto"/>
  9. <RowDefinition Height="Auto"/>
  10. <RowDefinition Height="Auto"/>
  11. <RowDefinition Height="Auto"/>
  12. </Grid.RowDefinitions>
  13. <Editor x:Name="Tb1" Grid.Row="0"/>
  14. <Editor x:Name="Tb2" Grid.Row="1"/>
  15. <Editor x:Name="Tb3" Grid.Row="2"/>
  16. <Grid Grid.Row="3">
  17. <Grid.ColumnDefinitions>
  18. <ColumnDefinition Width="*"/>
  19. <ColumnDefinition Width="*"/>
  20. <ColumnDefinition Width="*"/>
  21. <ColumnDefinition Width="*"/>
  22. </Grid.ColumnDefinitions>
  23. <Button x:Name="Btn1" Grid.Column="0" Text="Add" Clicked="Doadd"/>
  24. <Button x:Name="Btn2" Grid.Column="1" Text="Sub" Clicked="Dosub"/>
  25. <Button x:Name="Btn3" Grid.Column="2" Text="Mul" Clicked="Domul"/>
  26. <Button x:Name="Btn4" Grid.Column="3" Text="Div" Clicked="Dodiv"/>
  27. </Grid>
  28. </Grid>
  29. </ContentPage>

Ctrl+E+V将当前语句复制到下一行(每次都得按先ctrl再e 然后v的顺序按)
**
后端 C#:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Xamarin.Forms;
  8. namespace HelloMobile.Client
  9. {
  10. // Learn more about making custom code visible in the Xamarin.Forms previewer
  11. // by visiting https://aka.ms/xamarinforms-previewer
  12. [DesignTimeVisible(true)]
  13. public partial class MainPage : ContentPage
  14. {
  15. public MainPage()
  16. {
  17. InitializeComponent();
  18. }
  19. private void Doadd(object sender, EventArgs e)
  20. {
  21. double.TryParse(Tb1.Text, out double x);//TryParse从TB1.Text从字符串转换为双精度浮点型,赋值给x
  22. double.TryParse(Tb2.Text, out double y);
  23. var z = x + y;
  24. Tb3.Text = z.ToString();
  25. }
  26. private void Dosub(object sender, EventArgs e)
  27. {
  28. double.TryParse(Tb1.Text, out double x);
  29. double.TryParse(Tb2.Text, out double y);
  30. var z = x - y;
  31. Tb3.Text = z.ToString();
  32. }
  33. private void Domul(object sender, EventArgs e)
  34. {
  35. double.TryParse(Tb1.Text, out double x);
  36. double.TryParse(Tb2.Text, out double y);
  37. var z = x * y;
  38. Tb3.Text = z.ToString();
  39. }
  40. private void Dodiv(object sender, EventArgs e)
  41. {
  42. double.TryParse(Tb1.Text, out double x);
  43. double.TryParse(Tb2.Text, out double y);
  44. var z = x / y;
  45. Tb3.Text = z.ToString();
  46. }
  47. }
  48. }

注:最好先 Build生成项目,再进行调试
image.png

源码工程文件:HelloMobile.zip


打包成 APK

参考 SOFMSDN

  1. In your toolbar change the project from debug mode to release mode

    1. ![image.png](https://cdn.nlark.com/yuque/0/2019/png/302433/1559199647634-c2cc84ab-b710-4bf2-8c49-cb2ca01a0093.png#align=left&display=inline&height=118&name=image.png&originHeight=190&originWidth=245&size=10565&status=done&width=152)
  2. Right-click on your Project and Select Archive(存档)…

image.png右键image.png

  1. Click on the generated archive and below on the Right side you will find two options Open folder and Distribute. (Select Distribute)

image.png

  1. Then on the Pop-up that appears Select AD-HOC

image.png

  1. Click on the Green plus icon to add signing identity where you need to provide the identity of the signing person or company

image.png
image.png

  1. After creating the signing identity click on that identity to select it and then click on save as to save your APK.

image.png

  1. A pop-up will appear asking password for the signing identity which you will produce in step 5.

image.png(输入上一步自己设置的密码)

打包成 APK,安装到手机上的效果:
image.png

打包好的apk文件:jisuanqi.zip


其它问题

Android 设计器不工作(VS2019实操未遇到纯备档)

再 XAML 切换到设计器时提示:System.ComponentModel.Composition.ImportCardinalityMismatchException: No exports were found that match the constraint: ...

解决方法:

  1. 保证 SDK 都更新到了最新版
  2. 清除 C:\Users\UserName\AppData\Local\Microsoft\VisualStudio\15.0*\ComponentModelCache 里面的缓存文件

推荐扩展读物

深入浅出WPF完整版.pdf
WPF语法与Xamarin有很大一致性,该书也是刘老师所写推荐阅读