有点酷,使用 .NET MAUI 探索太空
Net 使用 探索 太空 有点 MAUI
2023-06-13 09:11:58 时间
设计
我准备用 .NET Maui 实现一个非常有意思的 "前往太空" 的程序。第一步,需要图片素材,我使用了 Aan Ragil 的一组非常棒的 Dribbble 素材图片。
当然,你也可以在最下面的链接进行下载。
实现
这个应用程序的完整源代码可以在 Github 上访问并下载。我们总共需要做三个页面。
初始化项目
我创建了一个空的 .NET Maui 程序。然后,我禁用了每个页面上的导航栏,然后设置了背景颜色,主要是修改了 App.xaml 文件。
<!-- Content Page Style -->
<Style TargetType="ContentPage" ApplyToDerivedTypes="True">
<Setter Property="NavigationPage.HasNavigationBar" Value="False" />
<Setter Property="BackgroundColor" Value="{StaticResource PageBackgroundColor}" />
<Setter Property="Padding" Value="0"/>
</Style>
<!-- Navigation Page -->
<Style TargetType="NavigationPage" ApplyToDerivedTypes="True">
<Setter Property="BackgroundColor" Value="{StaticResource PageBackgroundColor}" />
</Style>
对于安卓设备, 使用 Android LifeCycle 事件让状态栏变为半透明。
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("Montserrat-Medium.ttf", "RegularFont");
fonts.AddFont("Montserrat-SemiBold.ttf", "MediumFont");
fonts.AddFont("Montserrat-Bold.ttf", "BoldFont");
})
.ConfigureLifecycleEvents(events =>
{
#if ANDROID
events.AddAndroid(android => android.OnCreate((activity, bundle) => MakeStatusBarTranslucent(activity)));
static void MakeStatusBarTranslucent(Android.App.Activity activity)
{
activity.Window.SetFlags(Android.Views.WindowManagerFlags.LayoutNoLimits, Android.Views.WindowManagerFlags.LayoutNoLimits);
activity.Window.ClearFlags(Android.Views.WindowManagerFlags.TranslucentStatus);
activity.Window.SetStatusBarColor(Android.Graphics.Color.Transparent);
}
#endif
});
为了让视图覆盖底部,每个页面使用了 IgnoreSafeArea 属性。
<ContentPage>
<Grid IgnoreSafeArea="{OnPlatform Android=False, iOS=True}">
</Grid>
</ContentPage>
为了简单起见,我没有使用 MVVM 模式,而是普通的 Maui UI 结构。创建了一个 **Planet **类来保存有关行星的信息,并创建了一个 PlanetService 服务。
初始页
接下来是初始页面,我把它分成了两部分。
上部分由每个行星的单独图像组成的。我使用了 HorizontalOptions、VerticalOptions、TranslationX、TranslationY、WidthRequest 和 HeightRequest 控制每个图像的位置和大小。
<Image
Source="earth.png"
VerticalOptions="Start" HorizontalOptions
="Center"
TranslationX="-48"
TranslationY="148"
WidthRequest="96"
HeightRequest="96"/>
下部分,我没有使用 Frame 控件,而是使用了更轻量的 Border 控件。
<Border
Padding="24,32"
BackgroundColor="{StaticResource FrameBackgroundColor}"
Stroke="{StaticResource BorderColor}"
HorizontalOptions="Fill"
VerticalOptions="End"
Margin="20">
<Border.StrokeShape>
<RoundRectangle CornerRadius="24"/>
</Border.StrokeShape>
<VerticalStackLayout
Spacing="16">
<Label
HorizontalOptions="Center"
HorizontalTextAlignment="Center"
Style="{StaticResource IntroPageHeaderStyle}"
Text="Hello!"/>
<Label
HorizontalOptions="Center"
HorizontalTextAlignment="Center"
LineBreakMode="WordWrap"
Style="{StaticResource IntroPageTextStyle}"
Text="Want to know and explain all things about the planets in the Milky Way galaxy?"/>
<Button
Style="{StaticResource ButtonStyle}"
Text="Explore Now"
HorizontalOptions="Center"
Margin="0,12,0,6"
Clicked="ExploreNow_Clicked"/>
</VerticalStackLayout>
</Border>
看一下第一个页面的效果。
看起来还不错吧!我们还可以设置淡入的效果,加一些动画。
protected override async void OnAppearing()
{
base.OnAppearing();
if (this.AnimationIsRunning("TransitionAnimation"))
return;
var parentAnimation = new Animation();
//Planets Animation
parentAnimation.Add(0, 0.2, new Animation(v => imgMercury.Opacity = v, 0, 1, Easing.CubicIn));
parentAnimation.Add(0.1, 0.3, new Animation(v => imgVenus.Opacity = v, 0, 1, Easing.CubicIn));
parentAnimation.Add(0.2, 0.4, new Animation(v => imgEarth.Opacity = v, 0, 1, Easing.CubicIn));
parentAnimation.Add(0.3, 0.5, new Animation(v => imgMars.Opacity = v, 0, 1, Easing.CubicIn));
parentAnimation.Add(0.4, 0.6, new Animation(v => imgJupiter.Opacity = v, 0, 1, Easing.CubicIn));
parentAnimation.Add(0.5, 0.7, new Animation(v => imgSaturn.Opacity = v, 0, 1, Easing.CubicIn));
parentAnimation.Add(0.6, 0.8, new Animation(v => imgNeptune.Opacity = v, 0, 1, Easing.CubicIn));
parentAnimation.Add(0.7, 0.9, new Animation(v => imgUranus.Opacity = v, 0, 1, Easing.CubicIn));
//Intro Box Animation
parentAnimation.Add(0.7, 1, new Animation(v => frmIntro.Opacity = v, 0, 1, Easing.CubicIn));
//Commit the animation
parentAnimation.Commit(this, "TransitionAnimation", 16, 3000, null, null);
}
差不多完成了,我们看一下在手机上最后的效果,非常酷!
你可以在下面的地址找到它的源代码和素材信息。
https://github.com/naweed/MauiPlanets
https://dribbble.com/shots/15592060-Planet-Mobile-App
相关文章
- 从壹开始前后端分离【 .NET Core2.0/3.0 +Vue2.0 】框架之十三 || DTOs 对象映射使用,项目部署Windows+Linux完整版[通俗易懂]
- 踩坑 Windows 服务来宿主 .NET 程序
- Linux.Net:开启新技术之旅(linux.net)
- MySQL Variables net_retry_count 数据库 参数变量解释及正确配置使用
- MongoDB 使用.NET开发充分利用数据库性能(mongodb.net)
- 使用.NET技术操作MySQL数据库:简单易学,高效稳定(.net操作mysql)
- asp.net的web服务MSSQL检测ASP.NET的Web服务——利用它构建更棒的站点(mssql检测基于)
- 应用使用Linux环境部署.Net应用程序(linux部署.net)
- Net中连接MySQL技术简述(.net中引用mysql)
- 低耗时高效net下使用Redis实现队列处理(redis队列 .net)
- Asp.net给站点某目录增加Aspnet用户
- Asp.Net和AJAX.Net的区别
- asp.net获取某个时间段的星期并以表格形式排列出来
- asp.net通过httpModule计算页面的执行时间
- asp.net保存、修改没有runat=server控件的控件值的一个解决方案
- .NET解决TabControl页里面多余边距问题经验分享
- .NET正则表达式使用高级技巧之替换类介绍
- 在.NET中使用Newtonsoft.Json转换,读取,写入的方法介绍
- Asp.net后台把脚本样式输出到head标签中节省代码冗余
- ASP.NET页面在IE缓存的清除办法
- ADO.Net类型化DataSet的简单介绍
- .Net笔记:System.IO之Stream的使用详解
- Win7中IIS的ASP.NET环境配置简洁版
- .NET中文乱码的解决方法分享
- .NET的file文件上传控件使用方法修改web.config文件上传大文件
- asp.net使用npoi读取excel模板并导出下载详解
- asp.net自定义分页控件示例
- ASP.net中获取客户端参数操作系统信息
- asp.net发邮件示例
- 使用asp.net改变图片颜色如灰色的变成彩色