Silverlight - 根据用户角色控制可见性

发布于 2024-12-21 06:53:04 字数 539 浏览 2 评论 0原文

我试图弄清楚如何根据用户角色显示/隐藏导航链接。目前,我正在测试用户是否已登录,效果很好 - 这是我的代码:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光是把杀猪刀 2024-12-28 06:53:04

您需要在主页中添加登录事件处理程序,如下所示:
Authentication.LoggedIn += LoggedIn_Event;

LoggedIn_Event 方法中,检查用户是否处于所需角色:

private void LoggedIn_Event(object sender, AuthenticationEventArgs e){
    if (e.User.IsInRole("Required Role")){
        AdminLinkButton.Visibility = System.Windows.Visiblity.Visible;
     }
    else
    {
        AdminLinkButton.Visibility = System.Windows.Visiblity.Collapsed;
    }
}

并记住处理 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:

private void LoggedIn_Event(object sender, AuthenticationEventArgs e){
    if (e.User.IsInRole("Required Role")){
        AdminLinkButton.Visibility = System.Windows.Visiblity.Visible;
     }
    else
    {
        AdminLinkButton.Visibility = System.Windows.Visiblity.Collapsed;
    }
}

And remember to handle the LoggedOut_Event and disable the control.

浴红衣 2024-12-28 06:53:04

一种选择是通过 ConverterParameter< 传递参数/a> 将识别与确定从转换器返回的结果相关的用户类型

ConverterParameter='admin'

避免 ConverterParamter 施加的字符串限制的另一个选项是通过单例或其他静态方式存储登录的用户类型,转换器可以查询该类型以了解登录的用户,从而返回由此产生的可见性。如果必须访问 XAML 中的数据,可以使用 x:Static 来实现。

ConverterParameter={x:Static namespace:LoggedInUserType}

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.

ConverterParameter='admin'

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 of x:Static.

ConverterParameter={x:Static namespace:LoggedInUserType}
鲜肉鲜肉永远不皱 2024-12-28 06:53:04

有几种方法可以做到这一点,其中一些方法比其他方法更黑客:

  1. 创建一个转换器来完成将用户是否处于某个角色中转换为可见性值的整个工作
  2. 链接多个转换器(点击答案)每个转换器执行一部分转换

方法#1 可以帮助您实现这一目标,但最终这是一个糟糕的方法,因为它会导致过多的专用转换器,而这些转换器可能需要 PITA 来维护。

方法 2 需要更多工作,但总体来说是更好、更易于维护的方法。

There are a couple of ways to do this, some ways more of a hack than others:

  1. create one converter to do the whole job of converting whether the user is in a role to a Visibility value
  2. chain multiple converters (follow the link in the answer) with each converter doing one part of the conversion

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文