在WPF中在窗体/Windows之间共享数据 [英] Sharing Data between Forms/Windows in WPF

查看:96
本文介绍了在WPF中在窗体/Windows之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

昨天我问了一个有关Visual Basic和WPF的模块的问题,但仍未找到我的问题的答案.我目前正在从事一个计算项目,在该项目中必须将数据从表单"输入数据库.我的项目在Visual Basic中作为WPF应用程序.我使用WPF的原因是可以创建不同样式的表单和登录"屏幕.我试图设置2个WPF Windows,其中第一个(Window1)带有文本框,第二个(Window2)带有标签.我每次都会收到很多错误,因此,作为一个实验,我在Windows Forms而不是WPF Application下创建了一个新的新项目.在[表格1]中,我添加了一个名为[Textbox1]的文本框和一个名为[btnSend]的按钮.这是我的[Form 1]代码:

Hello

Yesterday I asked a question about Modules with Visual basic and WPF but still haven''t found an answer to my question. I am currently working on a computing project in which data must be entered in from a "Form" to a database. My project is in Visual Basic as a WPF Application. The reason I have used WPF is so that I can create different styled forms and Login screens. I attempted to set up 2 WPF Windows with Textboxes on the first one (Window1) and Labels on the second (Window2). I kept getting a number of errors every time, so as an experiment I created a fresh new project under Windows Forms rather than a WPF Application. In [Form 1] I added a textbox called [Textbox1] and a button called [btnSend]. Here is my code for [Form 1]:

Public Class Form1
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Form2.Show()
    End Sub
End Class



很简单,它要做的就是在按钮的click事件上打开Second Form.

在[表格2]中,我有一个名为[label1]的标签. (如果您想知道为什么我将这些名称保留为默认名称,那么它将使所有内容保持简单.)因此,这是[Form 2]的代码:



Very simple, All it does is open up the Second Form on the click event of the button.

On [Form 2] I have a label called [label1]. (If your wondering why I left the names as there default, its to keep everything simple.) So heres the code for [Form 2]:

Public Class Form2
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label1.Text = Form1.Textbox1.Text
    End Sub
End Class



因此,[Form 2]所做的只是在加载时使[label1]等于[Form 1]上的[textbox1].我编译了该应用程序,它运行良好.在文本框中输入随机的内容,然后单击按钮,第二个表单就会加载并显示我在第一个表单中输入文本框的内容.

因此,我对自己想,好吧,现在,我将创建一个新的新WPF应用程序,创建一个WPF窗口和一个窗体,然后将代码复制到其中,调整所有名称,以使所有内容正确地链接起来……".否.我现在收到错误消息:

错误1引用非共享成员需要对象引用.

与[Form 1]上的



So what [Form 2] does, is simply when loaded make [label1] equal to [textbox1] on [Form 1]. I compiled the application and it worked fine. Typed random things into the textbox and once I clicked the button the second form loaded up and displayed whatever I had typed into the textbox on the first form.

So I thought to myself, "Ok that works, now I will create a fresh new WPF Application, create a WPF Window and a Form and copy the code in, tweak any names so that everything is linked up correctly....". No. I now get the error message:

Error 1 Reference to a non-shared member requires an object reference.

Which corresponds to the the line

Form2.Show() 

行相对应.我也收到[Form 2]中

on [Form 1]. I also get the same error message for

Label1.Text = Form1.Textbox1.Text

的相同错误消息.

只是要确保与从WPF窗口更改为标准表单无关.我创建了一个新的新项目,并创建了2个标准表单,然后再次尝试.相同的错误消息.有谁知道为什么我在WPF应用程序中却收到错误消息,而在Windows Standard Form中却没有?谁能帮我解决一些在WPF应用程序中使用Visual Basic的示例,因为我已经搜索了几天却一无所获.
谢谢

in [Form 2].

Just to make sure it wasn''t something to do with the change from a WPF Window to a standard form. I create a fresh new project and create 2 standard Forms and tried it again. Same error messages. Does anyone know why I''m getting error messages in a WPF Application but not in a Windows Standard Form? Can anyone help me with some examples using Visual Basic in a WPF Application as I have been searching for days and have found nothing.

Thank you

推荐答案

如果不看XAML,很难判断出什么问题了.
我建议您看一下MVVM模式(Google是您的朋友).它解决了与您的问题相同的问题:允许视图绑定并共享相同的数据(ViewModels).
它还说明了如何将UI控件绑定到数据,以便可以在视图中显示和更新数据.
Without a look at your XAML it is hard to judge what is wrong.
I would suggest you take look at MVVM pattern (Google is your friend). It solves very same problem as yours: allow views bind to and share same data (ViewModels).
It also explains how to bind your UI controls to data so it could be displayed and updated in your views.


这篇关于在WPF中在窗体/Windows之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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