Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 适当形式的应用程序设计_C#_Winforms - Fatal编程技术网

C# 适当形式的应用程序设计

C# 适当形式的应用程序设计,c#,winforms,C#,Winforms,我正在用C#创建一个WinForm应用程序,它的一个功能是在文本框中显示文本。我正在为查询单独类中的数据库的逻辑编写代码,但无法访问我创建的类中的文本框元素(我得到的“名称”在当前上下文错误中不存在)。我是否将所有表单逻辑都放入Form1.cs文件中?如果没有,请尝试将属性中的修饰符设置为public或internal 编辑-编辑为适合答案格式如果要访问另一个类中的文本框,请将访问修饰符更改为 公共或内部(如果在同一程序集中) 。 默认情况下,它将是私有的 更好的方法是将值传递给业务逻辑层,而不

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

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


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

如果要访问另一个类中的文本框,请将访问修饰符更改为

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

。 默认情况下,它将是私有的

更好的方法是将值传递给业务逻辑层,而不是整个控件,这并不总是好的


B.I
是做所有的业务,所以文本框的值足够了。

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

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

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

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

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

我们目前正在winforms中开发一个具有MVP模式的应用程序。我们在winforms中使用绑定,因此当数据更新时,UI将更新。我们的表单使用绑定源和绑定列表。我们将主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);
        }

    }
}
表单文本框将绑定到演示者中的属性,任何列表框或组合框都将绑定到绑定列表。
然后,我们对模型使用Linq到Sql。表格中几乎没有逻辑。大部分只是验证所需的一点。

是否通过“修饰符”字段设置?如果是这样,我尝试将“修饰符”设置为public和internal,但两者都不起作用……我是OOP的新手,您能解释一下业务逻辑层是什么意思吗?业务逻辑是否在单独的类中,GUI逻辑是否在表单文件中?
   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");
                               }
                          }
                     }