开发Xamarin.From Android项目
<p><strong><span style="font-size: 18px">一、准备</span></strong></p><p><span style="font-size: 18px"><strong> </strong> <span style="font-size: 16px">安装Xamarin</span></span></p>
<p><span style="font-size: 18px"><strong><span style="font-size: 16px"> <img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328180218148-1498361299.png"></span></strong></span></p>
<p> </p>
<p> </p>
<p><span style="font-size: 18px"><strong><span style="font-size: 16px"> <img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328180503533-363839312.png"></span></strong></span></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> 打开<span data-ttu-id="fedde-113">Visual Studio 安装程序,选择更多,然后点击修改,之后,勾上.Net桌面开发</span></p>
<p><span data-ttu-id="fedde-113"> <img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328180302433-2023526078.png"></span></p>
<p> </p>
<p> 然后下载安装完成即可。</p>
<p> </p>
<p><span style="font-size: 18px"><strong>二、简介</strong></span></p>
<p><span style="font-size: 18px"> <span style="font-size: 16px">1、什么是Xamarin</span></span></p>
<p><span style="font-size: 16px"> <span style="font-size: 15px"> <span data-ttu-id="1e44b-105">Xamarin 是一个开放源代码平台,用于通过 .NET 构建适用于 iOS、Android 和 Windows 的新式高性能应用程序。 <span data-ttu-id="1e44b-106">Xamarin 是一个抽象层,可管理共享代码与基础平台代码的通信。 <span data-ttu-id="1e44b-107">Xamarin 在提供便利(如内存分配和垃圾回收)的托管环境中运行。</span></span></span>Xamarin 应用程序可以在电脑或 Mac 上进行编写并编译为本机应用程序包,如 Android 上的 .apk 文件,或 iOS 上的 .ipa 文件。</span></span><span style="font-size: 16px"><span style="font-size: 15px"> </span> </span></p>
<p><span style="font-size: 15px" data-ttu-id="1e44b-114"> Xamarin 适用于具有以下目标的开发人员:</span></p>
<ul>
<li><span style="font-size: 15px" data-ttu-id="1e44b-115">跨平台共享代码、测试和业务逻辑。</span></li>
<li><span style="font-size: 15px" data-ttu-id="1e44b-116">使用 Visual Studio 在 C# 中编写跨平台应用程序。</span></li>
</ul>
<p><span style="font-size: 16px"> 2、体系结构</span></p>
<p><span style="font-size: 16px"> <img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328175745862-2146351203.png"></span></p>
<p><span data-ttu-id="1e44b-119"> 该图显示跨平台 Xamarin 应用程序的总体体系结构。 <span data-ttu-id="1e44b-120">Xamarin 允许在每个平台上创建本机 UI,并在 C# 中编写跨平台共享的业务逻辑。 <span data-ttu-id="1e44b-121">在大多数情况下,80% 的应用程序代码可使用 Xamarin 进行共享。</span></span></span></p>
<p><span data-ttu-id="1e44b-122"> Xamarin 是在 Mono 基础上生成的,后者是基于 .NET ECMA 标准的 .NET Framework 的开放源代码版本。 <span data-ttu-id="1e44b-123">Mono 存在时间几乎与 .NET Framework 本身一样长,并可在大多数平台上运行,包括 Linux、Unix、FreeBSD 和 macOS。 <span data-ttu-id="1e44b-124">Mono 执行环境会自动处理任务,如内存分配、垃圾回收以及与基础平台的互操作性。</span></span></span></p>
<p><span style="font-size: 16px"> 3、Xamarin Android</span></p>
<p><span style="font-size: 18px"> <span style="font-size: 15px"> <span data-ttu-id="2eabb-103">Xamarin Android 应用程序在 Mono 执行环境中运行。 <span data-ttu-id="2eabb-104">此执行环境与 Android 运行时(ART)虚拟机并行运行。 <span data-ttu-id="2eabb-105">这两个运行时环境都在 Linux 内核的顶层运行,并向用户代码公开各种 Api,使开发人员能够访问基础系统。</span></span></span></span></span></p>
<p><span style="font-size: 18px"><span style="font-size: 15px"><span data-ttu-id="2eabb-103"><span data-ttu-id="2eabb-104"><span data-ttu-id="2eabb-105"> <img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328175528469-650843731.png"></span></span></span></span></span></p>
<p><span style="font-size: 18px"><span style="font-size: 15px"><span data-ttu-id="2eabb-103"><span data-ttu-id="2eabb-104"><span data-ttu-id="2eabb-105"> 4.一些基础教程</span></span></span></span></span></p>
<p><span style="font-size: 18px"><span style="font-size: 15px"><span data-ttu-id="2eabb-103"><span data-ttu-id="2eabb-104"><span data-ttu-id="2eabb-105"> 1)Grid</span></span></span></span></span><span style="font-size: 18px"><span style="font-size: 15px"><span data-ttu-id="2eabb-103"><span data-ttu-id="2eabb-104"><span data-ttu-id="2eabb-105"> </span></span></span></span></span></p>
<div class="cnblogs_code">
<pre><Grid Margin=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">20,35,20,20</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">0.5*</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<ColumnDefinition Width=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">0.5*</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">50</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<RowDefinition Height=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">50</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
</Grid.RowDefinitions>
<<span class="hljs-name">Label <span class="hljs-attr">Grid.ColumnSpan=<span class="hljs-string">"2" <span class="hljs-attr">Text=<span class="hljs-string">"This text uses the ColumnSpan property to span both columns." /></span></span></span></span></span>
<Label Grid.Row=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
Text</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Column 0, Row 1</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<Label Grid.Column=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
Grid.Row</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
Text</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Column 1, Row 1</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
</Grid></pre>
</div>
<p><span style="font-size: 15px" data-ttu-id="a0057-104">列和行数据存储在 <code>ColumnDefinitions</code> 和 <code>RowDefinitions</code> 属性中,这两个属性分别是 <code>ColumnDefinition</code> 和 <code>RowDefinition</code> 对象的集合。 <span data-ttu-id="a0057-105">每个 <code>ColumnDefinition</code> 的宽度由 <code>ColumnDefinition.Width</code> 属性设置,每个 <code>RowDefinition</code> 的高度由 <code>RowDefinition.Height</code> 属性设置。 <span data-ttu-id="a0057-106">有效的宽度值和高度值为:</span></span></span></p>
<p> </p>
<ul>
<li><span style="font-size: 15px" data-ttu-id="a0057-107"><code>Auto</code> 对列或行进行大小调整以适应内容。</span></li>
<li><span style="font-size: 15px" data-ttu-id="a0057-108">比例值,将列和行的大小设置为剩余空间的比例。 <span data-ttu-id="a0057-109">以 <code>*</code> 结尾表示比例值。</span></span></li>
<li><span style="font-size: 15px" data-ttu-id="a0057-110">绝对值,使用特定的固定值调整列或行的大小。</span></li>
</ul>
<p> </p>
<p><span style="font-size: 15px" data-ttu-id="a0057-111">因此,在上面的代码中,每列的宽度为 <code>Grid</code> 的一半,而每行的高度为 50 个与设备无关的单位。</span></p>
<p><span style="font-size: 15px" data-ttu-id="a0057-111"> 2)Shell</span></p>
<p><span style="font-size: 15px" data-ttu-id="a0057-111"> Shell 就是一个容器,负责处理每个应用都需要的基本 UI 功能,这样开发人员就能以应用的核心工作为重点。现有 iOS 和 Android 应用也可以轻松采用 Shell,并立即受益于导航、UI 性能和扩展性方面的改进。</span><span style="font-size: 15px" data-ttu-id="a0057-111"> </span></p>
<p> Shell 的优势如下:</p>
<p> </p>
<ul>
<li>在一个位置集中描述应用的可视结构</li>
<li>通用导航 UI 和无所不在的导航服务(含深层链接)</li>
<li>集成的搜索处理程序,可提高整体应用内搜索体验</li>
<li>默认可扩展理念,可提高多功能性和灵活性 </li>
</ul>
<p>Shell 由三个层次结构元素组成:ShellItem、ShellSection 和 ShellContent。每个 ShellContent 都是 ShellSection 的子级,ShellSection 又是 ShellItem(即 Shell 的所有部分)的子级。这三个元素本身都不代表 UI,而是代表应用体系结构组织。Shell 需要使用这些项,并针对运行平台生成适当的导航 UI:</p>
<ul>
<li>ShellItem:应用的顶级结构,由浮出控件中的项表示。可以包含多个 ShellSection。</li>
<li>ShellSection:应用内容分组,可通过底部的选项卡进行导航。可以包含一个或多个 ShellContent(多个 ShellContent 可通过顶部选项卡进行导航)。</li>
<li>ShellContent:应用的 ContentPage。</li>
</ul>
<div class="cnblogs_code">
<pre><ShellItem Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">主页</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<ShellSection Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">报告查询</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<ShellContent>
<local:MainPage/>
</ShellContent>
</ShellSection>
<ShellSection Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">联系我们</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<ShellContent>
<local:MainPage/>
</ShellContent>
</ShellSection>
<ShellSection Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">实验室</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<ShellContent>
<local:MainPage/>
</ShellContent>
</ShellSection>
<ShellSection Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">特色</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<ShellContent Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">资质</span><span style="color: rgba(128, 0, 0, 1)">"</span> ContentTemplate=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{DataTemplate local:AboutPage}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/>
<ShellContent Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">特色</span><span style="color: rgba(128, 0, 0, 1)">"</span> ContentTemplate=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{DataTemplate local:AboutPage}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/>
<ShellContent Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">实验室</span><span style="color: rgba(128, 0, 0, 1)">"</span> ContentTemplate=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{DataTemplate local:AboutPage}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/>
</ShellSection>
</ShellItem></pre>
</div>
<p><span style="font-size: 18px"><strong>三、开始开发</strong></span></p>
<p><span style="font-size: 18px"><strong>官方网址:https://docs.microsoft.com/zh-cn/xamarin/get-started/</strong></span></p>
<p><span style="font-size: 15px"> 新建项目Xamarin.From项目</span></p>
<p><span style="font-size: 18px"><strong> <img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328180808640-1764187999.png"></strong></span></p>
<p> </p>
<p> </p>
<p> 我的项目结构如下:</p>
<p> <img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328180858542-1852412487.png"></p>
<p> </p>
<p> 在这里,开发一个如下界面APP</p>
<p> <img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328184334993-1981779861.gif"></p>
<p>首先,修改我们的MainPage</p>
<div class="cnblogs_code">
<pre> <StackLayout Margin=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">10,35,10,10</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<Label Text=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Notes</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
HorizontalOptions</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Center</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
FontAttributes</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Bold</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<Editor x:Name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">editor</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
Placeholder</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Enter your note</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
HeightRequest</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">100</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">*</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<ColumnDefinition Width=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">*</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
</Grid.ColumnDefinitions>
<Button Text=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Save</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
Clicked</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">OnSaveButtonClicked</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<Button Grid.Column=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
Text</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Delete</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
Clicked</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">OnDeleteButtonClicked</span><span style="color: rgba(128, 0, 0, 1)">"</span>/>
</Grid>
</StackLayout></pre>
</div>
<p> </p>
<p>MainPage.xml.cs更改如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">string</span> _fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">notes.txt</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> MainPage()
{
InitializeComponent();
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (File.Exists(_fileName))
{
editor.Text </span>=<span style="color: rgba(0, 0, 0, 1)"> File.ReadAllText(_fileName);
}
}
</span><span style="color: rgba(0, 0, 255, 1)">void</span> OnSaveButtonClicked(<span style="color: rgba(0, 0, 255, 1)">object</span><span style="color: rgba(0, 0, 0, 1)"> sender, EventArgs e)
{
File.WriteAllText(_fileName, editor.Text);
}
</span><span style="color: rgba(0, 0, 255, 1)">void</span> OnDeleteButtonClicked(<span style="color: rgba(0, 0, 255, 1)">object</span><span style="color: rgba(0, 0, 0, 1)"> sender, EventArgs e)
{
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (File.Exists(_fileName))
{
File.Delete(_fileName);
}
editor.Text </span>= <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.Empty;
}</span></pre>
</div>
<p> </p>
<p>界面布局,我在这里讲用到Shell,为了使用,我们需要在MainActivity中进行配置:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">override</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> OnCreate(Bundle savedInstanceState)
{
TabLayoutResource </span>=<span style="color: rgba(0, 0, 0, 1)"> Resource.Layout.Tabbar;
ToolbarResource </span>=<span style="color: rgba(0, 0, 0, 1)"> Resource.Layout.Toolbar;
</span><span style="color: rgba(0, 0, 255, 1)">base</span><span style="color: rgba(0, 0, 0, 1)">.OnCreate(savedInstanceState);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">添加启用Shell功能的标识
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果需要其他功能则添加响应标识即可,如:"Visual_Experimental"等;</span>
<span style="color: rgba(0, 0, 255, 1)">global</span>::Xamarin.Forms.Forms.SetFlags(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Shell_Experimental</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Xamarin.Essentials.Platform.Init(this, savedInstanceState);</span>
<span style="color: rgba(0, 0, 255, 1)">global</span>::Xamarin.Forms.Forms.Init(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">, savedInstanceState);
LoadApplication(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> App());
}</span></pre>
</div>
<p> </p>
<p>现在,我们将添加一个Shell页面,用来控制整体布局</p>
<div class="cnblogs_code">
<pre><?xml version=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1.0</span><span style="color: rgba(128, 0, 0, 1)">"</span> encoding=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">"</span> ?>
<Shell xmlns=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://xamarin.com/schemas/2014/forms</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
xmlns:x</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://schemas.microsoft.com/winfx/2009/xaml</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
xmlns:local</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">clr-namespace:ZYY_APP</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
FlyoutBehavior</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Flyout</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
x:Class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ZYY_APP.AppShell</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
Title</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">主页</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<Shell.FlyoutHeader>
<Grid HeightRequest=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">200</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<Image Source=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">logo.png</span><span style="color: rgba(128, 0, 0, 1)">"</span> Aspect=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">AspectFill</span><span style="color: rgba(128, 0, 0, 1)">"</span> Margin=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">24</span><span style="color: rgba(128, 0, 0, 1)">"</span>/>
</Grid>
</Shell.FlyoutHeader>
<MenuItem Text=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">会员</span><span style="color: rgba(128, 0, 0, 1)">"</span> Icon=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">feed_png</span><span style="color: rgba(128, 0, 0, 1)">"</span> CommandParameter=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span> Command=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{Binding VipCommand}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/>
<MenuItem Text=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">我</span><span style="color: rgba(128, 0, 0, 1)">"</span> Icon=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">me_png</span><span style="color: rgba(128, 0, 0, 1)">"</span> CommandParameter=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2</span><span style="color: rgba(128, 0, 0, 1)">"</span> Command=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{Binding VipCommand}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/>
<MenuItem Text=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">设置</span><span style="color: rgba(128, 0, 0, 1)">"</span> Icon=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">editor_png</span><span style="color: rgba(128, 0, 0, 1)">"</span> CommandParameter=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">3</span><span style="color: rgba(128, 0, 0, 1)">"</span> Command=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{Binding VipCommand}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/>
<ShellItem Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">主页</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<ShellSection Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">报告查询</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<ShellContent>
<local:MainPage/>
</ShellContent>
</ShellSection>
<ShellSection Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">联系我们</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<ShellContent>
<local:MainPage/>
</ShellContent>
</ShellSection>
<ShellSection Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">实验室</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<ShellContent>
<local:MainPage/>
</ShellContent>
</ShellSection>
<ShellSection Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">特色</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<ShellContent Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">资质</span><span style="color: rgba(128, 0, 0, 1)">"</span> ContentTemplate=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{DataTemplate local:AboutPage}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/>
<ShellContent Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">特色</span><span style="color: rgba(128, 0, 0, 1)">"</span> ContentTemplate=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{DataTemplate local:AboutPage}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/>
<ShellContent Title=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">实验室</span><span style="color: rgba(128, 0, 0, 1)">"</span> ContentTemplate=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{DataTemplate local:AboutPage}</span><span style="color: rgba(128, 0, 0, 1)">"</span>/>
</ShellSection>
</ShellItem>
</Shell></pre>
</div>
<p> </p>
<p>这里有几点需要注意:</p>
<p> 1.ShellSection为分组内容,即底部导航栏。</p>
<p> 2.ShellItem表示浮出控件中的一个或多个项。</p>
<p> 3.ShellContent为页面内容,如果同一ShellSection下有多个ShellContent,则会变为带有顶部标签的形式。</p>
<p> 4.Flyout为浮出控件,可以从侧面滑出的菜单。主要有两部分组成:头部(FlyoutHeader)和菜单项(MenuItem)。</p>
<p>关于MenuItem,有的地方写法如下:</p>
<p> <img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328185222461-217231725.png"></p>
<p> </p>
<p> 这一种写法,在我的项目里面是不支持的,会报错,所以要注意,直接写MenuItem即可。</p>
<p>为了让主页面为我们的Shell页面,更改如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">partial</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> App : Application
{
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> App()
{
InitializeComponent();
MainPage </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> AppShell();
}
}</span></pre>
</div>
<p> </p>
<p>另外,我们的AppShell.xml.cs别忘记了,设置顶级容器为Shell:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">partial</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> AppShell : Shell
{
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> AppShell()
{
InitializeComponent();
}
}</span></pre>
</div>
<p>修改完成之后,生成整个解决方案:</p>
<p><img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328185655042-240975912.png"></p>
<p> </p>
<p> 报错了,为什么呢?</p>
<p> 因为目录里面有中文,在Xamarin.From生成Android项目中,整个路径里面都不能出现中文,否则无法生成。</p>
<p> 下面我们来对生成的项目进行调试,有两种,一种是真机,一种模拟器。</p>
<p>1)模拟器</p>
<p> 添加模拟器</p>
<p> <img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328190524331-878918729.png"></p>
<p> </p>
<p> </p>
<p> <img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328190546752-1334666936.png"></p>
<p> </p>
<p> 模拟器会比较慢,而且因为版本问题,不一定安装成功;</p>
<p>2)真机</p>
<p> 首先将我们的手机要调为开发者模式,然后打开USB调试,用数据线将手机与电脑连接</p>
<p> <img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328190744493-1460697616.png"></p>
<p> </p>
<p>连接成功之后</p>
<p><img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328190833485-385142944.png"></p>
<p> </p>
<p> 这里会出现你自己的本机,然后运行程序,将会在真机进行安装部署,在我们的APP开始安装之前,会安装一些插件,全部安装就可以了,安装完成后,如下:</p>
<p><img src="https://img2020.cnblogs.com/blog/496803/202003/496803-20200328190958455-446229208.png"></p>
<p> </p>
<p> </p>
<p> 至此,一个简单的安卓APP就安装成功了。</p>
<p> </p><br><br>
来源:https://www.cnblogs.com/zhangyunyun/p/12588835.html
頁:
[1]