需要有关INotifyPropertyChanged的帮助 [英] Need Help with INotifyPropertyChanged

查看:94
本文介绍了需要有关INotifyPropertyChanged的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用INotifyPropertyChanged时需要帮助.

我正在Silverlight应用程序上工作,该应用程序有一个登录窗口(这是Silverlight子窗口),我想从MainPage(即父"窗口)访问在登录窗口中输入的用户凭据.我知道我需要使用INotifyPropertyChanged做到这一点.但是我不知道该怎么做.有人可以给我一个例子吗?

Need help with using INotifyPropertyChanged.

Im working on a Silverlight Application, which has a login window (which is a Silverlight Child window) and I want to access the user credentials entered in the login window from the MainPage (that is the Parent window). I know i need to do it with INotifyPropertyChanged. But I am not able to figure out how to do it. Can somebody give me an example for doing it?

推荐答案

@Aswin
我添加了一个新答案,因为我可以正确设置其格式

好的.一种简单的方法是为密码页面创建一个模型,通常它将具有属性UsernamePassword,让我们调用类型LoginCredentials,它将实现INotifyPropertyChanged.然后,您可以将登录 Window "的DataContext设置为新的LoginCredentials对象,因为您可以在隐藏代码"中进行此操作.然后,将用户名"和密码"框的文本属性"绑定到DataContext中的等效属性.
请参阅此处以获取有关数据绑定的文章:
一次向WPF数据绑定移动一步 [



我还不清楚根本询问对象",对不起,当我回答时我很累,所以我很糟糕.主窗口将有权访问其子窗口.登录Windows DataContext您可以将其强制转换为LoginCredentials并访问添加的用户名和密码属性.

最后,我要添加以下内容:IMO,所有窗口都应具有自己的对象模型来控制行为,并具有在XAML中绑定的属性.后面的代码实际上仅用于支持事件等.可以构建模型,以便主页模型具有登录凭据属性.这样做的一个警告是,密码将在内存中可用,因此您应该对此进行一些操作.我只是将用户名保留在登录模型中,存储密码直到用户登录(登录模型应对此负责),然后保持布尔登录状态或类似状态.这样,您就可以很好地分离关注点,并且代码实际上变得更加清晰.

希望这个[颇费劲的]回复会有所帮助!
@Aswin
I''ve added a new answer, as I can format it properly

OK. A simple way to do this is to create a model for the password page, typically it would have a property Username and Password, Lets call the type LoginCredentials, it would implement INotifyPropertyChanged. You then set the DataContext of the Login Window to a new LoginCredentials object in you can do this in "code-behind". You then bind the Text Properties of the Username and Password boxes to the equivalent properties in your DataContext.

See here for an article on Data Binding:
Moving Toward WPF Data Binding One Step at a Time[^], just look at "Version 4 – Binding in XAML"



I wasn''t clear about "Interrogate the object at all", sorry, I was pretty tired when I replied, so my bad. The Main winow will have access to it''s child Login Windows DataContext You can cast this to LoginCredentials and access the Username and Password properties you added.

Finally I''d add this: IMO, all the windows should have their own object model controlling behaviour, and have properties bound up in XAML. The code-behind is really just to support the events, etc. The models can be built up so that the main page model has a login credentials property. One caveat if you do this is that the password will be available in memory, so you should do something about this. I''d just keep the username in the Login model, store the password until the user logs in (the login model should be responsible for this), then keep a bool logged in status or some-such. That way you get good separation of concerns, and the code actually becomes clearer.

Hope this [rather long-winded] reply helps!


INotiftyPropertyChanged并不是您真正需要的,它的主要目的是允许DataContext的对象发出警报在绑定场景中更改了其属性的GUI.
输入数据后,主页应查询DataContext中的对象并从中获取值.样本会占用很多空间.

所有这些都假定您已将模型绑定到GUI.如果您提供一些代码,将更容易获得帮助.
INotiftyPropertyChanged isn''t really what you need, its main purpose is to allow the DataContext''s object to alert the GUI that its property i changed in a binding scenario.
Once the data is entered, the main page should interrogate the object in the DataContext and get the values from that. A sample would take up a lot of space.

This all assumes you have bound up a model to the GUI. It would be easier to help if you gave some code please.


您不需要INotifyPropertyChanged.只需创建一个保存数据的静态类,在用户登录时从主页设置数据,然后您就可以从应用程序中的任何位置引用它.
You don''t need INotifyPropertyChanged. Just create a static class that holds the data, set the data from the main page when the user logs in, and you can then refer to it from any point in the application.


这篇关于需要有关INotifyPropertyChanged的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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