Caliburn.Micro Conductor:触发/动作触发超过预期 [英] Caliburn.Micro Conductor: Trigger/Action firing more than exptected

查看:157
本文介绍了Caliburn.Micro Conductor:触发/动作触发超过预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个非常简单的示例应用程序组合在一起,但是它无法按预期工作。

I'm trying to put together a very simple example app, but it is not working as intented.

这里是场景:

Caliburn.Micro,MVVM,Silverlight 5.0- https://caliburnmicro.codeplex.com/wikipage?title = Screens%2c%20Conductors%20and%20Composition& referringTitle =文档(简单导航)

Caliburn.Micro, MVVM, Silverlight 5.0 - simple Conductor example from https://caliburnmicro.codeplex.com/wikipage?title=Screens%2c%20Conductors%20and%20Composition&referringTitle=Documentation (Simple Navigation)

我只是给出了一个实时示例:
< a href = https://db.tt/kTIjKvRx rel = nofollow> https://db.tt/kTIjKvRx

I just put together a live example: https://db.tt/kTIjKvRx

-> hit enter in textbox (messagebox displays 1x)
-> go to master and go back to login
-> hit enter in textbox (messagebox displays 2x!)



ShellViewModel



ShellViewModel

public class ShellViewModel : Conductor<object> {
    public ShellViewModel() {
        ShowLogin();
    }

    public void ShowLogin() {
        ActivateItem(new LoginViewModel());
    }

    public void ShowMaster() {
        ActivateItem(new MasterViewModel());
    }
}



编辑:



与以下结果相同:

Same results with this:

public class ShellViewModel : Conductor<object> {
    public ShellViewModel() {
        LoginModel = new LoginViewModel();
        MasterModel = new MasterViewModel();
        ShowLogin();
    }

    public LoginViewModel LoginModel { get; set; }
    public MasterViewModel MasterModel { get; set; }

    public void ShowLogin() {
        ActiveItem = LoginViewModel;
    }

    public void ShowMaster() {
        ActiveItem = MasterViewModel;
    }
}



ShellView



ShellView

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" x:Class="Hele.ShellView"
    d:DesignWidth="438" d:DesignHeight="200">
<Grid>
    <StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Top">
        <Button x:Name="ShowLogin" Width="100" Height="30" Content="Login"/>
        <Button x:Name="ShowMaster" Width="100" Height="30" Content="Master"/>

        <ContentControl x:Name="ActiveItem" " />
</Grid>
</UserControl>



LoginView



LoginView

<UserControl ... >

<Grid x:Name="LayoutRoot">
    <StackPanel>
    <TextBlock>Login</TextBlock>
        <TextBox x:Name="Message" Text="{Binding Message, Mode=TwoWay}" >

            <i:Interaction.Triggers>
                <iex:KeyTrigger Key="Enter">
                    <cal:ActionMessage MethodName="Login" />
                </iex:KeyTrigger>
            </i:Interaction.Triggers>

        </TextBox>        
    </StackPanel>
</Grid>
</UserControl>



LoginViewModel



LoginViewModel

public class LoginViewModel : Screen
{
    public string Message { get; set; }

    public void Login()
    {
        MessageBox.Show("login messagebox");
    }
}

MasterView and MasterViewModel are just empty, nothing interesting there.

上面的示例运行良好,单击登录按钮显示登录视图后,在主上显示主视图

The above example just works fine, after clicking on Login button shows login view, on Master shows master view.

在登录视图中,有一个带有事件触发器的文本框。按下Enter键后,它会从viewmodel中调用一个方法并显示一个消息框。

In the Login View there is a textbox which has an event trigger. After hitting Enter key, it calls a method from viewmodel and displays a messagebox.

何时进入主视图并返回登录末尾,按Enter键-它两次显示消息框!

When going to master view and going back to login end hitting Enter - it shows the messagebox twice!

要进入主视图并再次返回->它将显示3x。

Going to master and again back -> it will display it 3x.. and so on.

我认为触发器应该只触发一次。我们如何实现此行为?

I think the Trigger should fire only once. How can we achieve this behavior?

推荐答案

我认为这是因为您每次加载视图时都要执行ActivateItem来重新绑定事件处理程序的视图。尝试改为设置ActiveItem属性(具有x:Name = ActiveItem的ContentControl绑定到该属性)。还可以尝试使用公共变量来保存您的视图模型:

I think it's because your doing the ActivateItem each time your loading the view which rebinds the event handlers to the view. Try setting the ActiveItem property instead (which the ContentControl with x:Name="ActiveItem" is bound to). Also try using public variables to hold your view models:

public class ShellViewModel : Conductor<object> {
    public ShellViewModel() {
        ShowLogin();
    }

    public LoginViewModel { get; set; }
    public MasterViewModel { get; set; }

    public void ShowLogin() {
        ActiveItem = LoginViewModel;
    }

    public void ShowMaster() {
        ActiveItem = MasterViewModel;
    }
}

编辑

EDIT

我能够重现此内容,这似乎与表达式交互有关。如果我使用附加的常规EventTrigger进行按键操作,则效果很好:

I was able to reproduce this and it seems to be an issue with the Expression Interactions. If I use a regular EventTrigger attached to key down it works fine:

        <TextBox Width="50" Text="{Binding Message, Mode=TwoWay}" >
            <i:Interaction.Triggers>
                <!--<iex:KeyTrigger Key="Enter">
                    <cm:ActionMessage MethodName="Page1KeyPress" />
                </iex:KeyTrigger>-->
                <i:EventTrigger EventName="KeyDown">
                    <cm:ActionMessage MethodName="Page1KeyPress" >
                        <cm:Parameter Value="$source" />
                        <cm:Parameter Value="$eventArgs" />
                    </cm:ActionMessage>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox>

    public void Page1KeyPress(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
            MessageBox.Show("Page 1 Key Press");
    }

这篇关于Caliburn.Micro Conductor:触发/动作触发超过预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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