C#-MDI-多窗口通讯格局计算

总结C#中窗体间传递数据的两种办法 (由外人的法子整理)

转自:http://blog.csdn.net/daisy423xu/article/details/1579854

 
在编写C#windows应用程序的时候大家平日会赶上这种问题,怎么着在三个窗体间传递数据呢?以下是本身收拾的网上的各样格局,在遇见一个实际问题:在form1中开辟一个form2窗口作为录入界面,将录入的值经转换后在form1中突显。
采取了寄托的方法,可以兑现。(与VC的回调的应用一般)

1.可以通过委托的法门来化解

题材:通过form1做一个录入界面,将其中通过文本框录入的数值复值给
form2中的listview各列,用3个textbox1.text比方吧,分别对应listview的3个列。

能够那样做,假设八个窗体是在同一个命名空间下

概念一个代理,注意这一个代理是大局的:(即一律命名空间下,与Form1,Form2平级的)

public delegate void MyInvoke(string Item1,string Item2,string Item3);

 

//在窗体From2中有那样一个回调函数,用于在ListView里添加一个新项的:

private void UpdateListView(string Item1,string Item2,string Item3)

{

    ListView1.Items.Add(Item1);

    ListView1.Items[ListView1.Items.Count – 1].SubItems.Add(Item2);

    ListView1.Items[ListView1.Items.Count – 1].SubItems.Add(Item3);

}

 

//比如说点击Form2的一个按钮弹出Form1进行录入,在点击按钮的事件下:

//把信托传过去

Form1 frmEdit=new Form1(new MyInvoke(UpdateListView));

frmEdit.ShowDialog(this);

 

//在Form1里定义一个特性

private MyInvoke mi=null;

 

在构造函数中收到那么些委托:

public Form1(MyInvoke myInvoke)

{

   this.mi=myInvoke;

}

 

//录入数据后,点击OK按钮,在点击事件下:

//回调

this.mi(this.TextBox1.Text,this.TextBox3.Text,this.TextBox3.Text);

this.Close();//关闭Form1

补偿:要是我要是想再把form2的值给form1,

Form1 frmEdit=new Form1(new MyInvoke(UpdateListView),string para1,string
para2…);

frmEdit.ShowDialog(this);

接下来将Form1的构造函数改成可以收到几个参数的就行了。

 

2.要是主框架为Form1,打开的摸索对话框是Form2.平素在Form2类中表贝拉米(Nutrilon)个Form1实例:Form1
f1=new
Form1();然后就可以透过f1来调用Form1中的域和函数了。其实不是那样的,你发明的新的Form1实例不是原来的充裕Form1对象了,那样操作的是新的Form1中的域和函数,和第一打开的Form1是从未涉及的。

俺们要做的是把近年来的Form1实例传递给Form2,假设是那样的话,问题就很好解决了。

方法1:首先,我们在Form2中定义:

private Form1 mF_Form

俺们改变Form2的构造函数为有参数的

public Form2 ( Form1 myForm )
{
//
// Windows 窗体设计器援救所须要的
//
InitializeComponent ( ) ;
this.mF_Form  = myForm ;  
/////那样在Form1中表明Form2的时候就把Form1的实例传递过来了
//
// TODO: 在 InitializeComponent 调用后添加其他构造函数代码
//
}

在Form1中,我在 要用到Form2的地方声明如下:

Form2 f2=new 
Form2(this);////那里的this指的就是Form1当前的实例,也就是把当前Form1的实例通过Form2的构造函数传递给Form2类(其实在网上看看过相比较蠢的办法,就是在构造函数里面传递要传送的新闻如:字符串或是数字等,这样做很有局限性,无法传递其余的,所有我们可以直接传送实例,来达成传递更加多的音信。)

那般在Form2中选拔myForm 就可以对本来的Form1窗口举行操作了。然而你要把要操作的Form1中的域和函数定义成public形式的(如此那般或许不安全),此时的myForm就是确实的最起始打开的Form1了,你可以用这一个实例来进行多少个窗体的通信了。
()

3.其实C#中提供了窗体间开展广播公布的现成的性质,呵呵,大家能想到的,微软也想开了,他们创制的言语其实确实可以说是人性化了。

在Form1类中申明Form2时用如下代码:

Form2 f2=new Form2();//////类Form2中的构造函数不改,如故无参的

f2.owner=this;////那里的this指的是类Form1当前的实例。

//也得以应用函数的主意,给当下实例添加一个专属窗口 
代码:this.AddOwnedForm(f2);

在Form2类的概念中写如下代码:

Form1 f1=this.owner;

如此那般f1对应的就是原来的Form1的实例了,也就可以用那么些举行报导了。不过依然要把差异类之间访问的域和函数定义成public,哎,安全的确是一个题目!!

 

  4.施用静态类
    这一个也是我们平时要用到的一种多少交互形式。

下边是概念的一个类:

using System;

using System.Collections;

namespace ZZ

{

     public class AppDatas

     {

         private static ArrayList listData;

         static AppDatas()

         {

              listData = new ArrayList();

              listData.Add(“DotNet”);

              listData.Add(“C#”);

              listData.Add(“Asp.net”);

              listData.Add(“WebService”);

              listData.Add(“XML”);

         }

         public static ArrayList ListData

         {

              get{return listData;}

         }

         public static ArrayList GetListData()

         {

              return listData;

         }

     }

}

下边包蕴了一个静态类成员,listData,一个静态构造函数static
AppDatas(),用来早先化listData的多寡。还有一个静态属性ListData和一个静态GetListData()方法,他们落到实处了一致的职能就是重返listData。

是因为前边两篇小说已经讲了不少,这里不细说了,上边是总体的代码:

Form1.cs文件

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

namespace ZZ

{

     public class Form1 : System.Windows.Forms.Form

     {

         private System.Windows.Forms.Button buttonEdit;

         private System.Windows.Forms.ListBox listBoxFrm1;

         private System.ComponentModel.Container components = null;

         public Form1()

         {

              InitializeComponent();

              this.listBoxFrm1.DataSource = AppDatas.ListData;

             

         }

         protected override void Dispose( bool disposing )

         {

              if( disposing )

                   if(components != null)

                       components.Dispose();

              base.Dispose( disposing );

         }

         [STAThread]

         static void Main()

         {

              Application.Run(new Form1());

         }

         private void InitializeComponent()

         {

              this.buttonEdit = new System.Windows.Forms.Button();

              this.listBoxFrm1 = new System.Windows.Forms.ListBox();

              this.SuspendLayout();

              this.buttonEdit.Location = new System.Drawing.Point(128,
108);

              this.buttonEdit.Name = “buttonEdit”;

              this.buttonEdit.TabIndex = 1;

              this.buttonEdit.Text = “修改”;

              this.buttonEdit.Click += new
System.EventHandler(this.buttonEdit_Click);

              this.listBoxFrm1.ItemHeight = 12;

              this.listBoxFrm1.Location = new System.Drawing.Point(12,
8);

              this.listBoxFrm1.Name = “listBoxFrm1”;

              this.listBoxFrm1.Size = new System.Drawing.Size(108, 124);

              this.listBoxFrm1.TabIndex = 2;

              this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

              this.ClientSize = new System.Drawing.Size(208, 141);

              this.Controls.Add(this.listBoxFrm1);

              this.Controls.Add(this.buttonEdit);

              this.Name = “Form1”;

              this.Text = “Form1”;

              this.ResumeLayout(false);

         }

         private void buttonEdit_Click(object sender, System.EventArgs
e)

         {

              Form2 formChild = new Form2();

              formChild.ShowDialog();

              this.listBoxFrm1.DataSource = null;

              this.listBoxFrm1.DataSource = AppDatas.ListData;

         }

     }

}

 

Form2.cs文件

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

namespace ZZ

{

     public class Form2 : System.Windows.Forms.Form

     {

         private System.Windows.Forms.Button buttonOK;

         private System.ComponentModel.Container components = null;

         private System.Windows.Forms.ListBox listBoxFrm2;

         private System.Windows.Forms.Button buttonAdd;

         private System.Windows.Forms.Button buttonDel;

         private System.Windows.Forms.TextBox textBoxAdd;

         public Form2()

         {

              InitializeComponent();

              foreach(object o in AppDatas.ListData)

                   this.listBoxFrm2.Items.Add(o);

         }

         protected override void Dispose( bool disposing )

         {

              if( disposing )

                   if(components != null)

                       components.Dispose();

              base.Dispose( disposing );

         }

         private void InitializeComponent()

         {

              this.buttonOK = new System.Windows.Forms.Button();

              this.listBoxFrm2 = new System.Windows.Forms.ListBox();

              this.buttonAdd = new System.Windows.Forms.Button();

              this.buttonDel = new System.Windows.Forms.Button();

              this.textBoxAdd = new System.Windows.Forms.TextBox();

              this.SuspendLayout();

              this.buttonOK.Location = new System.Drawing.Point(188,
108);

              this.buttonOK.Name = “buttonOK”;

              this.buttonOK.TabIndex = 0;

              this.buttonOK.Text = “确定”;

              this.buttonOK.Click += new
System.EventHandler(this.buttonOK_Click);

              this.listBoxFrm2.ItemHeight = 12;

              this.listBoxFrm2.Location = new System.Drawing.Point(8,
8);

              this.listBoxFrm2.Name = “listBoxFrm2”;

              this.listBoxFrm2.Size = new System.Drawing.Size(168, 124);

              this.listBoxFrm2.TabIndex = 2;

              this.buttonAdd.Location = new System.Drawing.Point(188,
44);

              this.buttonAdd.Name = “buttonAdd”;

              this.buttonAdd.TabIndex = 3;

              this.buttonAdd.Text = “增加”;

              this.buttonAdd.Click += new
System.EventHandler(this.buttonAdd_Click);

              this.buttonDel.Location = new System.Drawing.Point(188,
76);

              this.buttonDel.Name = “buttonDel”;

              this.buttonDel.TabIndex = 4;

              this.buttonDel.Text = “删除”;

              this.buttonDel.Click += new
System.EventHandler(this.buttonDel_Click);

              this.textBoxAdd.Location = new System.Drawing.Point(188,
12);

              this.textBoxAdd.Name = “textBoxAdd”;

              this.textBoxAdd.Size = new System.Drawing.Size(76, 21);

              this.textBoxAdd.TabIndex = 5;

              this.textBoxAdd.Text = “”;

              this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

              this.ClientSize = new System.Drawing.Size(272, 141);

              this.Controls.Add(this.textBoxAdd);

              this.Controls.Add(this.buttonDel);

              this.Controls.Add(this.buttonAdd);

              this.Controls.Add(this.listBoxFrm2);

              this.Controls.Add(this.buttonOK);

              this.Name = “Form2”;

              this.Text = “Form2”;

              this.ResumeLayout(false);

         }

         private void buttonOK_Click(object sender, System.EventArgs
e)
         {
              this.Close();
         }

         private void buttonAdd_Click(object sender, System.EventArgs
e)

         {

              if(this.textBoxAdd.Text.Trim().Length>0)

              {

                   AppDatas.ListData.Add(this.textBoxAdd.Text.Trim());

                  
this.listBoxFrm2.Items.Add(this.textBoxAdd.Text.Trim());

              }

              else

                   MessageBox.Show(“请输入添加的内容!”);

             

         }

         private void buttonDel_Click(object sender, System.EventArgs
e)

         {

              int index = this.listBoxFrm2.SelectedIndex;

              if(index!=-1)

              {

                    AppDatas.ListData.RemoveAt(index);

                   this.listBoxFrm2.Items.RemoveAt(index);

              }

              else

                   MessageBox.Show(“请选用删除项!”);

         }

     }

}    

    
计算,我觉得利用静态类相比多的地方就是把应用程序的计划文件装载到一个静态类里面,让抱有的窗体和任何实例都得以经过静态属性以及静态方法使用那一个数据,比如三层协会或多层协会都可以访问它,而不是在五个实例间传来传去。在此间大家谈论的是Windows窗体,其实在多少个例外的实例间互动数据,都可以动用三篇小说中的方案完成,除非是其一类特有的习性或着艺术。现在都讲完了,固然不是怎么着奥秘的东西,可是希望能对一些初学者有所援救,同时也欢迎各位朋友进行技术调换,共同升高。

 

 

解析上边三种方式:

1. 
应用了寄托的主意,可以达成。:很好的兑现了数量处理与数量显示的分手,即FORM2(主)呈现与FORM1数据处理,(不须求将FORM2的浮出现处FORM1中)与VC的回调的使用有可持续性。并且有限支撑了FORM1中要修改的性能的私有性。

 

2. 
方法2、3都是传递主窗口的引用,比较不难易用。可以兑现FORM2(主)与FORM1所有数据的传递(不过需求将要FORM1传递和要修改的多寡设为PUBLIC),而如此会存在安全问题。

 

信托方法可以很好地落实数据的护卫

 

总结C#中窗体间传递数据的两种方法(由别人的主意整理)

 

 
在编写C#windows应用程序的时候大家平常会遇到那种题材,怎么样在四个窗体间传递数据呢?以下是自家收拾的网上的各类法子,在遇见一个事实上问题:在form1中开辟一个form2窗口作为录入界面,将录入的值经转换后在form1中显得。
采取了寄托的主意,可以已毕。(与VC的回调的行使一般)

1.得以经过信托的不二法门来化解

题材:通过form1做一个录入界面,将中间通过文本框录入的数值复值给
form2中的listview各列,用3个textbox1.text比方吧,分别对应listview的3个列。

可以那样做,如果多个窗体是在同一个命名空间下

概念一个代理,注意那一个代理是大局的:(即一律命名空间下,与Form1,Form2平级的)

public delegate void MyInvoke(string Item1,string Item2,string Item3);

 

//在窗体From2中有那般一个回调函数,用于在ListView里添加一个新项的:

private void UpdateListView(string Item1,string Item2,string Item3)

{

    ListView1.Items.Add(Item1);

    ListView1.Items[ListView1.Items.Count – 1].SubItems.Add(Item2);

    ListView1.Items[ListView1.Items.Count – 1].SubItems.Add(Item3);

}

 

//比如说点击Form2的一个按钮弹出Form1举办录入,在点击按钮的风云下:

//把信托传过去

Form1 frmEdit=new Form1(new MyInvoke(UpdateListView));

frmEdit.ShowDialog(this);

 

//在Form1里定义一个性能

private MyInvoke mi=null;

 

在构造函数中吸纳这么些委托:

public Form1(MyInvoke myInvoke)

{

   this.mi=myInvoke;

}

 

//录入数据后,点击OK按钮,在点击事件下:

//回调

this.mi(this.TextBox1.Text,this.TextBox3.Text,this.TextBox3.Text);

this.Close();//关闭Form1

补给:如若自身假若想再把form2的值给form1,

Form1 frmEdit=new Form1(new MyInvoke(UpdateListView),string para1,string
para2…);

frmEdit.ShowDialog(this);

然后将Form1的构造函数改成可以选拔多少个参数的就行了。

 

2.即使主框架为Form1,打开的检索对话框是Form2.直接在Form2类中阐多美滋(Meadjohnson)个Form1实例:Form1
f1=new
Form1();然后就可以通过f1来调用Form1中的域和函数了。其实不是这么的,你发明的新的Form1实例不是原本的要命Form1对象了,那样操作的是新的Form1中的域和函数,和第一打开的Form1是没有涉嫌的。

我们要做的是把当前的Form1实例传递给Form2,固然是那样的话,问题就很好解决了。

方法1:首先,我们在Form2中定义:

private Form1 mF_Form

大家转移Form2的构造函数为有参数的

public Form2 ( Form1 myForm )
{
//
// Windows 窗体设计器帮衬所必备的
//
InitializeComponent ( ) ;
this.mF_葡京网上娱乐场,Form  = myForm ;  
/////那样在Form1中阐明Form2的时候就把Form1的实例传递过来了
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

在Form1中,我在 要用到Form2的地方注解如下:

Form2 f2=new 
Form2(this);////那里的this指的就是Form1当前的实例,也就是把当下Form1的实例通过Form2的构造函数传递给Form2类(其实在网上来看过相比蠢的措施,就是在构造函数里面传递要传递的音讯如:字符串或是数字等,那样做很有局限性,无法传递其余的,所有大家可以直接传送实例,来形成传递越多的音讯。)

如此那般在Form2中动用myForm 就可以对原本的Form1窗口进行操作了。不过你要把要操作的Form1中的域和函数定义成public形式的(如此可能不安全),此时的myForm就是真的的最初叶打开的Form1了,你能够用那么些实例来举行四个窗体的报道了。
()

3.其实C#中提供了窗体间展开广播公布的现成的性能,呵呵,大家能想到的,微软也想到了,他们创制的言语其实真的可以说是人性化了。

在Form1类中评释Form2时用如下代码:

Form2 f2=new Form2();//////类Form2中的构造函数不改,照旧无参的

f2.owner=this;////那里的this指的是类Form1当前的实例。

//也可以使用函数的不二法门,给当下实例添加一个直属窗口 
代码:this.AddOwnedForm(f2);

在Form2类的概念中写如下代码:

Form1 f1=this.owner;

这么f1对应的就是原先的Form1的实例了,也就可以用这么些进行报纸揭橥了。可是仍然要把不相同类之间访问的域和函数定义成public,哎,安全的确是一个问题!!

 

  4.应用静态类
    这些也是大家平日要用到的一种多少交互方式。

上边是概念的一个类:

using System;

using System.Collections;

namespace ZZ

{

     public class AppDatas

     {

         private static ArrayList listData;

         static AppDatas()

         {

              listData = new ArrayList();

              listData.Add(“DotNet”);

              listData.Add(“C#”);

              listData.Add(“Asp.net”);

              listData.Add(“WebService”);

              listData.Add(“XML”);

         }

         public static ArrayList ListData

         {

              get{return listData;}

         }

         public static ArrayList GetListData()

         {

              return listData;

         }

     }

}

地点包括了一个静态类成员,listData,一个静态构造函数static
AppDatas(),用来伊始化listData的数目。还有一个静态属性ListData和一个静态GetListData()方法,他们完结了一如既往的效果就是回来listData。

由于前边两篇小说已经讲了无数,这里不细说了,上边是完整的代码:

Form1.cs文件

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

namespace ZZ

{

     public class Form1 : System.Windows.Forms.Form

     {

         private System.Windows.Forms.Button buttonEdit;

         private System.Windows.Forms.ListBox listBoxFrm1;

         private System.ComponentModel.Container components = null;

         public Form1()

         {

              InitializeComponent();

              this.listBoxFrm1.DataSource = AppDatas.ListData;

             

         }

         protected override void Dispose( bool disposing )

         {

              if( disposing )

                   if(components != null)

                       components.Dispose();

              base.Dispose( disposing );

         }

         [STAThread]

         static void Main()

         {

              Application.Run(new Form1());

         }

         private void InitializeComponent()

         {

              this.buttonEdit = new System.Windows.Forms.Button();

              this.listBoxFrm1 = new System.Windows.Forms.ListBox();

              this.SuspendLayout();

              this.buttonEdit.Location = new System.Drawing.Point(128,
108);

              this.buttonEdit.Name = “buttonEdit”;

              this.buttonEdit.TabIndex = 1;

              this.buttonEdit.Text = “修改”;

              this.buttonEdit.Click += new
System.EventHandler(this.buttonEdit_Click);

              this.listBoxFrm1.ItemHeight = 12;

              this.listBoxFrm1.Location = new System.Drawing.Point(12,
8);

              this.listBoxFrm1.Name = “listBoxFrm1”;

              this.listBoxFrm1.Size = new System.Drawing.Size(108, 124);

              this.listBoxFrm1.TabIndex = 2;

              this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

              this.ClientSize = new System.Drawing.Size(208, 141);

              this.Controls.Add(this.listBoxFrm1);

              this.Controls.Add(this.buttonEdit);

              this.Name = “Form1”;

              this.Text = “Form1”;

              this.ResumeLayout(false);

         }

         private void buttonEdit_Click(object sender, System.EventArgs
e)

         {

              Form2 formChild = new Form2();

              formChild.ShowDialog();

              this.listBoxFrm1.DataSource = null;

              this.listBoxFrm1.DataSource = AppDatas.ListData;

         }

     }

}

 

Form2.cs文件

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

namespace ZZ

{

     public class Form2 : System.Windows.Forms.Form

     {

         private System.Windows.Forms.Button buttonOK;

         private System.ComponentModel.Container components = null;

         private System.Windows.Forms.ListBox listBoxFrm2;

         private System.Windows.Forms.Button buttonAdd;

         private System.Windows.Forms.Button buttonDel;

         private System.Windows.Forms.TextBox textBoxAdd;

         public Form2()

         {

              InitializeComponent();

              foreach(object o in AppDatas.ListData)

                   this.listBoxFrm2.Items.Add(o);

         }

         protected override void Dispose( bool disposing )

         {

              if( disposing )

                   if(components != null)

                       components.Dispose();

              base.Dispose( disposing );

         }

         private void InitializeComponent()

         {

              this.buttonOK = new System.Windows.Forms.Button();

              this.listBoxFrm2 = new System.Windows.Forms.ListBox();

              this.buttonAdd = new System.Windows.Forms.Button();

              this.buttonDel = new System.Windows.Forms.Button();

              this.textBoxAdd = new System.Windows.Forms.TextBox();

              this.SuspendLayout();

              this.buttonOK.Location = new System.Drawing.Point(188,
108);

              this.buttonOK.Name = “buttonOK”;

              this.buttonOK.TabIndex = 0;

              this.buttonOK.Text = “确定”;

              this.buttonOK.Click += new
System.EventHandler(this.buttonOK_Click);

              this.listBoxFrm2.ItemHeight = 12;

              this.listBoxFrm2.Location = new System.Drawing.Point(8,
8);

              this.listBoxFrm2.Name = “listBoxFrm2”;

              this.listBoxFrm2.Size = new System.Drawing.Size(168, 124);

              this.listBoxFrm2.TabIndex = 2;

              this.buttonAdd.Location = new System.Drawing.Point(188,
44);

              this.buttonAdd.Name = “buttonAdd”;

              this.buttonAdd.TabIndex = 3;

              this.buttonAdd.Text = “增加”;

              this.buttonAdd.Click += new
System.EventHandler(this.buttonAdd_Click);

              this.buttonDel.Location = new System.Drawing.Point(188,
76);

              this.buttonDel.Name = “buttonDel”;

              this.buttonDel.TabIndex = 4;

              this.buttonDel.Text = “删除”;

              this.buttonDel.Click += new
System.EventHandler(this.buttonDel_Click);

              this.textBoxAdd.Location = new System.Drawing.Point(188,
12);

              this.textBoxAdd.Name = “textBoxAdd”;

              this.textBoxAdd.Size = new System.Drawing.Size(76, 21);

              this.textBoxAdd.TabIndex = 5;

              this.textBoxAdd.Text = “”;

              this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

              this.ClientSize = new System.Drawing.Size(272, 141);

              this.Controls.Add(this.textBoxAdd);

              this.Controls.Add(this.buttonDel);

              this.Controls.Add(this.buttonAdd);

              this.Controls.Add(this.listBoxFrm2);

              this.Controls.Add(this.buttonOK);

              this.Name = “Form2”;

              this.Text = “Form2”;

              this.ResumeLayout(false);

         }

         private void buttonOK_Click(object sender, System.EventArgs
e)
         {
              this.Close();
         }

         private void buttonAdd_Click(object sender, System.EventArgs
e)

         {

              if(this.textBoxAdd.Text.Trim().Length>0)

              {

                   AppDatas.ListData.Add(this.textBoxAdd.Text.Trim());

                  
this.listBoxFrm2.Items.Add(this.textBoxAdd.Text.Trim());

              }

              else

                   MessageBox.Show(“请输入添加的内容!”);

             

         }

         private void buttonDel_Click(object sender, System.EventArgs
e)

         {

              int index = this.listBoxFrm2.SelectedIndex;

              if(index!=-1)

              {

                    AppDatas.ListData.RemoveAt(index);

                   this.listBoxFrm2.Items.RemoveAt(index);

              }

              else

                   MessageBox.Show(“请选取删除项!”);

         }

     }

}    

    
总括,我觉着采取静态类比较多的地方就是把应用程序的布署文件装载到一个静态类里面,让所有的窗体和其他实例都可以经过静态属性以及静态方法使用那一个多少,比如三层构造或多层协会都足以访问它,而不是在三个实例间传来传去。在那里大家商量的是Windows窗体,其实在五个不等的实例间互动数据,都足以利用三篇作品中的方案达成,除非是这几个类特有的性能或着办法。现在都讲完了,就算不是什么奥秘的东西,但是指望能对有些初学者有所援助,同时也欢迎各位朋友举办技术交换,共同升高。

 

 

解析上面二种方式:

1. 
选取了信托的措施,可以兑现。:很好的兑现了数额处理与数量突显的离别,即FORM2(主)突显与FORM1数据处理,(不须要将FORM2的呈现位于FORM1中)与VC的回调的施用有可持续性。并且有限支撑了FORM1中要修改的特性的私有性。

 

2. 
方法2、3都是传递主窗口的引用,相比较简单易用。可以完结FORM2(主)与FORM1所有多少的传递(不过要求将要FORM1传递和要修改的数量设为PUBLIC),而这般会设有安全问题。

 

信托方法可以很好地落到实处多少的维护

 

  不相同于Windows 8应用,Windows
10引入了“波士顿菜单”这一导航方式。说具体点,就拿官方的天气应用来说,左上角三条横杠的图标外加一个SplitView控件组成的这一导航情势就叫“罗马菜单”。

葡京网上娱乐场 1

  本文商讨的是怎么样贯彻官方的这一体裁(点击后左边出现一个填写矩形),普通落成网上遍地可见,有亟待的情人自己百度下啊。

  下边将介绍三种不相同的贯彻格局,第一种最简易的点子是直接采取 Template
10 模板,第二种就是纯手写了。

 

  若有怎么着不正确的地方望指正,我们一块儿钻探。

 

  1. Template 10 模板

  使用 Template 10 模板可以火速建立出利用的框架,不难便捷。(辅助文档
https://github.com/Windows-XAML/Template10/wiki )

  要采纳 Template 10 首先点击 Visual Studio
“工具”菜单中的“增添与更新”,搜索并安装 Template
10(简化搜索可以平素输入t10)

葡京网上娱乐场 2

  安装已毕要求重启,重启后按下图找到项目模板新建即可,使用很简短,协助文档连接也在上面给出。

葡京网上娱乐场 3

 

  2. 手写

  先分析一下界面的组合,暂时不看标题栏,由一个设置了 Canvas.ZIndex 的 Button
和一个 SplitView 构成。SplitView.Pane
中又富含了五个ListView(一流菜单和二级菜单)。ListView 里的每个 Item
又由 Rectangle,FontIcon,TextBlock 组成。见下图

葡京网上娱乐场 4

  构成清晰之后达成的思绪大概也就清清楚楚了。上边给一个简短的Demo,解决方案结构如下。(GitHub https://github.com/ZhangGaoxing/uwp-demo/tree/master/HamburgerDemo

葡京网上娱乐场 5

  先创建一个NavMenuItem类

using System;
using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;

namespace HamburgerDemo
{
    class NavMenuItem : INotifyPropertyChanged
    {
        // 记录图标字体
        public FontFamily FontFamily { get; set; }
        // 图标的C#转义代码
        public string Icon { get; set; }
        // 标题
        public string Label { get; set; }
        // 导航页
        public Type DestPage { get; set; }
        // 用于左侧矩形的显示
        private Visibility selected = Visibility.Collapsed;
        public Visibility Selected
        {
            get { return selected; }
            set
            {
                selected = value;
                this.OnPropertyChanged("Selected");
            }
        }
        // 双向绑定,用于更新矩形是否显示
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

  

  主页面框架代码

<Page
    x:Class="HamburgerDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HamburgerDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Page.Resources>
        <!--菜单的数据模板-->
        <DataTemplate x:Key="DataTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="48" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Rectangle Fill="{ThemeResource SystemControlBackgroundAccentBrush}" 
                           Visibility="{Binding Selected, Mode=TwoWay}" 
                           HorizontalAlignment="Left" Width="5" Height="48" />

                <FontIcon FontFamily="{Binding FontFamily}" Glyph="{Binding Icon}" Foreground="White" 
                          VerticalAlignment="Center" 
                          Margin="-2,0,0,0" Width="48" Height="48" />

                <TextBlock Grid.Column="1" 
                           Text="{Binding Label}" Foreground="White" 
                           Margin="12,0,0,0" VerticalAlignment="Center" />
            </Grid>
        </DataTemplate>
        <!--ListViewItem样式定制-->
        <Style x:Key="NavMenuItemContainerStyle" TargetType="ListViewItem">
            <Setter Property="MinWidth" Value="{StaticResource SplitViewCompactPaneThemeLength}"/>
            <Setter Property="Height" Value="48"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="UseSystemFocusVisuals" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListViewItem">
                        <ListViewItemPresenter ContentTransitions="{TemplateBinding ContentTransitions}"
                        Control.IsTemplateFocusTarget="True"
                        SelectionCheckMarkVisualEnabled="False"
                        PointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}"
                        PointerOverForeground="{ThemeResource ListViewItemForegroundPointerOver}"
                        SelectedBackground="Transparent"
                        SelectedForeground="{ThemeResource SystemControlForegroundAccentBrush}"
                        SelectedPointerOverBackground="{ThemeResource SystemControlHighlightListLowBrush}"
                        PressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
                        SelectedPressedBackground="{ThemeResource SystemControlHighlightListMediumBrush}"
                        DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
                        HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                        ContentMargin="{TemplateBinding Padding}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <!--汉堡菜单开关-->
        <Button Name="PaneOpenButton" 
                FontFamily="Segoe MDL2 Assets" Content="&#xE700;" Foreground="White" 
                Background="{Binding BackgroundColor}" 
                Width="48" Height="48" 
                VerticalAlignment="Top" Canvas.ZIndex="100" />

        <SplitView Name="RootSplitView" 
                   DisplayMode="CompactOverlay" 
                   CompactPaneLength="48" OpenPaneLength="256" 
                   IsPaneOpen="True">

            <SplitView.Pane>
                <Grid Background="#CC000000">
                    <Grid.RowDefinitions>
                        <!--空出Button的高度-->
                        <RowDefinition Height="48" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <!--一级菜单-->
                    <ListView Name="NavMenuPrimaryListView" 
                              Grid.Row="1" VerticalAlignment="Top" 
                              SelectionMode="None" IsItemClickEnabled="True" 
                              ItemTemplate="{StaticResource DataTemplate}" 
                              ItemContainerStyle="{StaticResource NavMenuItemContainerStyle}"/>
                    <!--二级菜单-->
                    <ListView Name="NavMenuSecondaryListView" 
                              Grid.Row="2" VerticalAlignment="Bottom" 
                              SelectionMode="None" IsItemClickEnabled="True" 
                              ItemTemplate="{StaticResource DataTemplate}" 
                              ItemContainerStyle="{StaticResource NavMenuItemContainerStyle}" 
                              BorderBrush="{ThemeResource SystemControlBackgroundAccentBrush}" BorderThickness="0,1,0,0" />
                </Grid>
            </SplitView.Pane>

            <SplitView.Content>
                <Frame Name="RootFrame" />
            </SplitView.Content>

        </SplitView>

    </Grid>
</Page>

 

  主页面的后台代码

using HamburgerDemo.Views;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace HamburgerDemo
{
    public sealed partial class MainPage : Page
    {
        // 为不同的菜单创建不同的List类型
        private List<NavMenuItem> navMenuPrimaryItem = new List<NavMenuItem>(
            new[]
            {
                new NavMenuItem()
                {
                    FontFamily = new FontFamily("Segoe MDL2 Assets"),
                    Icon = "\xE10F",
                    Label = "页面1",
                    Selected = Visibility.Visible,
                    DestPage = typeof(Page1)
                },

                new NavMenuItem()
                {
                    FontFamily = new FontFamily("Segoe MDL2 Assets"),
                    Icon = "\xE11A",
                    Label = "页面2",
                    Selected = Visibility.Collapsed,
                    DestPage = typeof(Page1)
                },

                new NavMenuItem()
                {
                    FontFamily = new FontFamily("Segoe MDL2 Assets"),
                    Icon = "\xE121",
                    Label = "页面3",
                    Selected = Visibility.Collapsed,
                    DestPage = typeof(Page1)
                },

                new NavMenuItem()
                {
                    FontFamily = new FontFamily("Segoe MDL2 Assets"),
                    Icon = "\xE122",
                    Label = "页面4",
                    Selected = Visibility.Collapsed,
                    DestPage = typeof(Page1)
                }

            });

        private List<NavMenuItem> navMenuSecondaryItem = new List<NavMenuItem>(
            new[]
            {
                new NavMenuItem()
                {
                    FontFamily = new FontFamily("Segoe MDL2 Assets"),
                    Icon = "\xE713",
                    Label = "设置",
                    Selected = Visibility.Collapsed,
                    DestPage = typeof(Page1)
                }
            });

        public MainPage()
        {
            this.InitializeComponent();
            // 绑定导航菜单
            NavMenuPrimaryListView.ItemsSource = navMenuPrimaryItem;
            NavMenuSecondaryListView.ItemsSource = navMenuSecondaryItem;
            // SplitView 开关
            PaneOpenButton.Click += (sender, args) =>
            {
                RootSplitView.IsPaneOpen = !RootSplitView.IsPaneOpen;
            };
            // 导航事件
            NavMenuPrimaryListView.ItemClick += NavMenuListView_ItemClick;
            NavMenuSecondaryListView.ItemClick += NavMenuListView_ItemClick;
            // 默认页
            RootFrame.SourcePageType = typeof(Page1);
        }

        private void NavMenuListView_ItemClick(object sender, ItemClickEventArgs e)
        {
            // 遍历,将选中Rectangle隐藏
            foreach (var np in navMenuPrimaryItem)
            {
                np.Selected = Visibility.Collapsed;
            }
            foreach (var ns in navMenuSecondaryItem)
            {
                ns.Selected = Visibility.Collapsed;
            }

            NavMenuItem item = e.ClickedItem as NavMenuItem;
            // Rectangle显示并导航
            item.Selected = Visibility.Visible;
            if (item.DestPage != null)
            {
                RootFrame.Navigate(item.DestPage);
            }

            RootSplitView.IsPaneOpen = false;
        }
    }
}

 

  运行效果图如下

葡京网上娱乐场 6