c# - 我想在 C# 中使用 winform MVP 模式进行数字增量
问题描述
嗨,我正在练习在 C# 中使用 winform MVP 模式。
我制作了 Models、Presenters 和 Views 文件夹,每个类都有一个。(Models 有 Data.cs,Presenters 有 Datapresenter.cs,View 有 interface.cs 和 Form.cs)
我使用了“FlowLayoutPanel”。我制作了Label来制作数字。像这样。
到目前为止我的进步。
我制作的WinForm。
这是Data.cs(模型)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LayoutSample.Models
{
public class Data
{
public string label { get; set; }
public string CalculateArea()
{
return label;
}
}
}
这是 DataPresenter.cs(演示者)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using LayoutSample.Models;
using LayoutSample.Views;
namespace LayoutSample.Presenters
{
public class DataPresenter
{
IFlowLabel LabelView;
public DataPresenter(IFlowLabel view)
{
LabelView = view;
}
public void CalculateArea()
{
Data data = new Models.Data();
data.label = string.Copy(LabelView.label);
var th = new Thread(() =>
{
for ( int i = 1; i < 101; i++)
{
for (int j=1; j<101;j++)
{
Label label = new Label();
label.Text = j.ToString();
Console.WriteLine(label);
}
Thread.Sleep(1000);
}
});
th.Start();
}
}
}
这是 interface.cs(View)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LayoutSample.Views
{
public interface IFlowLabel
{
string label { get; set; }
}
}
这是 Form.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LayoutSample.Models;
using LayoutSample.Presenters;
using LayoutSample.Views;
namespace LayoutSample
{
public partial class Form1 : Form, IFlowLabel
{
public Form1()
{
InitializeComponent();
}
string IFlowLabel.label
{
get
{
return flowLayoutPanel1.ToString();
}
set
{
if (flowLayoutPanel1.InvokeRequired)
{
flowLayoutPanel1.Invoke(new MethodInvoker(() =>
{
flowLayoutPanel1.Text = value;
}));
}
else
{
flowLayoutPanel1.Text = value;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 100; i++)
{
Label label = new Label();
label.AutoSize = false;
label.Width = 50;
label.Text = i.ToString();
flowLayoutPanel1.Controls.Add(label);
}
DataPresenter presenter = new DataPresenter(this);
presenter.CalculateArea();
}
}
}
从这里开始,我想让数字增加。
我怎样才能同时增加它们?
我可以通过控制台看到数字增加,但我看不到 WimForm 的变化。如何将增量带到 WinForm 与控制台的结果相同?
解决方案
您不能增加数字,因为数字是硬编码的,并从下面的代码应用于标签。特别是 for 循环 i.ToString()。
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 100; i++)
{
Label label = new Label();
label.AutoSize = false;
label.Width = 50;
label.Text = i.ToString();
flowLayoutPanel1.Controls.Add(label);
}
DataPresenter presenter = new DataPresenter(this);
presenter.CalculateArea();
}
如果你想增加字符串包含的值,那么你必须对标签文本本身做一些事情,我不确定你在哪里增加值并在控制台中看到它。
label.Text = i.ToString() + addedValue;
如果标签被更改,请尝试调用表单刷新方法。
推荐阅读
- python - Python中的决策树桩
- google-bigquery - 通过命令提示符/计划任务验证 bigquery 是否成功上传
- javascript - JS(jquery)自动搜索不拉 MySQL 数据
- c# - 如何加快 WinForms 中的自动完成
- c# - ASP.NET Core 和 Visual Studio 中的模型类
- javascript - 尝试从 API 解析和存储 JSON 数据时如何知道目标是什么
- docker - 在 GitLab 中使用 ElasticSearch,如果 ES Container 宕机了怎么办?
- android - 无法解析“:app@debug/compileClasspath”的依赖关系:无法解析com.android.volley:volley:1.1.1
- python - Python 2d 数组布尔减少
- ssis - 如何通过 SSIS 将所有表从 ODBC 源移动到 Access?