gpt4 book ai didi

c# - 评论我的简单 MVP Winforms 应用程序

转载 作者:可可西里 更新时间:2023-11-01 08:11:08 25 4
gpt4 key购买 nike

<分区>

我正在努力思考 C#/Winforms 应用程序中使用的 MVP 模式。所以我创建了一个简单的“记事本”之类的应用程序来尝试计算出所有的细节。我的目标是创建一些东西来执行打开、保存、新建等经典 Windows 行为,并在标题栏中反射(reflect)已保存文件的名称。此外,当有未保存的更改时,标题栏应包含一个 *。

所以我创建了一个 View 和一个管理应用程序持久状态的展示器。我考虑过的一项改进是分解文本处理代码,使 View /演示器真正成为一个单一用途的实体。

这是一个屏幕截图供引用...

alt text

我在下面包含了所有相关文件。我对关于我是否以正确的方式完成它或者是否有改进的方法的反馈感兴趣。

笔记模型.cs:

public class NoteModel : INotifyPropertyChanged 
{
public string Filename { get; set; }
public bool IsDirty { get; set; }
string _sText;
public readonly string DefaultName = "Untitled.txt";

public string TheText
{
get { return _sText; }
set
{
_sText = value;
PropertyHasChanged("TheText");
}
}

public NoteModel()
{
Filename = DefaultName;
}

public void Save(string sFilename)
{
FileInfo fi = new FileInfo(sFilename);

TextWriter tw = new StreamWriter(fi.FullName);
tw.Write(TheText);
tw.Close();

Filename = fi.FullName;
IsDirty = false;
}

public void Open(string sFilename)
{
FileInfo fi = new FileInfo(sFilename);

TextReader tr = new StreamReader(fi.FullName);
TheText = tr.ReadToEnd();
tr.Close();

Filename = fi.FullName;
IsDirty = false;
}

private void PropertyHasChanged(string sPropName)
{
IsDirty = true;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(sPropName));
}


#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

#endregion
}

Form2.cs:

public partial class Form2 : Form, IPersistenceStateView
{
PersistenceStatePresenter _peristencePresenter;

public Form2()
{
InitializeComponent();
}

#region IPersistenceStateView Members

public string TheText
{
get { return this.textBox1.Text; }
set { textBox1.Text = value; }
}

public void UpdateFormTitle(string sTitle)
{
this.Text = sTitle;
}

public string AskUserForSaveFilename()
{
SaveFileDialog dlg = new SaveFileDialog();
DialogResult result = dlg.ShowDialog();
if (result == DialogResult.Cancel)
return null;
else
return dlg.FileName;
}

public string AskUserForOpenFilename()
{
OpenFileDialog dlg = new OpenFileDialog();
DialogResult result = dlg.ShowDialog();
if (result == DialogResult.Cancel)
return null;
else
return dlg.FileName;
}

public bool AskUserOkDiscardChanges()
{
DialogResult result = MessageBox.Show("You have unsaved changes. Do you want to continue without saving your changes?", "Disregard changes?", MessageBoxButtons.YesNo);

if (result == DialogResult.Yes)
return true;
else
return false;
}

public void NotifyUser(string sMessage)
{
MessageBox.Show(sMessage);
}

public void CloseView()
{
this.Dispose();
}

public void ClearView()
{
this.textBox1.Text = String.Empty;
}

#endregion

private void btnSave_Click(object sender, EventArgs e)
{
_peristencePresenter.Save();
}

private void btnOpen_Click(object sender, EventArgs e)
{
_peristencePresenter.Open();
}

private void btnNew_Click(object sender, EventArgs e)
{
_peristencePresenter.CleanSlate();
}

private void Form2_Load(object sender, EventArgs e)
{
_peristencePresenter = new PersistenceStatePresenter(this);
}

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
_peristencePresenter.Close();
e.Cancel = true; // let the presenter handle the decision
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
_peristencePresenter.TextModified();
}
}

IPersistenceStateView.cs

public interface IPersistenceStateView
{
string TheText { get; set; }

void UpdateFormTitle(string sTitle);
string AskUserForSaveFilename();
string AskUserForOpenFilename();
bool AskUserOkDiscardChanges();
void NotifyUser(string sMessage);
void CloseView();
void ClearView();
}

PersistenceStatePresenter.cs

public class PersistenceStatePresenter
{
IPersistenceStateView _view;
NoteModel _model;

public PersistenceStatePresenter(IPersistenceStateView view)
{
_view = view;

InitializeModel();
InitializeView();
}

private void InitializeModel()
{
_model = new NoteModel(); // could also be passed in as an argument.
_model.PropertyChanged += new PropertyChangedEventHandler(_model_PropertyChanged);
}

private void InitializeView()
{
UpdateFormTitle();
}

private void _model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "TheText")
_view.TheText = _model.TheText;

UpdateFormTitle();
}

private void UpdateFormTitle()
{
string sTitle = _model.Filename;
if (_model.IsDirty)
sTitle += "*";

_view.UpdateFormTitle(sTitle);
}

public void Save()
{
string sFilename;

if (_model.Filename == _model.DefaultName || _model.Filename == null)
{
sFilename = _view.AskUserForSaveFilename();
if (sFilename == null)
return; // user canceled the save request.
}
else
sFilename = _model.Filename;

try
{
_model.Save(sFilename);
}
catch (Exception ex)
{
_view.NotifyUser("Could not save your file.");
}

UpdateFormTitle();
}

public void TextModified()
{
_model.TheText = _view.TheText;
}

public void Open()
{
CleanSlate();

string sFilename = _view.AskUserForOpenFilename();

if (sFilename == null)
return;

_model.Open(sFilename);
_model.IsDirty = false;
UpdateFormTitle();
}

public void Close()
{
bool bCanClose = true;

if (_model.IsDirty)
bCanClose = _view.AskUserOkDiscardChanges();

if (bCanClose)
{
_view.CloseView();
}
}

public void CleanSlate()
{
bool bCanClear = true;

if (_model.IsDirty)
bCanClear = _view.AskUserOkDiscardChanges();

if (bCanClear)
{
_view.ClearView();
InitializeModel();
InitializeView();
}
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com