定时器
namespace AlarmClock
{
public partial class Main : Form
{
// 定时器刚启动时的秒数
private int secondStart = 0;
public Main()
{
InitializeComponent();
}
//定时器事件
private void timer1_Tick(object sender, EventArgs e)
{
// 需求:每间隔1秒钟,判断是否已经到休息时间,如果已经到休息时间,
// 需要打开新窗体提醒休息一会。
int minute = (int)numericUpDown1.Value;
int second = minute * 60;
secondStart++;
if (secondStart >= second) // 休息时间到
{
// 到达休息时间时,把定时器停止。timer1_Tick事件不再执行。
button2_Click(sender, e);
// 隐藏Main窗体
Hide();
// 拿到Tip窗体的实例,参数1:工作分钟数,参数2:Main窗体
Tip tipWindow = new Tip(minute, this);
// 打开窗体
tipWindow.ShowDialog();
}
}
//启动闹钟
public void button1_Click(object sender, EventArgs e)
{
secondStart = 0; // 每次启动闹钟时,把走过的秒数重置。
button2.Enabled = true; // 停止闹钟按钮能用
timer1.Enabled = true; // 让定时器能用。
timer1.Start(); // 定时器启动,前提定时必须能用Enabled=true;
button1.Enabled = false;
}
//停止闹钟
private void button2_Click(object sender, EventArgs e)
{
button1.Enabled = true;
timer1.Stop(); // 定时器停止,前提定时必须能用Enabled=true;
button2.Enabled = false;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AlarmClock
{
public partial class Tip : Form
{
// 10秒后,关闭此窗体
int secondClose = 10;
// 工作的的分钟数
int minute = 0;
// 音频的路径
static string path = Application.StartupPath + "/alarm.wav";
// 播放器
SoundPlayer sp = new SoundPlayer(path);
// 存储一下Main窗体
Main? mainForm = null;
public Tip(int min, Main main)
{
InitializeComponent();
minute = min; // 把Main窗体中的工作分钟数存储起来,将来在其他事件中使用。
mainForm = main; // 把Main窗体存储起来,将来在其他事件中使用。
}
//tip定时器事件
private void timer1_Tick(object sender, EventArgs e)
{
secondClose--;
label2.Text = $"{secondClose}秒后自动关闭";
if (secondClose <= 0)
{
timer1.Stop(); // 定时器停止
sp.Stop(); // 音频停止
Close(); // Tip窗口关闭
// 把Main窗体再显示出来
// mainForm?.Show();
if (mainForm != null)
{
mainForm.Show();
}
}
}
//TIP窗体加载时
private void Tip_Load(object sender, EventArgs e)
{
label1.Text = $"您已经工作{minute}分钟了,休息一会!";
label2.Text = $"{secondClose}秒后自动关闭";
timer1.Start();
// 播放音频
sp.Play();
}
}
}
MVC
MVC基本原理
C# MVC (Model-View-Controller)是一种常用的Web应用程序设计模式,
它将应用程序分为三个主要部分:模型(Model)、视图(View)和控制器(Controller)。
下面是C# MVC的基本原理
1、模型(Model)
模型负责处理应用程序的数据和业务逻辑。它包含了数据的定义、数据访问和业务规则的实现。模型通常由实体类组成,用于表示应用程序中的数据结构。模型不会直接依赖于视图或控制器,它只关注数据的处理和逻辑的执行。
2、视图(View)
视图负责展示模型中的数据,并向用户呈现应用程序的界面。视图通常由HTML、CSS和一些视图引擎((如Razor)等技术来构建。视图通过使用模型中的数据来生成动态内容,并将其呈现给用户。视图不处理数据的传递和处理,它只负责展示数据。
3、控制器(Controller)
控制器是MVC模式中的核心部分,它负责接收用户的请求,并根据请求选择相应的模型和视图进行处理。控制器接收用户输入,并根据输入调用相应的模型处理数据和业务逻辑,然后选择合适的视图来将数据呈现给用户。控制器充当模型和视图的协调者,使它们之间的交互变得简单和有序。
MVC工作流程
1)用户发送请求到应用程序的特定URL。
2)路由器(Router)根据URL解析并确定应该由哪个控制器处理该请求。
3)控制器接收请求,并根据请求调用相应的模型进行数据处理和业务逻辑操作。4)模型处理完数据和逻辑后,将结果返回给控制器。
Model(模型)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//MVC 设计模式在 WinForms 应用程序中的实现通常涉及将应用程序的不同部分(模型、视图、控制器)分离开来
//模型(Model): 模型代表应用程序的数据和业务逻辑。
namespace ZhiYou_winform_mvc17.Model
{
internal class People
{
public string Name { get; set; }
public string Age { get; set; }
public void PeopleModleDoSomething()
{
//业务逻辑 比如json数据解析
Console.WriteLine(Name);
Console.WriteLine(Age);
}
}
}
VIew(视图)
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ZhiYou_winform_mvc17
{ //视图(View): 视图是用户界面。显示控件信息的
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 更新视图的方法
public void UpdateView(string name,string age)
{
this.label1.Text = name;
this.label2.Text = age;
}
}
}
Controller(控制器)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZhiYou_winform_mvc17.Model;
namespace ZhiYou_winform_mvc17.Control
{
// 控制器(Controller): 控制器负责在模型和视图之间传递信息
internal class Form1Control
{
//model对象
private People model;
//view对象
private Form1 view;
//重载构造函数
public Form1Control(People model, Form1 view)
{
this.model = model;
this.view = view;
// 初始化时将视图控件的事件绑定
HookUpEvents();
}
// 初始化时将视图控件的事件绑定
public void HookUpEvents()
{
this.view.button1.Click += button1_Click;
}
//button事件
private void button1_Click(object sender, EventArgs e)
{
this.model.PeopleModleDoSomething();
//触发点击事件 更新试图
this.view.UpdateView(model.Name, model.Age);
}
}
}
入口类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZhiYou_winform_mvc17.Control;
using ZhiYou_winform_mvc17.Model;
namespace ZhiYou_winform_mvc17
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//创建model对象
People people = new People() { Name = "zhangsan", Age = "18" };
//创建窗体对象
Form1 form1 = new Form1();
//创建control对象
Form1Control fc = new Form1Control(people,form1);
Application.Run(form1);
}
}
}