为什么我不能写公开给数据成员,所以其他班级形式可以看到他? [英] why i cant write public to data member so other class form can see him?

查看:69
本文介绍了为什么我不能写公开给数据成员,所以其他班级形式可以看到他?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wpf应用程序中有两个表单,并且在一个表单中有一个类

我将数据放在类中

我希望你在另一个中看到这些可变数据表格

我该怎么做?



 名称空间 OnlineTradingSystem 
{
/// < 摘要 >
/// UserPassword.xaml的交互逻辑
/// < / summary >
public partial UserPassword:窗口
{
public UserPassword()
{
InitializeComponent ();


}
// 这是我的课后我按下按钮我想将他传递给另一个表格
// 它不让我写公告为什么?

// 为什么我不能写这个regis公共所以另一种形式可以看到他吗?
URegistration regis = new URegistration();
private void button1_Click( object sender,RoutedEventArgs e)
{

string username = usernameBox.Text;
string password = passwordBox.Password;
regis = new URegistration(密码,用户名);

DAL dal = new DAL();
regis = dal.ValidateUsernamePasswordCompatible(regis);


}



}




}

解决方案

这是关于窗口协作的热门问题。最强大的解决方案是在窗口类中实现适当的接口,并传递接口引用而不是引用Window的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。

解决方案是关于 System.Window.Form ,但WPF和 Window 的一切都完全相同。



也请请参阅本讨论中的其他解决方案如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个窗口的实例的引用传递给另一个窗口的实例一样简单窗口。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA


存在两种方式。

第一种方式:

你编写静态类并将两个属性设置为静态字符串。当您在一个表单中输入用户名和密码时,将变量设置为静态类。在工作之后,您可以使用静态类和accsses值的用户名和密码。

公共静态类容器

{

公共静态字符串用户名

{get; set;}

公共静态字符串密码

{get; set;}

}

// in form1

private void save()

{

container.username = textBox1.text;

container.password = textBox2.text;

}


由于这个问题非常受欢迎,我以前的答案往往不太清楚,可能还不够清楚,我决定写一篇提示/技巧文章,详细的代码示例和解释:许多问题一次回答 - Windows窗体或WPF Windows之间的协作



-SA

i have two form in wpf application and one class
in one form i put data in the class
and i want to u see those variable data in the other form
how can i do that?

namespace OnlineTradingSystem
{
    /// <summary>
    /// Interaction logic for UserPassword.xaml
    /// </summary>
    public partial class UserPassword : Window
    {
        public UserPassword()
        {
            InitializeComponent();

           
        }
        //this is my class that after i press on the button i want to pass him to another form
        // it not let me to write public why?

//   why i cant write to this regis public so the other form can see him?
         URegistration regis = new URegistration();
        private void button1_Click(object sender, RoutedEventArgs e)
        {
           
            string username = usernameBox.Text;
            string password = passwordBox.Password;
            regis = new URegistration(password,username);

            DAL dal = new DAL();
            regis= dal.ValidateUsernamePasswordCompatible(regis);
           
        
        }


 
    }




}

解决方案

This is the popular question about window collaboration. The most robust solution is implementation of an appropriate interface in window class and passing the interface reference instead of reference to a "whole instance" of a Window. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].
The solution is about System.Window.Form, but everything is exactly the same for WPF and Window.

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one window to the instance of another window. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


two way exist.
first way:
you write static class and set two property as static string.when you input username ana password in one form,you set variable into static class.after work,you can anywhere use of static class and accsses value username and password.
public static class container
{
public static string username
{get;set;}
public static string password
{get;set;}
}
//in form1
private void save()
{
container.username=textBox1.text;
container.password=textBox2.text;
}


As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA


这篇关于为什么我不能写公开给数据成员,所以其他班级形式可以看到他?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆