资讯详情

[开发过程]上位机>关于MAUI

1 框架

2 VS2022中新建的MAUI项目

2.1 程序入口

2.2 跳转到App

2.3 跳转到AppShell

2.4 跳转到MainPage


1 框架

BCL :.NET 6 基类库

Mono Runtime和WinRT:提供执行环境。

Mono : 是CLR开源实现(针对Android、iOS和 macOS), 是第三方开源项目。

The Mono Runtime | Monohttps://www.mono-project.com/docs/advanced/runtime/关于CLR(Common Language Runtime),查看以下链接:

https://docs.microsoft.com/en-us/dotnet/standard/clrhttps://docs.microsoft.com/en-us/dotnet/standard/clr


谈及CLR,就需了解.net实现跨平台代码的过程:

  1. 选择编译器。

  2. 编译代码Microsoft 中间语言将您的代码编译为 MSIL。

  3. 在CLR,即时编译器(JIT) 将 MSIL 转换为本机代码。

  4. 运行代码。

https://docs.microsoft.com/en-us/dotnet/standard/managed-execution-processhttps://docs.microsoft.com/en-us/dotnet/standard/managed-execution-process


托管代码:托管代码执行runtime”管理。.NET中的“runtime”称为CLR,CLR与实现无关(例如Mono、.NET Framework 或 .NET Core/.NET 5 )。CLR 负责获取托管代码,编译成机器代码,然后执行。runtime提供自动内存管理、安全边界、类型安全等重要服务。

非托管代码(如:C/C 程序):非托管代码几乎负责一切,本质上是加载到内存并启动的二进制文件。非托管代码开发者需要考虑从内存管理到安全。

What is managed code? | Microsoft DocsLearn how managed code is code whose execution is managed by a runtime, the Common Language Runtime (CLR).https://docs.microsoft.com/en-us/dotnet/standard/managed-code


2 VS2022中新建的MAUI项目

2.1 程序入口

VS2022中新建了MAUI项目后,程序入口如下:

其中代码如下: 

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");
			});

		return builder.Build();
	}
}

代码.UseMauiApp<App>()会跳转到 “App”中。


2.2 跳转到App

再看App,包含两个文件,

.xaml后缀的是界面UI相关内容,

.cs后缀的是该UI相关的逻辑。

App.xaml代码如下:

<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:OneDream"
             x:Class="OneDream.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
                <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

 主要看:

<ResourceDictionary Source="Resources/Styles/Colors.xaml" /> <ResourceDictionary Source="Resources/Styles/Styles.xaml" />

是颜色和默认样式的定义。

而App.xaml.cs中,主要是进行了页面的跳转,跳转到AppShell,代码如下:

public partial class App : Application
{
	public App()
	{
		InitializeComponent();

		MainPage = new AppShell();
	}
}

2.3 跳转到AppShell

再看AppShell,同样包含两个文件,但主要看AppShell.xaml。

AppShell.xaml代码如下,主要用了一个模板,路由到MainPage。

<?xml version="1.0" encoding="UTF-8" ?>
<Shell
    x:Class="OneDream.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:OneDream"
    Shell.FlyoutBehavior="Disabled">

    <ShellContent
        Title="Home"
        ContentTemplate="{DataTemplate local:MainPage}"
        Route="MainPage" />

</Shell>

2.4 跳转到MainPage

接下来就要看 MainPage,同样是包括两个文件:

MainPage.xaml代码如下,是运行后看到的那个界面的UI代码: 

<?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="OneDream.MainPage">
			 
    <ScrollView>
        <VerticalStackLayout 
            Spacing="25" 
            Padding="30,0" 
            VerticalOptions="Center">

            <Image
                Source="dotnet_bot.png"
                SemanticProperties.Description="Cute dot net bot waving hi to you!"
                HeightRequest="200"
                HorizontalOptions="Center" />
                
            <Label 
                Text="Hello, World!"
                SemanticProperties.HeadingLevel="Level1"
                FontSize="32"
                HorizontalOptions="Center" />
            
            <Label 
                Text="Welcome to .NET Multi-platform App UI"
                SemanticProperties.HeadingLevel="Level2"
                SemanticProperties.Description="Welcome to dot net Multi platform App U I"
                FontSize="18"
                HorizontalOptions="Center" />

            <Button 
                x:Name="CounterBtn"
                Text="Click me"
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnCounterClicked"
                HorizontalOptions="Center" />

        </VerticalStackLayout>
    </ScrollView>
 
</ContentPage>

而MainPage.xaml.cs的代码如下,主要是看其中的点击事件,即OnCounterClicked函数。

里面的CounterBtn与UI界面文件(MainPage.xaml)中的按钮对应。

即:<Button                  x:Name="CounterBtn"                 Text="Click me"                 SemanticProperties.Hint="Counts the number of times you click"                 Clicked="OnCounterClicked"                 HorizontalOptions="Center" />

namespace OneDream;

public partial class MainPage : ContentPage
{
	int count = 0;

	public MainPage()
	{
		InitializeComponent();
	}

	private void OnCounterClicked(object sender, EventArgs e)
	{
		count++;

		if (count == 1)
			CounterBtn.Text = $"Clicked {count} time";
		else
			CounterBtn.Text = $"Clicked {count} times";

		SemanticScreenReader.Announce(CounterBtn.Text);
	}
}


后记:

持续更新中。

标签: 韩国cas传感器bcl

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台