繁体   English   中英

适当的表格申请设计

[英]Proper Form Application Design

我正在用C#创建WinForm应用程序,其功能之一是在文本框中显示文本。 我正在编码用于在单独的类中查询数据库的逻辑,并且无法访问正在创建的类中的文本框元素(当前上下文错误中不存在“名称”)。 是否将所有表单逻辑都放入Form1.cs文件中?

您应该尝试将显示逻辑与应用程序的其余部分分开-最简单的操作是让表单类句柄获取/设置表单值。 这意味着您的数据访问组件将查询数据库,并且表单必须将输出映射到可以显示的内容,例如

public class Form1 : Form
{
    public DataAccess Db { get; set; }

    public void UpdateSomething()
    {
        this.textbox.Text = this.Db.GetSomeDatabaseValue();
    }
}

不能将业务逻辑与UI逻辑分开。 您应该在Business类中引发一个事件,并在UI表单中捕获该事件。 从那里显示它。

如果不是,请尝试将属性中的修饰符设置为public或internal。

编辑-编辑以适合答案格式

如果要访问另一个类中的TextBox,则将access修饰符更改为

公共的或内部的(如果在同一程序集中)

默认它将是私有的

更好的是,您可以将值传递给业务逻辑层。不是整个控件,总会不好。

BI将负责所有事务,因此文本框的价值就足够了。

你看过背景工作者了吗? 这样,您可以在单击表单上的按钮时异步运行事物。 您在表单上所做的所有更新操作都将在表单本身上完成。 您的其他代码(您无法从中访问Form1)将“报告进度”。 报告进度时,可以将所需的任何对象发送到Form1,然后在表单上的事件处理程序中,可以从该对象获取信息并更新视图。 例如,您可以使用它来更新进度条,同时保持UI响应。

我们目前正在使用Winforms中具有MVP模式的应用程序。 我们在winforms中使用绑定,因此UI会在数据更新时更新。 我们的表单使用BindingSources和BindingLists。 我们将主BindingSource绑定到presenter类。

后面的表单代码示例

   using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SomeNameSpace.Utilities.UI;
using SomeNameSpace.Utilities.Validation;

namespace Management.UI
{
    public partial class ManualControl : UserControl, IManualRaffleView
    {


        private ManualPresenter _presenter;

        public ManualControl()
        {
            InitializeComponent();
        }


        [Browsable(false)]
        public ManualPresenter Presenter
        {
            get
            {
                return _presenter;
            }
            set
            {
                _presenter = value;
                if(_presenter != null)
                {
                    _manualPresenterBindingSource.DataSource = _presenter;
                    _ListBindingSource.DataSource = _presenter;
                    _ListBindingSource.DataMember = "Something";
                    _KindListBindingSource.DataSource = _presenter;
                    _KindListBindingSource.DataMember = "SomethingElse";

                    _presenter.CloseView += new Action(CloseMe);
                    _presenter.VerifyingCancel += new Func<bool>(VerifyingCancel);
                    _presenter.Showing += new Action(ShowMe);
                    _presenter.Synchronizer = this;
                }
            }
        }


        void CloseMe()
        {
            this.Enabled = false;
        }

        private void ManualRaffleForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = false;
        }

        private void ShowMe()
        {
            this.Enabled = true;
        }

        bool VerifyingCancel()
        {
            return MessageBox.Show("Cancel?", Notifier.ApplicationName,
                MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button2) == DialogResult.Yes;
        }

        private void _cancelButton_Click(object sender, EventArgs e)
        {
            Presenter.HandleCancel();
        }

        private void _initiateButton_Click(object sender, EventArgs e)
        {
            Presenter.HandleInitiate();
        }

        private void _saveButton_Click(object sender, EventArgs e)
        {
            if(Presenter.Error == true.ToString())
                Presenter.HandleDone();
            else
                _manualPresenterBindingSource.ResetBindings(false);
        }

    }
}

然后我们的演示者实现INotifyPropertyChanged,可能看起来像这样

   namespace SomeCompany.UI
    {
    public class ManualPresenter : INotifyPropertyChanged, IDataErrorInfo
        {
            #region Fields
                    //fields
                    #endregion Fields

                    public string SomeFormField
                    { get{ return _someFormField;}
                      set{
                            if(_someFormField != value)
                              {
                                 _someFormField = value;
                                  //Update Model if Needed
                                  _model.SomeFormField = _someFormField;
                                 NotifyCHanged("SomeFormField");
                               }
                          }
                     }

表单文本框将绑定到演示者中的属性,所有列表框或组合框都将绑定到BindingList。 然后,我们将Linq to Sql用于我们的模型。 表格中的逻辑很少。 通常,验证所需的只是一点点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM