如何从 App.xaml.cs 代码隐藏创建对 xaml 页面的对象引用?
我有一个 Silverlight 4 业务项目,其中启用了 ASP.NET 身份验证/授权角色信息。我想将当前经过身份验证的用户的帐户信息从 app.xaml.cs 代码隐藏传递到不同的 XAML 页面,但我不知道这是如何完成的,或者是否可能。
我的目标是根据当前用户是否处于特定的管理相关角色,对目标 XAML 页面的各个按钮的 IsEnabled 属性进行数据绑定。 app.xaml.cs 的 Application_UserLoaded 事件处理程序似乎是启动此任务的最安全的事件处理程序,因为它仅在从服务器加载用户的帐户信息后触发。
我以前曾尝试直接从目标 XAML 页面检索当前用户信息,但我从未获取当前用户信息,因为 Application_UserLoaded 尚未完成加载当前用户信息。
public partial class App : Application { private void Application_UserLoaded(LoadUserOperation operation) { // How do you create an object reference to a XAML page from your project solution // from this event handler? } }
预先感谢您的任何帮助, 约翰
I have a Silverlight 4 Business Project where I have enabled the ASP.NET Authentication/Authorization role information. I would like to pass the currently authenticated user's account information from the app.xaml.cs codebehind to a different XAML page, but I have no idea how that is done, or if it's even possible.
My goal is to databind the IsEnabled property of various buttons of my target XAML page, based on whether the current user is in a particular admin related role or not. The Application_UserLoaded event handler of app.xaml.cs seems to be the safest event handler to initiate this task because it fires only after the user's account information is loaded from the server.
I had previously attempted to retrieve the current user information directly from my target XAML page, but I was never getting the current user information because Application_UserLoaded hadn't finished loading the current user info yet.
public partial class App : Application { private void Application_UserLoaded(LoadUserOperation operation) { // How do you create an object reference to a XAML page from your project solution // from this event handler? } }
Thanks in advance for any assistance,
John
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要引用您的视图来完成此任务。有一个更好的解决方案。首先,使用 IsAdmin 属性为应用程序范围的上下文创建一个类,并将其添加到应用程序的资源字典中,
在 Application_UserLoaded 处理程序中设置 IsAdmin 属性,并从任何视图绑定到它干杯
。
You do not need a reference to your view to accomplish this task. There is a better solution. At first, create a class for the application-wide context with IsAdmin property, and add it to the App's resource dictionary
Set IsAdmin property in your Application_UserLoaded handler, and bind to it from any of your views
Cheers.
经过一番尝试和错误后,我通过在 app.xaml.cs 的 Application_UserLoaded() 处理程序中引发自定义事件处理程序解决了最初的问题。
然后,我从特定 XAML 视图页面的代码隐藏中订阅了这个自定义事件处理程序,在该页面中我需要对各种 Silverlight 控件的 IsEnabled 属性进行数据绑定。
我必须创建自定义事件处理程序的原因是因为您不能假设安全角色信息始终可以在 XAML 视图页面的 Loaded() 事件处理程序中引用。
我在以下位置找到了有关此特定时间问题的更多信息:
http://forums.silverlight.net/forums/p/146631/328217.aspx干杯
,
约翰
After some trial and error, I solved my original issue by raising a custom event handler in the Application_UserLoaded() handler of app.xaml.cs.
I then subscribed to this custom event handler from the codebehind of my specific XAML view page where I needed to databind the IsEnabled property of my various Silverlight controls.
The reason I had to create a custom event handler is because you cannot assume that the security role information will always be ready to reference in the Loaded() event handler of your XAML view page.
I found more information about this specific timing issue at:
http://forums.silverlight.net/forums/p/146631/328217.aspx
cheers,
John