Caliburn Micro内容控件导航 [英] Caliburn Micro content control navigation

查看:173
本文介绍了Caliburn Micro内容控件导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为此项目使用caliburn micro。
我的ShellView具有我的contentcontrol:

I'm using caliburn micro for this project. I have my ShellView with my contentcontrol:

<ContentControl x:Name="ActiveItem"
                        Grid.Row="0" Grid.Column="0" />

在ShellViewModel中,我得到了它来显示我的用户控件LoginView:

In ShellViewModel i got it to show my usercontrol LoginView with:

 public class ShellViewModel : Conductor<object>
    {
        public ShellViewModel()
        {
            ActivateItem(new LoginViewModel());
        }

        public void ShowSignUp()
        {
            ActivateItem(new SignUpViewModel());
        }
    }

但是,我无法从LoginView导航到SignUpView使用我的按钮:

However, i can't navigate to SignUpView from LoginView with my button:

<!-- Row 4 -->
<Button x:Name="ShowSignUp"
        Content="Sign Up Now!"
        Grid.Row="3" Grid.Column="1"
        Style="{StaticResource LoginBtnsStyle}" />

从ShellViewModel派生的LoginViewModel:

LoginViewModel deriving from ShellViewModel:

public class LoginViewModel : ShellViewModel
    {

    }

如何使用LoginView上的按钮从LoginView导航到SignUpView?
我没有任何错误,只是没有改变视图。
我也尝试将ShowSignUp()放在LoginViewModel上,但没有成功。

How do i navigate from LoginView to SignUpView with a button that is on the LoginView? I'm getting no errors, it just isn't changing view. I also tried putting ShowSignUp() on the LoginViewModel but no success.

更新1 ShellViewModel:

Update 1 ShellViewModel:

public class ShellViewModel : Conductor<object>, IHandle<ActionInvokedMessage>
    {
        DispatcherTimer dt = new DispatcherTimer();
        private SplashScreenViewModel _splashVM;
        private LoginViewModel _loginVM;
        private SignUpViewModel _signUpVM;
        private IEventAggregator _eventAggregator;

        public ShellViewModel(SplashScreenViewModel splashVM, LoginViewModel loginVM, SignUpViewModel signUpVM)
        {
            _loginVM = loginVM;
            _signUpVM = signUpVM;
            _splashVM = splashVM;
            ActivateItem(_splashVM);

            dt.Tick += new EventHandler(Dt_Tick);
            dt.Interval = new TimeSpan(0, 0, 2);
            dt.Start();
        }

        private void Dt_Tick(object sender, EventArgs e)
        {
            dt.Stop();
            ActivateItem(_loginVM);
        }

        public ShellViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            _eventAggregator.Subscribe(this);
            ActivateItem(new LoginViewModel(_eventAggregator));
        }

        public void Handle(ActionInvokedMessage message)
        {
            ActivateItem(message.Page);
        }

        public void ShowSignUp()
        {
            ActivateItem(new SignUpViewModel());
        }



    }


推荐答案

您可以使用 EventAggregator 发布指示性代码来实现从LoginViewModel到ShellViewModel的消息以更新UI。

You could achieve this using EventAggregator to publish indicative messages from LoginViewModel to ShellViewModel to update the UI.

首先,您需要定义一个消息类,该消息类将告诉ShellViewModel需要更改哪个ViewModel。例如,

To begin with, you need to define an message class, which would tells the ShellViewModel which ViewModel needs to be changed. For example,

public class ActionInvokedMessage
    {
        public Screen Page { get; set; }
    }

Page属性将指示需要加载哪个屏幕。现在,您可以按以下方式更改LoginViewModel。

The Page property would indicate which Screen needs to be loaded. Now, you could change your LoginViewModel as the following.

public class LoginViewModel: Screen
    {
        private IEventAggregator _eventAggregator;
        public LoginViewModel(IEventAggregator eventAggregator)
        {

            _eventAggregator = eventAggregator;
            _eventAggregator.Subscribe(this);
        }

        public void ShowSignUp()
        {
            _eventAggregator.PublishOnUIThread(new ActionInvokedMessage { Page = new SignupViewModel() }); ;
        }
    }

PublishOnUIThread方法会将消息广播给所有侦听器消息类型ActionInvokedMessage进行更改。下一步将是确保ShellViewModel将监听更改。

The PublishOnUIThread method would broadcast a message to all the listeners of the Message Type ActionInvokedMessage for the change. Next step would be to ensure the ShellViewModel would be listening to the change.

public class ShellViewModel : Conductor<object>, IHandle<ActionInvokedMessage>
    {
        private IEventAggregator _eventAggregator;
        public ShellViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            _eventAggregator.Subscribe(this);
            ActivateItem(new LoginViewModel(_eventAggregator));
        }

        public void Handle(ActionInvokedMessage message)
        {
            ActivateItem(message.Page);
        }

        public void ShowSignUp()
        {
            ActivateItem(new SignupViewModel());
        }


    }

IHandle接口允许我们处理ShellViewModel收到ActionInvokedMessage时需要执行的操作。如代码中所示,这是使用ActivateItem方法加载注册页面的合适位置。

The Implementation of IHandle interface allows us to handle the action that would be required to be executed when the ShellViewModel recieves the ActionInvokedMessage. As seen in the code, this would be an appropriate place to use the ActivateItem method to load the Signup Page.

这篇关于Caliburn Micro内容控件导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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