Caliburn Micro:从虚拟机查询特定于视图的数据
我对 CM 完全陌生,为了学习它,我正在将应用程序从 MVVM light 迁移到 Caliburn Micro。在我的原始代码中,我有一个虚拟机,它响应一些 UI 操作(通过命令)以将某些文本替换为字符串。该位置由视图使用文本框选择给出。
因此,VM 具有 (1) 一个表示文本框文本的绑定字符串属性,(2) 另一个表示要添加的新文本的绑定字符串属性,以及 (3) 需要知道选择开始和长度以便替换右侧部分与新文本的文本。
在我的原始代码中,我在 VM 命令实现中发送了一个自定义的 DialogMessage 派生对象,其中包含几个用于选择数据的属性:发出命令时,发送消息,视图接收它并用其文本框填充它选择开始和长度;然后VM被回调并且可以使用这些数据。
在 CM 中实现这一点的最佳方法是什么?我希望虚拟机对视图保持不可知,因此我不太喜欢从中访问视图的想法。我宁愿选择像上面这样的基于“消息”的机制,但我不确定如何在 CM 中实现它:我可能会查看 IResult,但我找到的大多数示例都与协程和相关我不确定如何将虚拟机的 void ReplaceText() 方法与后面的视图代码关联起来。
谁能给我指出正确的方向,和/或一些关于虚拟机“命令”方法和视图之间类似对话框交互的代码示例?谢谢!
I'm completely new to CM and also to learn it I'm migrating an application from MVVM light to Caliburn Micro. In my original code, I had a VM which responds to some UI actions (via commands) to replace some text into a string. The position is given by the view, using the textbox selection.
So the VM has (1) a bound string property representing the textbox's text, (2) another bound string property to represent the new text to be added, and (3) needs to know selection start and length in order to replace the right portion of text with the new one.
In my original code, I had a custom DialogMessage-derived object sent in the VM command implementation with a couple of properties for selection data: when the command was issued, the message was sent, and the view received it and filled it with its textbox selection start and length; then the VM was called back and could use these data.
Which would be the best way of implementing this in CM? I'd prefer the VM to remain agnostic of the view, so I don't like too much the idea of accessing the view from it. I'd rather opt for a "message"-based mechanism like the above, but I'm not sure how I can implement it in CM: I would probably look at IResult, but most of the samples I find are related to coroutines and I'm not sure how to relate the void ReplaceText() method of the VM to the view code behind.
Could anyone point me in the right direction, and/or to some code samples about dialog-like interactions between VM 'command' methods and view? Thanks!
我可能会看看 IResult 选项。您将有权访问该视图,以便您在后面的代码中拥有的代码可以位于您的结果中,而不是位于您的虚拟机中。
这是来自 ShowDialog 结果的代码。我相信我是从 CM 讨论组获取的。在讨论组中搜索 ShowDialog 以获取更多示例。 CM自带的GameLibrary示例也有一些。
编辑:如果扩展 TextBox,则可以绑定 SelectedText。
然后:
I'd probably look at the IResult option. You'll have access to the view so code that you would have had in the code behind can be in your Result and not in your VM.
Here is code from a ShowDialog result. I believe I grabbed it from the CM discussion group. Search the discussion group for ShowDialog for more examples. The GameLibrary sample that comes with CM also has some.
edit: If you extend TextBox you can bind SelectedText.
then: