Silverlight - 根据用户角色控制可见性
我试图弄清楚如何根据用户角色显示/隐藏导航链接。目前,我正在测试用户是否已登录,效果很好 - 这是我的代码:
<HyperlinkButton x:Name="AdminLinkButton" Visibility="{Binding User.IsAuthenticated, Source={StaticResource WebContext}, TargetNullValue=false, Converter={StaticResource VisibilityConverter}}" Style="{StaticResource LinkStyle}" NavigateUri="/Admin" TargetName="ContentFrame" Content="{Binding Path=Strings.AdminPageTitle, Source={StaticResource ApplicationResources}}"/>
但是,现在我需要更改它以确保“管理”按钮仅在用户处于“管理员”角色时才可见。
有人有建议吗?
谢谢,
I am trying to figure out how to show/hide a navigation link based on the users role. Currently I am testing to see if the user is logged in, and that works great - here's my code:
<HyperlinkButton x:Name="AdminLinkButton" Visibility="{Binding User.IsAuthenticated, Source={StaticResource WebContext}, TargetNullValue=false, Converter={StaticResource VisibilityConverter}}" Style="{StaticResource LinkStyle}" NavigateUri="/Admin" TargetName="ContentFrame" Content="{Binding Path=Strings.AdminPageTitle, Source={StaticResource ApplicationResources}}"/>
However, now I need to change it to make sure the Admin button is only visible if the user is in the "Admin" role.
Anybody have a suggestion?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在主页中添加登录事件处理程序,如下所示:
Authentication.LoggedIn += LoggedIn_Event;
在
LoggedIn_Event
方法中,检查用户是否处于所需角色:并记住处理
LoggedOut_Event
并禁用该控件。You need to add the loggedin eventhandler in the mainpage like so:
Authentication.LoggedIn += LoggedIn_Event;
In the
LoggedIn_Event
method, check whether the user is in the required role:And remember to handle the
LoggedOut_Event
and disable the control.一种选择是通过 ConverterParameter< 传递参数/a> 将识别与确定从转换器返回的结果相关的用户类型。
避免
ConverterParamter
施加的字符串限制的另一个选项是通过单例或其他静态方式存储登录的用户类型,转换器可以查询该类型以了解登录的用户,从而返回由此产生的可见性。如果必须访问 XAML 中的数据,可以使用x:Static
来实现。One option is to pass a parameter via the ConverterParameter which will identify the type of user associated with determining the result returned from the converter.
Another option to avoid the String limitation imposed by the
ConverterParamter
is to store the type of user logged in via a Singleton or other static means which the converter can query to know what user is logged in and thus return the resulting visibility. If you must access the data within XAML you can do so by making use ofx:Static
.有几种方法可以做到这一点,其中一些方法比其他方法更黑客:
方法#1 可以帮助您实现这一目标,但最终这是一个糟糕的方法,因为它会导致过多的专用转换器,而这些转换器可能需要 PITA 来维护。
方法 2 需要更多工作,但总体来说是更好、更易于维护的方法。
There are a couple of ways to do this, some ways more of a hack than others:
Approach #1 will get you there, but ultimately is a bad approach because it leads to a plethora of specialized converters which can be a PITA to maintain.
Approach #2 is more work but overall a better and more maintainable approach.