Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF中编写VTK应用程序,尝试遵循MVVM_C#_Wpf_Xaml_Mvvm_Vtk - Fatal编程技术网

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

C# 在WPF中编写VTK应用程序,尝试遵循MVVM,c#,wpf,xaml,mvvm,vtk,C#,Wpf,Xaml,Mvvm,Vtk,所以我对WPF和MVVM还很陌生,虽然我理解这个前提,但很多这些东西就像是在帮我读象形文字 基本上,我的情况是这样的:我正在使用Activiz,VTK的c#包装器,这是一个图像处理/可视化库。因此,在这个库中,有一个名为vtk:RenderWindowControl的WinForms控件,它是一个opengl控件,包含处理所有可视化功能的类。我认为只使用WinForms会更容易,但这对我来说不是一个真正的选择 因此,要在WPF应用程序中使用vtk:RenderWindowControl,我只需要

所以我对WPF和MVVM还很陌生,虽然我理解这个前提,但很多这些东西就像是在帮我读象形文字

基本上,我的情况是这样的:我正在使用Activiz,VTK的c#包装器,这是一个图像处理/可视化库。因此,在这个库中,有一个名为vtk:RenderWindowControl的WinForms控件,它是一个opengl控件,包含处理所有可视化功能的类。我认为只使用WinForms会更容易,但这对我来说不是一个真正的选择

因此,要在WPF应用程序中使用vtk:RenderWindowControl,我只需要将它放入WindowsFormsHost中,然后我就可以或多或少地使用它,就像代码背后的一样(如果这是.xaml.cs文件的正确术语)

对于测试应用程序来说,这很好,但实际上,如果可能的话,我希望遵循MVVM。这就是我碰到墙的地方。如果“renderControl”存在于View类中,我如何引用它并从ViewModel中使用它?我认为绑定是这个问题的答案,但我只知道如何对简单的类型和命令进行绑定

根据我发现的另一条线索中的想法,我成功地建立了类似

我的代码隐藏如下所示:

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;
    }
}
我的.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>

所以有两件事。首先,xaml头的最后一行是一个错误,“无法识别或访问成员'RWControl'。我真的不明白为什么。第二,因为我猜测的是等式的ViewModel的一半,VMControlProperty应该如何定义


我至少在这方面是正确的,还是偏离了基准?

有些控件对MVVM不友好,您已经让ViewModel了解视图界面,并允许直接与它交互。不要将整个控件打开到ViewModel,否则会破坏编写测试的能力,请在顶部放置一个接口,例如iEnderPanelView,并在接口中仅打开需要从ViewModel访问的功能。然后,您可以在视图中创建此类型的DP属性,在构造函数中设置它,并将其绑定到xaml中的ViewModel.view属性