.NET MAUI 安卓应用开发初体验
一、.NET MAUI开发环境搭建&安卓SDK和安卓模拟器安装提示网络连接失败问题解决
引言
本节目标是帮助第一次搭建.NET MAUI开发环境,在下载安卓SDK和安卓模拟器过程中一直提示网络问题解决思路。
现象一:Visiual Studio 2022中添加Android设备管理时,提示系统映像下载错误:Network is not reachable. Please check your connection and try again.
![](https://img2022.cnblogs.com/blog/1963451/202211/1963451-20221122152815022-711288951.png)
现象二:Visiual Studio 2022中添加Android SDK和工具时,提示"网络不可用。请检查你的连接,然后再重试"
![](https://img2022.cnblogs.com/blog/1963451/202211/1963451-20221122152827440-1526046680.png)
.NET MAUI 简介
官方文档
什么是 .NET MAUI? - .NET MAUI | Microsoft Learn
.NET Multi-platform App UI (.NET MAUI) is a cross-platform framework for creating native mobile and desktop apps with C# and XAML.
Using .NET MAUI, you can develop apps that can run on Android, iOS, macOS, and Windows from a single shared code-base.
.NET MAUI 全称 .NET Multi-platform App UI
开发环境介绍
VS版本:Microsoft Visual Studio Community 2022 (64 位) - Current 版本 17.4.1
.NET 版本:.NET 7.0 STS
安装【 .NET Multi-platform App UI 开发】
![](https://img2022.cnblogs.com/blog/1963451/202211/1963451-20221122164729467-1346744263.png)
Android SDK 和 安卓模拟器
这里直接介绍出现网络问题后,解决方案
1、查看VS中 Android SDK 保存位置
2、安装 JAVA JDK,并配置在系统环境中JAVA_HOME (可以选择JDK11或者JDK 8.0)
3、下载并安装 Android Studio
4、启动Android Studio,设置国内镜像
![](https://img2022.cnblogs.com/blog/1963451/202211/1963451-20221122164758704-1474003153.png)
![](https://img2022.cnblogs.com/blog/1963451/202211/1963451-20221122164806184-196582890.png)
5、设置 Android SDK 保存位置,将Android Studio 中SDK 保存位置 和 VS 中一样
![](https://img2022.cnblogs.com/blog/1963451/202211/1963451-20221122164818751-23715661.png)
6、下载对应的Android SDK 和 Android Emulator
![](https://img2022.cnblogs.com/blog/1963451/202211/1963451-20221122164829887-1144339093.png)
7、通过AVD Manager新建安卓模拟设备
二、第一个.NET MAUI 安卓应用程序
跟着官方文档,开启第一个.NET MAUI 程序
新建一个.NET MAUI应用程序
![](https://img2022.cnblogs.com/blog/1963451/202211/1963451-20221123214403056-1272208573.png)
运行效果
![](https://img2022.cnblogs.com/blog/1963451/202211/1963451-20221123214555981-1727521352.png)
项目截图
![](https://img2022.cnblogs.com/blog/1963451/202211/1963451-20221123214431624-114016975.png)
MainViewModel
新建ViewModel文件夹,添加MainViewModel类,继承ObservableObject。通过NuGet添加CommunityToolkit.Mvvm 8.1.0-preview1 依赖项。
public partial class MainViewModel : ObservableObject
{
public MainViewModel()
{
this.Items= new ObservableCollection<string>();
}
[ObservableProperty]
public string inputText;
[ObservableProperty]
public ObservableCollection<string> items;
[RelayCommand]
public void Add()
{
if (!string.IsNullOrWhiteSpace(InputText))
{
Items.Add(InputText);
InputText = string.Empty;
}
}
[RelayCommand]
public void Remove(string item)
{
if (Items.Contains(item))
{
Items.Remove(item);
}
}
}
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiAppMvvM.MainPage"
xmlns:viewModel="clr-namespace:MauiAppMvvM.ViewModel"
x:DataType="viewModel:MainViewModel"
x:Name="mainPage">
<Grid RowDefinitions="100, Auto, *"
ColumnDefinitions=".75*, .25*"
Padding="10"
RowSpacing="10"
ColumnSpacing="10">
<Image Grid.ColumnSpan="2"
Source="dotnet_bot.svg"
BackgroundColor="Orange"/>
<Entry Placeholder="请输入您的任务项"
Grid.Row="1"
Grid.Column="0"
Keyboard="Chat"
Text ="{Binding InputText}"/>
<Button Text="Add"
Grid.Row="1"
Grid.Column="1"
Command="{Binding AddCommand}" />
<CollectionView Grid.Row="2"
Grid.ColumnSpan="2"
ItemsSource="{Binding Items}">
<CollectionView.ItemTemplate >
<DataTemplate x:DataType="{x:Type x:String}">
<SwipeView>
<SwipeView.RightItems>
<SwipeItem Text="Delete"
IconImageSource="delete.png"
BackgroundColor="LightPink"
Command="{Binding Source={RelativeSource AncestorType={x:Type viewModel:MainViewModel}}, Path=RemoveCommand}"
CommandParameter="{Binding .}"/>
</SwipeView.RightItems>
<Grid Padding="0,5">
<Frame>
<Label Text="{Binding .}"
FontSize="24"/>
</Frame>
</Grid>
</SwipeView>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</Grid>
</ContentPage>
MainPage.xaml.cs
public MainPage(MainViewModel viewModel)
{
InitializeComponent();
this.BindingContext = viewModel;
}
MauiProgram.cs
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
#if DEBUG
builder.Logging.AddDebug();
#endif
builder.Services.AddSingleton<MainPage>();
builder.Services.AddSingleton<ViewModel.MainViewModel>();
return builder.Build();
}
}
至此,一个完整的.NET MAUI完成了,让我们一起享受.NET MAUI之旅吧!
后记
<SwipeItem Text="Delete"
IconImageSource="delete.png"
BackgroundColor="LightPink"
Command="{Binding Source={RelativeSource AncestorType={x:Type viewModel:MainViewModel}}, Path=RemoveCommand}"
CommandParameter="{Binding .}"/>
SwipeItem的Command实际没有生效,目前还没有找到原因
相关文章
- 如何在 Linux 中挂载 Bitlocker 加密的 Windows 分区
- HDF驱动框架探路(一):工欲善其事,必先利其器(linux驱动开发)
- 七个使用 wget 命令的技巧
- 微软为Windows 11推送更新补丁:仅测试新管线 无任何实质新内容
- Linux 系统日常巡检脚本
- IntelliJ IDEA 2022.3正式发布,配置云同步&支持Redis好用到炸
- 入门:如何在 Fedora Linux 中切换到深色模式
- vifm:为铁杆 Vim 爱好者提供的终端文件浏览器
- Linux驱动实践:一步一步编写字符设备驱动程序
- 继承Windows 10缺点!教你关闭Windows 11烦人的网络搜索
- Windows 10系统,Msconfig怎么进入安全模式,Windows 10进入安全模式方法
- 五年不用升级 微软官方精简:全新Windows 10系统发布
- 还没收到Windows 11推送?让Windows 10用上Windows 11开始菜单
- 教大家Windows 10系统电脑怎么分盘
- 只需三个步骤,即可在Windows 11上禁用文件资源管理器命令栏
- 电脑技巧:Windows 10 自带存储感知功能给电脑磁盘瘦身
- Windows 10系统,无法连接打印机提示0x000003e3,Windows 10打印机设置方法
- 支付业务订单系统分库分表
- 自定义新建文件夹的名称,让你的系统更加个性化
- 树莓派 3 还是 4:你应该买哪一个?