基于角色的视图 XAML [英] Role based View XAML

查看:27
本文介绍了基于角色的视图 XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有许多用户角色的 WinRT 应用程序.我的应用程序中许多页面的视图根据用户角色而变化

I am developing a WinRT app which has many user roles. The View of many pages in my app change based on the 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?

绑定 Grid.Visibility 是实现此目的的好方法还是有更好的方法?

Is binding Grid.Visibility a good way of doing this or is there any better way?

更新:
通过按照@Ahmed(下面的答案)建议的方式执行此操作,我将加载设计中的所有控件,并且仅更改它的可见性.我的页面仍然很重.

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. My page will still be heavy.

无论如何我只能加载我希望看到的控件

Is there anyway where I can load only the controls which I wish to see

推荐答案

当然有很多方法可以组成您的 UI.

There are certainly many ways to compose your UI.

  • 您可以按照建议使用 Visibility 来显示或隐藏 UI 的各个部分.
  • 您可以使用 VisualStateManager 来显示或隐藏各种视图,甚至更改模板.
  • 您可以使用带有 ItemTemplateSelectorItemsControl 来根据视图模型的输入集合显示不同的项目 - 例如对于学生,ItemsControlItemsSource 将绑定到个人信息、绩效图表、待办事项和收到的徽章的视图模型集合,而对于教授,您将获得查看个人信息、待办事项和收到的反馈的模型.ItemTemplateSelector 将提供这些特定项目的不同视图.
  • 您还可以使用带有 ItemTemplateSelectorItemsControl 来显示相同​​视图模型的不同视图,具体取决于您将传递给选择器的用户角色.
  • 您可以使用带有自定义 ContentTemplateSelector 将根据绑定到 Content<的视图模型中提供的用户角色信息提供不同的视图/code> 属性.
  • 您可以编写一些代码来根据用户角色显示/隐藏/添加/删除 UI 组件.
  • 您可以将各种视图(徽章、个人信息、反馈等)封装在每个视图的单独 UserControl 中,并使用上述任何一种技术来显示/隐藏特定视图.
  • 您可以为不同的用户角色设计不同的页面,并根据角色 - 导航到特定页面.
  • 最后,您可以为不同的用户角色创建单独的应用.
  • You can use Visibility as suggested to show or hide various parts of your UI.
  • You can use VisualStateManager to show or hide various views or even change the templates.
  • You can use an ItemsControl with ItemTemplateSelector to display different items based on the input collection from the view model - e.g. 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. The ItemTemplateSelector would provide a different view of these specific items.
  • 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.
  • 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.
  • You can write some code behind to show/hide/add/remove UI components based on the user role.
  • You can encapsulate various views (badges, personal info, feedback, etc.) in separate UserControls per view and use any of the above techniques to show/hide specific views.
  • 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.

以上所有内容都将从使用 MVVM 模式中受益匪浅.选择一种技术或技术组合取决于您想要显示什么信息、它应该如何布局、您希望它的可维护性与最初开发的速度、您希望它的安全性以及最后,您知道多少、愿意学习多少或有多少时间这样做.

All of the above would strongly benefit from using the MVVM pattern. 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.

如果角色不多 - 我个人可能会为每个角色创建一个单独的应用程序并尽可能多地重用代码,尽管这也取决于其他一些要求 - 比如两个人访问的能力来自同一设备的系统.我根本不会使用显示/隐藏技术,因为这仍然需要加载 UI、使用内存等.大多数应用程序都有某种中央集线器/仪表板,实现为 GridView 或其他一些布局并显示缩短的预览的数据/链接转到详细视图或完整列表.这是我在视图模型中为不同角色提供不同数据的地方.详细信息页面可能不需要那么多自定义,因为每个角色的数据看起来基本相同,或者某些角色根本无法访问.当然,根据权限,某些人可能会被允许编辑或查看更多/更少的数据.对于这种较小的差异,您可以使用模板选择器,提供来自视图模型的不同数据,并使用命令绑定控制编辑按钮的可见性.

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. 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.

这篇关于基于角色的视图 XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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