生成第一个 Xamarin.Forms 应用

Windows 分步说明

Download Sample 下载示例

请按照以下步骤以及上面的视频操作:

  1. 选择“文件”>“新建”>“项目...”,或按“创建新项目...”按钮。

  2. 搜索“Xamarin”或从“项目类型”菜单中选择“移动”。 选择“移动应用(Xamarin.Forms)”项目类型。

  3. 选择项目名称 – 示例使用“AwesomeApp”。

  4. 单击“空白”项目类型,确保选择了“Android”和“iOS”:

    Android and iOS Blank App

  5. 等到 NuGet 包还原(状态栏中将出现“还原已完成”消息)。

  6. 新的 Visual Studio 2022 安装不会安装 Android SDK,系统可能会提示你安装最新的 Android SDK:

    Install Android SDK

  7. 新的 Visual Studio 2022 安装不会配置 Android 仿真器。 单击“调试”按钮上的下拉箭头,然后选择“创建 Android Emulator”以启动仿真器创建屏幕:

    Create Android Emulator dropdown

  8. 在仿真器创建屏幕中,使用默认设置并单击“创建”按钮:

    Android emulator creation screen

  9. 创建仿真器会使你返回“设备管理器”窗口。 单击“启动”按钮以启动新仿真器:

    Android emulator in the Device Manager

  10. Visual Studio 2022 现在应在“调试”按钮上显示新仿真器的名称:

    Android emulator name on the Debug button

  11. 单击“调试”按钮以生成应用程序并将其部署到 Android 仿真器:

    Android emulator displaying the application

自定义应用程序

可以自定义应用程序以添加交互功能。 执行以下步骤可向应用程序添加用户交互:

  1. 编辑 MainPage.xaml,在 </StackLayout> 结束之前添加此 XAML:

    <Button Text="Click Me" Clicked="Button_Clicked" />
    
  2. 编辑 MainPage.xaml.cs,将此代码添加到类的末尾:

    int count = 0;
    void Button_Clicked(object sender, System.EventArgs e)
    {
        count++;
        ((Button)sender).Text = $"You clicked {count} times.";
    }
    
  3. 调试 Android 上的应用:

    Android app with Button

在 Visual Studio 2022 中生成 iOS 应用

可使用联网的 Mac 计算机从 Visual Studio 生成和调试 iOS 应用。 有关详细信息,请参阅安装说明

Windows 分步说明

Download Sample 下载示例

请按照以下步骤以及上面的视频操作:

  1. 选择“文件”>“新建”>“项目...”,或按“创建新项目...”按钮:

    Create a new project

  2. 搜索“Xamarin”或从“项目类型”菜单中选择“移动”。 选择“移动应用(Xamarin.Forms)”项目类型:

    Filter for Xamarin projects

  3. 选择项目名称 - 示例使用“AwesomeApp”:

    Choose a project name

  4. 单击“空白”项目类型,确保选择了“Android”和“iOS”:

    Android and iOS, with .NET Standard

  5. 等到 NuGet 包还原(状态栏中将出现“还原已完成”消息)。

  6. 新 Visual Studio 2019 安装不会配置 Android 模拟器。 单击“调试”按钮上的下拉箭头,然后选择“创建 Android Emulator”以启动仿真器创建屏幕:

    Create Android Emulator dropdown

  7. 在仿真器创建屏幕中,使用默认设置并单击“创建”按钮:

    Android emulator creation screen

  8. 创建仿真器会使你返回“设备管理器”窗口。 单击“启动”按钮以启动新仿真器:

    Android emulator in the Device Manager

  9. Visual Studio 2019 现在应在“调试”按钮上显示新仿真器的名称:

    Android emulator name on the Debug button

  10. 单击“调试”按钮以生成应用程序并将其部署到 Android 仿真器:

    Android emulator displaying the application

自定义应用程序

可以自定义应用程序以添加交互功能。 执行以下步骤可向应用程序添加用户交互:

  1. 编辑 MainPage.xaml,在 </StackLayout> 结束之前添加此 XAML:

    <Button Text="Click Me" Clicked="Button_Clicked" />
    
  2. 编辑 MainPage.xaml.cs,将此代码添加到类的末尾:

    int count = 0;
    void Button_Clicked(object sender, System.EventArgs e)
    {
        count++;
        ((Button)sender).Text = $"You clicked {count} times.";
    }
    
  3. 调试 Android 上的应用:

    Android app

注意

示例应用程序包括视频中未涵盖的附加交互功能。

在 Visual Studio 2019 中生成 iOS 应用

可使用联网的 Mac 计算机从 Visual Studio 生成和调试 iOS 应用。 有关详细信息,请参阅安装说明

此视频介绍在 Windows 上使用 Visual Studio 2019 生成和测试 iOS 应用的过程:

Mac 分步说明

Download Sample 下载示例

请按照以下步骤以及上面的视频操作:

  1. 选择“文件”>“新建解决方案...”或按“新建项目...”按钮,然后选择“多平台”>“应用”>“空白窗体应用”:

    Blank Forms App

  2. 确保已选择“Android”和“iOS”:

    Android and iOS, with .NET Standard

注意

应用名称和组织标识符仅支持 A-Z、a-z、“_”、“.”和数字字符。

  1. 右键单击解决方案,还原 NuGet 包:

    Screenshot shows Restore NuGet Packages selected from the context menu for the solution.

  2. 按调试按钮(或“运行”>“开始调试”)启动 Android Emulator。

  3. 编辑 MainPage.xaml,在 </StackLayout> 结束之前添加此 XAML:

    <Button Text="Click Me" Clicked="Handle_Clicked" />
    
  4. 编辑 MainPage.xaml.cs,将此代码添加到类的末尾:

    int count = 0;
    void Handle_Clicked(object sender, System.EventArgs e)
    {
        count++;
        ((Button)sender).Text = $"You clicked {count} times.";
    }
    
  5. 调试 Android 上的应用:

    Screenshot shows the Android Emulator.

  6. 右键单击,将 iOS 设置为“启动项目”:

    Set the startup project to iOS

  7. 通过从下拉列表中选择 iOS 模拟器来调试 iOS 上的应用。

Mac 分步说明

Download Sample 下载示例

请按照以下步骤以及上面的视频操作:

  1. 选择“文件”>“新建解决方案...”或按“新建项目...”按钮,然后选择“多平台”>“应用”>“空白窗体应用”:

    Blank Forms App

  2. 请确保选中“Android”和“iOS”且勾选了“.NET Standard”代码共享策略 :

    Android and iOS, with .NET Standard

注意

应用名称和组织标识符仅支持 A-Z、a-z、“_”、“.”和数字字符。

  1. 右键单击解决方案,还原 NuGet 包:

    Screenshot shows Restore NuGet Packages selected from the context menu for the solution.

  2. 按调试按钮(或“运行”>“开始调试”)启动 Android Emulator。

  3. 编辑 MainPage.xaml,在 </StackLayout> 结束之前添加此 XAML:

    <Button Text="Click Me" Clicked="Handle_Clicked" />
    
  4. 编辑 MainPage.xaml.cs,将此代码添加到类的末尾:

    int count = 0;
    void Handle_Clicked(object sender, System.EventArgs e)
    {
        count++;
        ((Button)sender).Text = $"You clicked {count} times.";
    }
    
  5. 调试 Android 上的应用:

    Screenshot shows the Android Emulator.

  6. 右键单击,将 iOS 设置为“启动项目”:

    Set the startup project to iOS

  7. 调试 iOS 上的应用:

    iOS app

可从示例库下载或在 GitHub 上查看完整代码。

后续步骤