当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Why caliburn micro calls this method, when it should not do this?

问题描述:

I have a view and viewmodel in caliburn micro as follow:

view:

 <Canvas>

<Rectangle x:Name="CloseApp" Canvas.Top="0" Canvas.Left="0" Fill="green" Opacity="0.2" Height="20" Width="30" />

<Rectangle x:Name="MoveWindow" Canvas.Top="0" Canvas.Left="60" Fill="red" Opacity="0.2" Height="20" Width="964" cal:Message.Attach="[Event MouseLeftButtonDown] = [Action MoveWindow]" />

</Canvas>

and ViewModel

 /// </summary>

public void MoveWindow()

{

Window view = this.GetView(null) as Window;

if (view != null)

{

view.DragMove();

}

}

public void CloseApp()

{

}

I notice that when I click on red rectangle and drag it, after I released left button, Caliburn micro calls CloseApp.

Why is it calling this method when it should do this?

Edit 1:

I found that if the green rect starts from left=1 (canvas.Left="1") this doesn't happen.

网友答案:

Caliburn.Micro falls back to a convention based command model if you don't explicitly specify a message to attach, it will look for a method on your VM that matches the name of the control and automatically wire it up. It will wire up to some events automatically to do this. To override it, either change the name of the Rectangle "CloseApp" or add your own action message on the Rectangle "CloseApp", or change the name of the method called CloseApp().

The next thing the ViewModelBinder does after locating the elements for convention bindings is inspect them for matches to methods on the ViewModel. It does this by using a bit of reflection to get the public methods of the ViewModel. It then loops over them looking for a case-insensitive name match with an element. If a match is found, and there aren’t any pre-existing Interaction.Triggers on the element, an action is attached. The check for pre-existing triggers is used to prevent the convention system from creating duplicate actions to what the developer may have explicitly declared in the markup. To be on the safe side, if you have declared any triggers on the matched element, it is skipped.

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: