Caliburn Micro:从虚拟机查询特定于视图的数据

发布于 11-10 18:21 字数 586 浏览 1 评论 0原文

我对 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅冇旳回忆2024-11-17 18:21:51

我可能会看看 IResult 选项。您将有权访问该视图,以便您在后面的代码中拥有的代码可以位于您的结果中,而不是位于您的虚拟机中。

这是来自 ShowDialog 结果的代码。我相信我是从 CM 讨论组获取的。在讨论中搜索 ShowDialog 以获取更多示例。 CM自带的GameLibrary示例也有一些。

  public class ShowDialog : IResult
    {
        private readonly Type _screenType;
        private readonly string _name;        

        [Import]
        public IWindowManager WindowManager { get; set; }

        public ShowDialog(string name)
        {
            _name = name;
        }

        public ShowDialog(Type screenType)
        {
            _screenType = screenType;
        }

        public void Execute(ActionExecutionContext context)
        {
            var screen = !string.IsNullOrEmpty(_name)
                             ? IoC.Get<object>(_name)
                             : IoC.GetInstance(_screenType, null);

            Dialog = screen;
            WindowManager.ShowDialog(screen);

            var deactivated = screen as IDeactivate;
            if (deactivated == null)
                Completed(this, new ResultCompletionEventArgs());
            else
            {
                deactivated.Deactivated += (o, e) =>
                                               {
                                                   if (e.WasClosed)
                                                   {
                                                       Completed(this, new ResultCompletionEventArgs());
                                                   }
                                               };
            }
        }

        public object Dialog { get; private set; }
        public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };

        public static ShowDialog Of<T>()
        {
            return new ShowDialog(typeof (T));
        }
    }

编辑:如果扩展 TextBox,则可以绑定 SelectedText。

 public class TextBoxEx : TextBox
    {
        public static readonly DependencyProperty SelectedTextProperty = DependencyProperty.Register("SelectedText", typeof(string), typeof(TextBoxEx), new PropertyMetadata("oa"));

        public TextBoxEx()
        {
            SelectionChanged += UpdateDependencyProperty;
        }

        private void UpdateDependencyProperty(object sender, RoutedEventArgs e)
        {
            SelectedText = base.SelectedText;
        }

        public new string SelectedText
        {
            get { return GetValue(SelectedTextProperty).ToString(); }
            set { SetValue(SelectedTextProperty, base.SelectedText); }
        }
    }

然后:

 <SLTest:TextBoxEx x:Name="MyTextBox2"
                 Grid.Row="1"
                 Width="200"
                 SelectedText="{Binding SelectedText, Mode=TwoWay}"
                 Text="This is some text." />

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.

  public class ShowDialog : IResult
    {
        private readonly Type _screenType;
        private readonly string _name;        

        [Import]
        public IWindowManager WindowManager { get; set; }

        public ShowDialog(string name)
        {
            _name = name;
        }

        public ShowDialog(Type screenType)
        {
            _screenType = screenType;
        }

        public void Execute(ActionExecutionContext context)
        {
            var screen = !string.IsNullOrEmpty(_name)
                             ? IoC.Get<object>(_name)
                             : IoC.GetInstance(_screenType, null);

            Dialog = screen;
            WindowManager.ShowDialog(screen);

            var deactivated = screen as IDeactivate;
            if (deactivated == null)
                Completed(this, new ResultCompletionEventArgs());
            else
            {
                deactivated.Deactivated += (o, e) =>
                                               {
                                                   if (e.WasClosed)
                                                   {
                                                       Completed(this, new ResultCompletionEventArgs());
                                                   }
                                               };
            }
        }

        public object Dialog { get; private set; }
        public event EventHandler<ResultCompletionEventArgs> Completed = delegate { };

        public static ShowDialog Of<T>()
        {
            return new ShowDialog(typeof (T));
        }
    }

edit: If you extend TextBox you can bind SelectedText.

 public class TextBoxEx : TextBox
    {
        public static readonly DependencyProperty SelectedTextProperty = DependencyProperty.Register("SelectedText", typeof(string), typeof(TextBoxEx), new PropertyMetadata("oa"));

        public TextBoxEx()
        {
            SelectionChanged += UpdateDependencyProperty;
        }

        private void UpdateDependencyProperty(object sender, RoutedEventArgs e)
        {
            SelectedText = base.SelectedText;
        }

        public new string SelectedText
        {
            get { return GetValue(SelectedTextProperty).ToString(); }
            set { SetValue(SelectedTextProperty, base.SelectedText); }
        }
    }

then:

 <SLTest:TextBoxEx x:Name="MyTextBox2"
                 Grid.Row="1"
                 Width="200"
                 SelectedText="{Binding SelectedText, Mode=TwoWay}"
                 Text="This is some text." />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文