wpf 将数据从一个窗口传递到另一个窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15114782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:45:13  来源:igfitidea点击:

passing data from one Window to another

c#wpf

提问by user2093348

I have 2 windows. When I press a button, I want to pass a Personobject from current Windowto the other one.

我有2个窗户。当我按下一个按钮时,我想将一个Person对象从当前Window传递到另一个。

This is the code in the main window:

这是主窗口中的代码:

private void Button_Click(object sender, RoutedEventArgs e)
{
    person owner = new person();
    owner.ID = 1;
    owner.Name = "Mark";
    w2 newWindow = new w2(owner);

    newWindow.Show();
}

In the other window named w2I have this code:

在另一个名为w2我的窗口中,有以下代码:

public partial class w2 : Window
{
    public class person
    {
        int _ID;
        string _Name;

        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
   }

   public w2()
   {
       InitializeComponent();
   }

   public w2(person passedPerson) : this()
   { 
       this.passedPerson = passedPerson;
   }
}

I get these errors when I try to compile:

当我尝试编译时出现这些错误:

Error 3 'passing.w2' does not contain a definition for 'passedPerson' and no extension method 'passedPerson' accepting a first argument of type 'passing.w2' could be found (are you missing a using directive or an assembly reference? 48 18 passing

Error 1 The best overloaded method match for 'passing.w2.w2(passing.w2.person)' has some invalid arguments 57 28 passing

Error 2 Argument 1: cannot convert from 'passing.MainWindow.person' to 'passing.w2.person' 57 35 passing

错误 3“passing.w2”不包含“passedPerson”的定义,并且找不到接受“passing.w2”类型的第一个参数的扩展方法“passedPerson”(您是否缺少 using 指令或程序集引用?48 18 路过

错误 1 ​​'passing.w2.w2(passing.w2.person)' 的最佳重载方法匹配有一些无效参数 57 28 传递

错误 2 参数 1:无法从 'passing.MainWindow.person' 转换为 'passing.w2.person' 57 35 通过

BTW. I've using this example as a starting point:

顺便提一句。我使用这个例子作为起点:

http://go4answers.webhost4life.com/Example/pass-variables-windowopen-wpf-31068.aspx

http://go4answers.webhost4life.com/Example/pass-variables-windowopen-wpf-31068.aspx

回答by Youssef

Try to explicitly assign each property of the passed person. what I mean is try to do

尝试显式分配传递者的每个属性。我的意思是尝试做

public w2(MainForm.person passedPerson)
        : this()
{
    this.passedPerson.ID = passedPerson.ID;
    this.passedPerson.Name = passedPerson.Name;
}

You might have defined 2 classes for Person. One in each form.

您可能已经为 Person 定义了 2 个类。一种形式。

Although you should only define person one time only. (Create a seperate class and use it for both forms)

虽然你应该只定义一个人一次。(创建一个单独的类并将其用于两种形式)

回答by Servy

So first, the easy one:

所以首先,简单的:

'passing.w2' does not contain a definition for 'passedPerson'

“passing.w2”不包含“passedPerson”的定义

When you say:

当你说:

this.passedPerson = passedPerson;

Clearly you're trying to assign an instance field for the person, but there is no such field. Just add one:

显然,您正在尝试为该人分配一个实例字段,但没有这样的字段。只需添加一个:

private person passedPerson;

For the other two errors, it's a result of the fact that you have defined two personclasses, one in each window. You shouldn't be doing that. Just because there are two classes that have the same name and the same fields, doesn't make them the same class, and doesn't mean that you can treat one as if it were the other.

对于其他两个错误,这是因为您定义了两个person类,每个窗口一个。你不应该那样做。仅仅因为有两个具有相同名称和相同字段的类,并不使它们成为同一个类,也不意味着您可以将一个类视为另一个类。

Create your personclass at the top level (meaning in it's own file, ideally), and ensure that both windows use that one definition.

person在顶层创建您的类(最好是在它自己的文件中),并确保两个窗口都使用同一个定义。

Also note that, by convention, class names start with an upper case letter:

另请注意,按照惯例,类名以大写字母开头:

回答by Sachin Hinge

try this,first window LoginPageWindow

试试这个,第一个窗口 LoginPageWindow

public partial class LoginPageWindow : Window
 {

    private void btnSave_Click(object sender, RoutedEventArgs e)
    {
       new Suucessfullogin(textboxvalue.text).ShowDialog();
    }
 }

in second window we can get value

在第二个窗口中,我们可以获得价值

public partial class Suucessfullogin : Window
{
      string str;   
    public Suucessfullogin(string value)
    {
        InitializeComponent();
        str= value;     
    }
}

回答by bash.d

Just nesting a class inside a class doesn't create a member of it. Try

只是在一个类中嵌套一个类并不会创建它的成员。尝试

public partial class w2 : Window
{
   private person passedPerson;
   ....
}

Then populate your member with a reference.

然后用参考填充您的成员。

Or better: put your class person outside of the other classes so they both have access to its definition.

或者更好:将您的班级人员放在其他班级之外,以便他们都可以访问其定义。

回答by Vale

Are you sure that your "owner" variable is of type person defined in w2 class (w2.person)? And where is your this.passedPerson field defined? It looks like it is not defined at all. You should not create inner class if you want to use it outside the "parent" class. Just make Person class in a separate file.

您确定您的“所有者”变量是在 w2 类 (w2.person) 中定义的 person 类型吗?你的 this.passedPerson 字段在哪里定义?看起来它根本没有定义。如果您想在“父”类之外使用它,则不应创建内部类。只需在单独的文件中创建 Person 类。