简体   繁体   English

在WPF中编写VTK应用程序,尝试遵循MVVM

[英]writing a VTK aplication in WPF, trying to follow MVVM

So I'm pretty new to WPF and MVVM, and while I understand the premise, a lot of this stuff is like trying to read hieroglyphs for me. 因此,我对WPF和MVVM还是很陌生,虽然我了解前提,但其中很多东西都像是试图为我阅读象形文字。

Basically, my situation is this: I'm using Activiz, ac# wrapper for VTK, which is an image processing/visualization library. 基本上,我的情况是这样的:我正在使用Activiz,VTK的ac#包装器,这是一个图像处理/可视化库。 So, in this library, there's a WinForms control called vtk:RenderWindowControl, which is an opengl control containing the class that handles all of the visualization functionality. 因此,在此库中,有一个名为vtk:RenderWindowControl的WinForms控件,这是一个opengl控件,其中包含处理所有可视化功能的类。 I think it'd be easier to just use WinForms, but that's not really an option for me. 我认为仅使用WinForms会更容易,但这对我来说并不是一个选择。

So, to use vtk:RenderWindowControl in a WPF application, I just need to shove it into a WindowsFormsHost and then I can use it more or less just like the example code , in the code behind (if that's the correct term for the .xaml.cs file) 因此,要在WPF应用程序中使用vtk:RenderWindowControl,我只需要将其推到WindowsFormsHost中,然后就可以像后面的代码一样使用它,或多或少地像示例代码一样(如果这是.xaml的正确术语) .cs文件)

That's fine for a test app, but in practice, I'd like to follow MVVM if possible. 这对于测试应用程序来说很好,但是在实践中,如果可能的话,我想遵循MVVM。 This is where I've run into a wall. 这是我碰壁的地方。 If "renderControl" lives in the View class, how can I reference it and use it from the ViewModel? 如果“ renderControl”位于View类中,如何在ViewModel中引用它并使用它? I think binding is the answer to that question, but I only really know how to do that for simple types and commands. 我认为绑定是该问题的答案,但我只真正知道如何针对简单的类型和命令执行此操作。

Following ideas in another thread I found, I managed to set up something like this answer 按照我发现的另一个主题中的想法,我设法建立了这样的答案

My codebehind looks like this: 我的后台代码如下所示:

public partial class RenderPanel_View : UserControl
{
    public static readonly new DependencyProperty RWControlProperty =
        DependencyProperty.Register("RWControl", typeof(RenderWindowControl), typeof(RenderPanel_View), new PropertyMetadata(null));

    public RenderWindowControl RWControl
    {
        get { return (RenderWindowControl)GetValue(RWControlProperty); }
        set { SetValue(RWControlProperty, value); }
    }

    public RenderPanel_View()
    {
        // This is necessary to stop the rendercontrolwindow from trying to load in the 
        // designer, and crashing the Visual Studio. 
        if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) {
            this.Height = 300;
            this.Width = 300;
            return;
        }

        InitializeComponent();
        this.RWControl = new RenderWindowControl();
        this.RWControl.Dock = System.Windows.Forms.DockStyle.Fill;
        this.WFHost.Child = this.RWControl;
    }
}

My .xaml looks like this 我的.xaml看起来像这样

<UserControl x:Class="vtkMVVMTest.RenderPanel.RenderPanel_View"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:vtk="clr-namespace:Kitware.VTK;assembly=Kitware.VTK"
         xmlns:rp="clr-namespace:vtkMVVMTest.RenderPanel"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300"
         RWControl="{Binding VMControlProperty}">
    <Grid>
        <WindowsFormsHost x:Name ="WFHost"/>
    </Grid>
</UserControl>

So two things. 有两件事。 One, That last line of the xaml header is an error, "The member 'RWControl' is not recognized or accessible". 一个,xaml标头的最后一行是一个错误,“无法识别或访问成员'RWControl'。 I don't really understand why. 我真的不明白为什么。 Second, for what I'm guessing is the ViewModel half of the equation, how is VMControlProperty supposed to be defined? 其次,由于我猜想ViewModel是等式的一半,应该如何定义VMControlProperty?

Am I at least on the right track here, or is this way off base? 我至少在这里是正确的轨道,还是这种偏离基准?

Some controls are not MVVM friendly and you have make the ViewModel aware of View interface and allow interact with it directly. 某些控件不是MVVM友好的,并且您已使ViewModel意识到View接口并允许直接与其交互。 Do not open the whole control to the ViewModel it will ruin the ability to write tests, put an interface on top for example IRenderPanelView and open in the interface only the functionality you need to access from ViewModel. 不要对ViewModel打开整个控件,这会破坏编写测试的能力,将一个接口放在例如IRenderPanelView的顶部,并仅在该接口中打开您需要从ViewModel访问的功能。 You can then create a DP property of this type in the view, set it in the constructor and bind it to ViewModel.View property in xaml. 然后,您可以在视图中创建此类型的DP属性,在构造函数中进行设置并将其绑定到xaml中的ViewModel.View属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM