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

该文章由我前面的文章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、应用展示(一部分)

        

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值