Xamarin.Forms(移动应用)轮盘抽签软件(Android)

2023-11-15

该文章由我前面的文章https://blog.csdn.net/dabo_520/article/details/129760956?spm=1001.2014.3001.5501改编而来,它是程序的核心,具体详细可自行前往观看。

1、软件开发准备

首先是选择移动应用这个        

然后在MainPage.xaml这里修改,因为我用的是shell布局

 

 这是MainPage.xaml的代码:

<?xml version="1.0" encoding="utf-8" ?>
<Shell xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       xmlns:v="clr-namespace:hope"
             x:Class="hope.MainPage">
    <Shell.FlyoutHeader>
        <StackLayout>
            <Image Source="kon.jpg" Aspect="AspectFill"/>
        </StackLayout>
    </Shell.FlyoutHeader>

    <Shell.FlyoutFooter>
        <StackLayout>
            <Label Text="制作:CHL" HorizontalOptions="Center" VerticalOptions="Center"
                   HeightRequest="30" FontSize="15"/>
        </StackLayout>
    </Shell.FlyoutFooter>

    <FlyoutItem Icon="zhu.png" Title="主页">
        <Tab Icon="zao.png" Title="早餐">
            <ShellContent ContentTemplate="{DataTemplate v:Page1}"/>
        </Tab>
        <Tab Icon="wu.png" Title="午餐">
            <ShellContent ContentTemplate="{DataTemplate v:Page2}"/>
        </Tab>
        <Tab Icon="wang.png" Title="晚餐">
            <ShellContent ContentTemplate="{DataTemplate v:Page3}"/>
        </Tab>
    </FlyoutItem>
</Shell>

这是MainPage.xaml.cs的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace hope
{
    public partial class MainPage : Shell //这里要改
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }
}

这里只演示一部分功能:

这是Page1.xaml的代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             Title="早餐"
             x:Class="hope.Page1">
    <ContentPage.Content>
        <StackLayout>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="120" />
                    <RowDefinition Height="120" />
                    <RowDefinition Height="120" />
                </Grid.RowDefinitions>

                <Label x:Name="xian1" Text="粥" Background="gray" FontSize="50" Grid.Column="0" Grid.Row="0" 
                   HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                <Label x:Name="xian2" Text="炒粉、面" Background="gray" FontSize="30" Grid.Column="1" Grid.Row="0" 
                   HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                <Label x:Name="xian3" Text="自选糕点" Background="gray" FontSize="40" Grid.Column="2" Grid.Row="0" 
                   HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                <Label x:Name="xian4" Text="饺子" Background="gray" FontSize="50" Grid.Column="0" Grid.Row="1" 
                   HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                <Label x:Name="xian5" Text="烧卖类" Background="gray" FontSize="40" Grid.Column="1" Grid.Row="1" 
                   HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                <Label x:Name="xian6" Text="包子" Background="gray" FontSize="50" Grid.Column="2" Grid.Row="1" 
                   HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                <Label x:Name="xian7" Text="肠粉" Background="gray" FontSize="50" Grid.Column="0" Grid.Row="2" 
                   HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                <Label x:Name="xian8" Text="汤面、粉" Background="gray" FontSize="30" Grid.Column="1" Grid.Row="2" 
                   HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                <Label x:Name="xian9" Text="面饼类" Background="gray" FontSize="40" Grid.Column="2" Grid.Row="2" 
                   HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
            </Grid>

            <Label Text="如果不知道吃什么,就请点击下面开始按钮吧!" HorizontalOptions="Center"/>
            <Label Text="再点击停止即可" HorizontalOptions="Center"/>

            <AbsoluteLayout>
                <Button x:Name="button1" Text="开始" BackgroundColor="#EEE" AbsoluteLayout.LayoutBounds="70,30"
                    Clicked="Button_Clicked_1" CornerRadius="5"/>
                <Button x:Name="button2" Text="停止" BackgroundColor="#EEE" AbsoluteLayout.LayoutBounds="210,30"
                    Clicked="Button_Clicked_2" CornerRadius="5"/>
            </AbsoluteLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

这是Page1.xaml.cs的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace hope
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Page1 : ContentPage
    {
        System.Timers.Timer timer;
        System.Timers.Timer timer1;
        System.Timers.Timer timer2;
        System.Timers.Timer timer3;
        System.Timers.Timer timer4;
        System.Timers.Timer timer5;
        System.Timers.Timer timer6;
        System.Timers.Timer timer7;
        System.Timers.Timer timer8;
        System.Timers.Timer timer9;
        public enum RunState // 枚举一些类:开始和停止
        {
            running,
            stop
        }
        RunState state = RunState.stop; // 引用类,初始化为停止
        public Page1()
        {
            InitializeComponent();
            timer = new System.Timers.Timer(450);
            timer1 = new System.Timers.Timer(50);
            timer2 = new System.Timers.Timer(50);
            timer3 = new System.Timers.Timer(50);
            timer4 = new System.Timers.Timer(50);
            timer5 = new System.Timers.Timer(50);
            timer6 = new System.Timers.Timer(50);
            timer7 = new System.Timers.Timer(50);
            timer8 = new System.Timers.Timer(50);
            timer9 = new System.Timers.Timer(50);

            timer1.Elapsed += new System.Timers.ElapsedEventHandler(Tick1);
            timer2.Elapsed += new System.Timers.ElapsedEventHandler(Tick2);
            timer3.Elapsed += new System.Timers.ElapsedEventHandler(Tick3);
            timer4.Elapsed += new System.Timers.ElapsedEventHandler(Tick4);
            timer5.Elapsed += new System.Timers.ElapsedEventHandler(Tick5);
            timer6.Elapsed += new System.Timers.ElapsedEventHandler(Tick6);
            timer7.Elapsed += new System.Timers.ElapsedEventHandler(Tick7);
            timer8.Elapsed += new System.Timers.ElapsedEventHandler(Tick8);
            timer9.Elapsed += new System.Timers.ElapsedEventHandler(Tick9);
        }
        private void Runcolor(object sender, EventArgs e) // 颜色变动的函数(轮盘抽签)
        {

            timer.Enabled = false; // timer为这个函数的执行计时器,这里设关闭
            if (state == RunState.running) // 实现颜色变动的条件(开始条件)
            {
                xian1.BackgroundColor = Color.Yellow; // 设第一个框为黄色
                timer1.Enabled = true; // 执行第一个变框

                timer.Enabled = true; // 开启函数计时器
            }
        }

        public void Tick1(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (xian1.BackgroundColor == Color.Yellow)
            {
                xian1.BackgroundColor = Color.Gray;
                xian2.BackgroundColor = Color.Yellow;
            }
            timer1.Enabled = false;
            timer2.Enabled = true;

        }

        public void Tick2(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (xian2.BackgroundColor == Color.Yellow)
            {
                xian2.BackgroundColor = Color.Gray;
                xian3.BackgroundColor = Color.Yellow;
            }
            timer2.Enabled = false;
            timer3.Enabled = true;

        }

        public void Tick3(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (xian3.BackgroundColor == Color.Yellow)
            {
                xian3.BackgroundColor = Color.Gray;
                xian4.BackgroundColor = Color.Yellow;
            }
            timer3.Enabled = false;
            timer4.Enabled = true;

        }

        public void Tick4(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (xian4.BackgroundColor == Color.Yellow)
            {
                xian4.BackgroundColor = Color.Gray;
                xian5.BackgroundColor = Color.Yellow;
            }
            timer4.Enabled = false;
            timer5.Enabled = true;

        }

        public void Tick5(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (xian5.BackgroundColor == Color.Yellow)
            {
                xian5.BackgroundColor = Color.Gray;
                xian6.BackgroundColor = Color.Yellow;
            }
            timer5.Enabled = false;
            timer6.Enabled = true;

        }

        public void Tick6(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (xian6.BackgroundColor == Color.Yellow)
            {
                xian6.BackgroundColor = Color.Gray;
                xian7.BackgroundColor = Color.Yellow;
            }
            timer6.Enabled = false;
            timer7.Enabled = true;

        }

        public void Tick7(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (xian7.BackgroundColor == Color.Yellow)
            {
                xian7.BackgroundColor = Color.Gray;
                xian8.BackgroundColor = Color.Yellow;
            }
            timer7.Enabled = false;
            timer8.Enabled = true;

        }

        public void Tick8(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (xian8.BackgroundColor == Color.Yellow)
            {
                xian8.BackgroundColor = Color.Gray;
                xian9.BackgroundColor = Color.Yellow;
            }
            timer8.Enabled = false;
            timer9.Enabled = true;

        }

        public void Tick9(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (xian9.BackgroundColor == Color.Yellow)
            {
                xian9.BackgroundColor = Color.Gray;
            }
            timer9.Enabled = false;
        }

        private void Button_Clicked_1(object sender, EventArgs e)
        {
            button1.IsEnabled = false;
            button2.IsEnabled = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(Runcolor); // 委托执行Runcolor函数(每一时间间隔执行一次)
            timer.Enabled = true; // 开启计时器
            this.state = RunState.running; // 条件变为开始
        }
        string a = "";

        private void Button_Clicked_2(object sender, EventArgs e)
        {
            button1.IsEnabled = true;
            state = RunState.stop; // 设停止条件(与上面开始条件对应)
            this.timer.Enabled = false; // 关闭计时器
            if (timer1.Enabled == true) // 以下是直接停止中间的步骤(选中对应的框)
            {
                timer1.Enabled = false;
                a = "粥";
            }
            if (timer2.Enabled == true)
            {
                timer2.Enabled = false;
                a = "炒面、粉";
            }
            if (timer3.Enabled == true)
            {
                timer3.Enabled = false;
                a = "自选糕点";
            }
            if (timer4.Enabled == true)
            {
                timer4.Enabled = false;
                a = "饺子";
            }
            if (timer5.Enabled == true)
            {
                timer5.Enabled = false;
                a = "烧卖类";
            }
            if (timer6.Enabled == true)
            {
                timer6.Enabled = false;
                a = "包子";
            }
            if (timer7.Enabled == true)
            {
                timer7.Enabled = false;
                a = "肠粉";
            }
            if (timer8.Enabled == true)
            {
                timer8.Enabled = false;
                a = "汤面、粉";
            }
            if (timer9.Enabled == true)
            {
                timer9.Enabled = false;
                a = "面饼类";
            }

            this.DisplayAlert("恭喜你", "你今天的早餐是" + a + " , 有需要的可加杯豆浆或牛奶哦!", "我接受");
        }
    }
}

具体和之前的文章差不多,但由于一些控件的差异和引用的方法不同所以做了修改。

2、安卓应用修饰

        这里包含icon图标的添加和软件应用的名称和图标的修改。

这是添加icon图标,放在这个文件夹里就行了,之后的引用直接写名称就行。

 

 这是修改应用名和图标:

打开MainActivity.cs

using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.OS;

namespace hope.Droid
{
    // 修改这里,label是应用名,icon是图标
    [Activity(Label = "今天吃啥", Icon = "@drawable/xixi", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

这里的图标引用要相对路径,我们一般把软件应用需要的图片都放在drawable这个文件夹里,这里引用不要后缀名。

3、应用展示(一部分)

        

 

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Xamarin.Forms(移动应用)轮盘抽签软件(Android) 的相关文章

随机推荐

  • java list 默认排序_List集合排序(默认及自定义排序)

    一 java提供的默认list排序方法 主要代码 List list new ArrayList list add 王硕 list add 刘媛媛 list add 刘迪 list add 刘布 升序 Collections sort li
  • 数据类型与printf,scanf函数

    printf与scanf函数 printf函数是一个格式化输出函数 scanf 函数则是一个输入函数 其一般的调用格式 scanf 格式控制 地址列表 其中格式控制一般是双引号中间加一些字符 由 与格式符组成 用于控制输入的格式 比如 d
  • 内存条 udimm rdimm 等和 ECC 功能

    RDIMM registered DIMM Registered Dual In line Memory Module 带寄存器的双线内存模块 表示控制器输出的地址和控制信号经过Reg寄存后输出到DRAM芯片 控制器输出的时钟信号经过PLL
  • MySQL 查询语句大全

    目录 基础查询 直接查询 AS起别名 去重 复 查询 条件查询 算术运算符查询 逻辑运算符查询 正则表达式查询 模糊查询 范围查询 是否非空判断查询 排序查询 限制查询 分页查询 随机查询 分组查询 HAVING 高级查询 子查询 嵌套查询
  • vs显示行号

    工具 gt 选项 gt 文本编译器 gt 然后选择对应的编程语言 gt 在右侧 行号前的对勾打上确定即可
  • 【知识分享】Modbus通信协议详解

    一 协议 这里分两部分 Modbus和协议 首先什么是协议 百度解释下就是 意思是共同计议 协商 经过谈判 协商而制定的共同承认 共同遵守的文件 比如大学毕业找工作的时候 一般要签一份叫 三方协议 的 三方指自己 校方 企业 这份协议里规定
  • kali学习4——使用msfvenom制作exe木马

    使用msfvenom制作exe木马文件 1 msfvenom msfvenom是msfpayload msfencode的结合体 使用msfvenom制作木马的思路是 木马在目标机上执行后 向本机发送信息 而本机则需要开启监听状态 收到信息
  • 2021级新生个人训练赛第38场

    问题 A chicken 题目描述 小 x 非常喜欢小鸡翅 他得知 NSC 超市为了吸引顾客 举行了如下的活动 一旦有顾客在其他超市找到更便宜的小鸡翅 NSC 超市将免费送给顾客 1000g 小鸡翅 小 x 为了尽可能的省钱 走遍了各大超市
  • java Unreachable错误

    Unreachable code 错误 不可达代码 比如在循环的break或者return后面的代码就是不可达代码 因为执行它们之前就已经跳出循环了 只要把这段代码移到break return之前就好了 参考https blog csdn
  • np.mean()和np.std()函数

    一 np mean 函数定义 numpy mean a axis dtype out keepdims mean 函数功能 求取均值 经常操作的参数为axis 以m n矩阵举例 axis 不设置值 对 m n 个数求均值 返回一个实数 ax
  • kibana启动问题:Kibana server is not ready yet

    第一点 KB ES版本不一致 网上大部分都是这么说的 解决方法 把KB和ES版本调整为统一版本 第二点 kibana yml中配置有问题 通过查看日志 发现了Error No Living connections的问题 解决方法 将配置文件
  • Vue3通透教程【八】获取DOM、操作组件

    文章目录 写在前面 Vue2 ref 的使用 Vue3获取DOM Vue3操作组件 写在最后 写在前面 专栏介绍 凉哥作为 Vue 的忠实 粉丝输出过大量的 Vue 文章 应粉丝要求开始更新 Vue3 的相关技术文章 Vue 框架目前的地位
  • 【Linux】计算机操作系统和软硬件体系结构

    目录 1 冯诺依曼体系结构 1 1 中央处理器 CPU 2 操作系统 OS 2 1 操作系统的概念 2 2 操作系统的作用 2 3 操作系统如何进行管理 2 3 1 操作系统通过分级管理的方式 实现对整体的管理 2 3 2 管理的本质是对数
  • 如何使用chrome 浏览器自带截屏?

    1 ctrl shift c 2 ctrl shift p 3 输入 capture 4 选择capture full size screenshot 实现截取整个网页
  • enscape各种材质参数_它来了!Enscape专属素材库!

    ENSCAPE素材库 近两年风靡整个设计的渲染器 一款实时渲染软件Enscape 凭借入门低 成效快 效果逼真 从渲染界脱颖而出 用其他渲染器1 的时间渲出VRAY等老大哥级别90 的效果 作为建筑师的你会怎么选择呢 简单的分析了下渲染界最
  • MySQL connector/C++ 连接mysql效率低下解决

    这个问题 说解决也不算是被解决了 只能是让数据库插入的时候不会有像直接插入一样有那么多的问题了 我的解决方法是 开启mysql的事务 开始我也不知道是不是我的mysql配置优化的问题 WAMP统一安装 无限默认下一步的 在用PHP测试的时候
  • int与byte[]之间进行转换

    如何将int与byte 之间转换 int类型在内存中占4个字节 采用补码方式存储 而一个byte占一个字节 下面有两种方法进行转换 package cn fh vertxboot utils description int与Byte数组转换
  • 苹果app上架流程之傻瓜式教程剖析

    iOS开发者开发好一款APP之后 进行内测后没问题 下一步就是要上架AppStore了 一些开发者不知道该如何上架AppStore 下面 我们来说说iOS上架流程 以及如何快速上架AppStore 工具 1 iOS开发者账号 2 App U
  • Linux内存泄露案例分析和内存管理分享

    一 问题 近期我们运维同事接到线上LB 负载均衡 服务内存报警 运维同事反馈说LB集群有部分机器的内存使用率超过80 有的甚至超过90 而且内存使用率还再不停的增长 接到内存报警的消息 让整个团队都比较紧张 我们团队负责的LB服务是零售 物
  • Xamarin.Forms(移动应用)轮盘抽签软件(Android)

    该文章由我前面的文章https blog csdn net dabo 520 article details 129760956 spm 1001 2014 3001 5501改编而来 它是程序的核心 具体详细可自行前往观看 1 软件开发准