简体   繁体   English

基于角色的View XAML

[英]Role based View XAML

I am developing a WinRT app which has many user roles. 我正在开发具有许多用户角色的WinRT应用程序。 The View of many pages in my app change based on the Userrole 应用程序中许多页面的视图根据Userrole进行更改

For eg. 例如。 I have a Student role and a Professor role. 我有学生角色和教授角色。 When the Student logs in he will see personal-info, performance chart, todos and badges received and when a professor logs in she will see her personal-info, todos and feedback received 当学生登录时,他将看到收到的个人信息,性能图表,待办事项和徽章;当教授登录时,她将看到其收到的个人信息,待办事项和反馈

How do I only show the components which are necessary? 我如何只显示必要的组件?

Is binding Grid.Visibility a good way of doing this or is there any better way? 绑定Grid.Visibility是执行此操作的好方法还是有更好的方法?

Update: 更新:
By doing it the way suggested by @Ahmed(answers below) I will be loading all the controls in the design and only change the visibility of it. 通过@Ahmed建议的方式(下面的答案),我将加载设计中的所有控件,仅更改其可见性。 My page will still be heavy. 我的页面仍然很沉重。

Is there anyway where I can load only the controls which I wish to see 无论如何,我只能加载希望看到的控件

There are certainly many ways to compose your UI. 当然,有很多方法可以组成您的UI。

  • You can use Visibility as suggested to show or hide various parts of your UI. 您可以按照建议使用“ Visibility ”来显示或隐藏UI的各个部分。
  • You can use VisualStateManager to show or hide various views or even change the templates. 您可以使用VisualStateManager显示或隐藏各种视图,甚至更改模板。
  • You can use an ItemsControl with ItemTemplateSelector to display different items based on the input collection from the view model - eg for a student the ItemsSource of the ItemsControl would be bound to a collection of view models for personal-info, performance chart, todos and badges received and for a professor you would get view models for personal-info, todos and feedback received. 您可以将ItemsControlItemTemplateSelector一起使用,以基于视图模型的输入集合显示不同的项目-例如,对于学生而言, ItemsControlItemsSource将绑定到用于个人信息,性能图表,待办事项和徽章的视图模型的集合收到的信息,对于教授而言,您将获得个人信息,待办事项和反馈的视图模型。 The ItemTemplateSelector would provide a different view of these specific items. ItemTemplateSelector将提供这些特定项目的不同视图。
  • You could also use an ItemsControl with ItemTemplateSelector to display different views of same view models depending on the user role that you would pass to the selector. 您还可以结合使用ItemsControlItemTemplateSelector来显示相同​​视图模型的不同视图,具体取决于要传递给选择器的用户角色。
  • You could use a ContentControl with a custom ContentTemplateSelector that would provide a different view based on the user role information provided in the view model bound to the Content property. 您可以将ContentControl与自定义ContentTemplateSelector ,后者将基于绑定到Content属性的视图模型中提供的用户角色信息提供不同的视图。
  • You can write some code behind to show/hide/add/remove UI components based on the user role. 您可以根据用户角色在后面编写一些代码以显示/隐藏/添加/删除UI组件。
  • You can encapsulate various views (badges, personal info, feedback, etc.) in separate UserControl s per view and use any of the above techniques to show/hide specific views. 您可以在每个视图的单独UserControl封装各种视图(徽章,个人信息,反馈等),并使用上述任何一种技术来显示/隐藏特定视图。
  • You can design a different page for a different user role and depending on the role - navigate to a specific page. 您可以为不同的用户角色设计不同的页面,具体取决于角色-导航到特定页面。
  • Finally you can create separate apps for different user roles. 最后,您可以为不同的用户角色创建单独的应用程序。

All of the above would strongly benefit from using the MVVM pattern. 上面的所有内容都将从使用MVVM模式中受益匪浅。 The choice of a technique or rather combination of techniques would depend on what information you want displayed, how it's supposed to be laid out, how maintainable you want it to be vs. how quick to initially develop, how secure you want it to be and finally how much you know, are willing to learn or have time to do so. 选择一种技术或更确切地说是多种技术的组合将取决于您希望显示的信息,应该如何布置,想要保持多大的可维护性,最初开发的速度如何,想要的安全性以及最后,您知道多少,愿意学习或有时间这样做。

If there are not too many roles - I would personally probably create a separate app for each role and reuse as much of the code as possible, though that would also depend on some other requirements - like the ability for two people to access the system from the same device. 如果没有太多角色-我个人可能会为每个角色创建一个单独的应用程序并重用尽可能多的代码,尽管这还取决于其他一些要求-例如两个人从系统访问系统的能力同一台设备。 I wouldn't use the show/hide techniques at all since that still requires the UI to be loaded, use up memory etc. Most apps have some sort of a central hub/dashboard implemented as a GridView or some other layout and display shortened previews of the data/links to go to detailed views or full lists. 我根本不会使用显示/隐藏技术,因为这仍然需要加载UI,消耗内存等。大多数应用程序都有某种形式的中心集线器/仪表板,实现为GridView或其他布局,并显示缩短的预览的数据/链接转到详细视图或完整列表。 This is where I would provide different data in the view model for different roles. 在这里,我将在视图模型中为不同的角色提供不同的数据。 Detail pages would likely not need as much customization since the data would look mostly the same for each role or be inaccessible to some roles at all. 详细信息页面可能不需要太多的自定义,因为每个角色的数据看起来几乎相同,或者某些角色根本无法访问。 Depending on permissions of course some people might be allowed to edit or see more/less of the data. 当然,根据权限,可能会允许某些人编辑或查看更多/更少的数据。 For such smaller differences you could use template selectors, provide different data from the view models and control edit button visibility using command bindings. 对于如此小的差异,您可以使用模板选择器,从视图模型提供不同的数据,并使用命令绑定控制编辑按钮的可见性。

The answer might be pretty broad and vague since a lot here depends on your specific design and requirements. 答案可能很宽泛也很模糊,因为这在很大程度上取决于您的特定设计和要求。

you can make your own converter something like this, that will change visibility based on roles 您可以将自己的转换器制作成类似这样的内容,这将根据角色来更改可见性

 public class RoleToVisibilityConverter : IValueConverter
 {
   public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
   {
    var user= value as User;
    if(user!= null) {
        return user.IsInRole((string)parameter) ? Visibility.Visible : Visibility.Collapsed;
   }
    return null;
   }

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo   culture)
  {
     throw new NotImplementedException();
  }
 }

And in your XAML you can use your converter to set control's visibility like this 在XAML中,您可以使用转换器来设置控件的可见性,如下所示

<Control Visibility={Binding Path=CurrentUser, Converter={StaticResource RoleToVisibilityConverter}, ConverterParameter=Student }/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM