简体   繁体   English

在caliburn.micro中启用管理模式视图

[英]Enable admin mode view in caliburn.micro

I am re-writing a WPF app to use Caliburn.Micro. 我正在重新编写WPF应用程序以使用Caliburn.Micro。 The app is a menu system that shows Folders and then files within those folders. 该应用程序是一个菜单系统,显示文件夹,然后显示这些文件夹中的文件。 The problem is that I need to allow the user to switch to an "Admin" mode which will allow additional options. 问题是我需要允许用户切换到“管理员”模式,这将允许其他选项。 I currently have a FolderView and FileView along with ViewModels for each. 我目前每个都有一个FolderView和FileView以及ViewModels。 I was thinking of having a seperate FolderAdminView and FileAdminView so I can change the UI and enable the additional options. 我当时在考虑使用单独的FolderAdminView和FileAdminView,以便可以更改UI并启用其他选项。 The problem is switching between the two when the user changes modes. 问题是当用户更改模式时在两者之间切换。

Both a FolderViewModel and FileViewModel can be loaded at the same time so a call to DeactivateItem(ActiveItem, true); FolderViewModel和FileViewModel可以同时加载,因此可以调用DeactivateItem(ActiveItem, true); will act as a back button and return to the folder view. 将充当后退按钮并返回到文件夹视图。

I would also like to carry over values from the FolderViewModel to the FolderAdminViewModel since the main difference is UI. 我还想将值从FolderViewModel继承到FolderAdminViewModel,因为主要区别在于UI。

Is there an easy way to swap out items in the WindowManager or an easier way to do this altogether? 是否有一种简单的方法可以在WindowManager中换出项目,或者可以更简单地将其全部替换? Could I have one ViewModel but two Views? 我可以有一个ViewModel但有两个视图吗? Is there a way to have both templates in one view and select the correct one there? 有没有一种方法可以将两个模板放在一个视图中并在其中选择正确的模板?

Can you not just have an IsAdmin property on your ViewModel and bind the visibility of your admin only items to that using a BooleanToVisibilityConverter ? 您是否不仅可以在ViewModel上具有IsAdmin属性,还可以使用BooleanToVisibilityConverter将仅管理员项目的可见性绑定到该项目?

ViewModel 视图模型

public bool IsAdmin
{
    get
    {
        //What ever you do to work out if user is admin 
        //omitted any INotifyPropertyChanged gubbins
    }
}

Xaml XAML

<StackPanel Visibility="{Binding IsAdmin,Converter={StaticResource BooleanToVisibiltyConverter}}"></StackPanel>

Converter 变流器

public sealed class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var flag = false;
            if (value is bool)
            {
                flag = (bool)value;
            }
            else if (value is bool?)
            {
                var nullable = (bool?)value;
                flag = nullable.GetValueOrDefault();
            }
            if (parameter != null)
            {
                if (bool.Parse((string)parameter))
                {
                    flag = !flag;
                }
            }
            return flag ? Visibility.Visible : Visibility.Collapsed;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var back = ((value is Visibility) && (((Visibility)value) == Visibility.Visible));
            if (parameter != null)
            {
                if ((bool)parameter)
                {
                    back = !back;
                }
            }
            return back;
        }
    }

You can use the 'context' attached property to specify the context for any views which are loaded eg: 您可以使用'context'附加属性为所有加载的视图指定上下文,例如:

<ContentControl x:Name="SomeSubViewModel" cal:View.Context="SomeContext" />

CM uses ToString() on the context object to obtain a value it will use to build the typename during view resolution. CM在上下文对象上使用ToString()获得一个值,它将在视图解析期间用于构建类型名。 This means you can have multiple views for the same viewmodel and therefore add additional functions when the user is in admin mode by binding the View.Context property 这意味着您可以为同一个视图模型拥有多个视图,因此当用户处于管理模式时,通过绑定View.Context属性可以添加其他功能。

You could also create a binding for each item you want to hide on the viewmodel and use a converter to check if the user is logged in etc - obviously it depends on whether you want to duplicate the XAML in two views or have a single view with conditional logic to hide/show areas 您还可以为要隐藏在viewmodel上的每个项目创建一个绑定,并使用转换器检查用户是否已登录等-显然,这取决于您是要在两个视图中复制XAML还是在单个视图中使用隐藏/显示区域的条件逻辑

Read up on the context property: 阅读context属性:

http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Conventions&referringTitle=Documentation http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Conventions&referringTitle=Documentation

the link above has some examples (in the first few sections) 上面的链接提供了一些示例(在前几节中)

and here: 和这里:

http://caliburnmicro.codeplex.com/wikipage?title=Screens%2c%20Conductors%20and%20Composition http://caliburnmicro.codeplex.com/wikipage?title=Screens%2c%20Conductors%20and%20Composition

in the Multiple Views over the Same ViewModel section 同一ViewModel多个视图部分中

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

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