2014-06-22 3 views
1

저는 WPF와 MVVM에 대해 처음 소개했습니다. 전제를 이해하는 동안이 많은 것들이 나를 위해 상형 문자를 읽는 것과 같습니다.WPVM에 VTK 응용 프로그램을 작성하여 MVVM을 따르려고합니다.

기본적으로 내 상황은 다음과 같습니다. 이미지 처리/시각화 라이브러리 인 VTK 용 C# 래퍼 인 Activiz를 사용하고 있습니다. 그래서이 라이브러리에는 vtk : RenderWindowControl이라는 WinForms 컨트롤이 있습니다.이 컨트롤은 모든 시각화 기능을 처리하는 클래스가 포함 된 OpenGL 컨트롤입니다. WinForms를 사용하는 것이 더 쉬울 것이라고 생각합니다. 그러나 그것은 실제로 나를위한 선택 사항은 아닙니다.

WPF 응용 프로그램에서 vtk : RenderWindowControl을 사용하려면 코드를 WindowsFormsHost로 밀어 넣은 다음 코드 뒤에 example code과 같이 더 많이 또는 덜 사용할 수 있습니다 (올바른 용어 인 경우). .xaml.cs 파일)

테스트 응용 프로그램에서는 문제가 없지만 실제로는 가능한 경우 MVVM을 따르고 싶습니다. 여기가 내가 벽에 부딪쳤다. "renderControl"이 View 클래스에 있으면 어떻게 참조하고 ViewModel에서 사용할 수 있습니까? 나는 바인딩이 그 질문에 대한 해답이라고 생각하지만, 나는 단순한 유형과 명령에 대해서 그렇게하는 법을 정말로 알고있다.

은 내가 찾은 다른 스레드의 아이디어에 따라, 나는 내 코드 숨김은 다음과 같습니다 this answer

같은 것을 설정 관리 :

내 .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> 

과 같은

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; 
    } 
} 

그래서 두 가지. One, xaml 헤더의 마지막 줄에 오류가 있습니다. "구성원 'RWControl'이 인식되지 않거나 액세스 할 수 없습니다. 나는 이유를 정말로 이해하지 못한다. 둘째, 내가 추측하고있는 것은 ViewModel의 방정식의 절반입니다. VMControlProperty는 어떻게 정의되어야합니까?

적어도 올바른 길을 걷고 있습니까, 아니면 이쪽입니까?

답변

0

일부 컨트롤은 MVVM과 호환되지 않으므로 ViewModel에서 View 인터페이스를 인식하고 직접 상호 작용할 수 있습니다. ViewModel 전체 컨트롤을 열지 마십시오. 테스트를 작성하는 기능이 손상됩니다. 예를 들어 IRenderPanelView와 같은 인터페이스를 맨 위에 배치하고 ViewModel에서 액세스해야하는 기능 만 인터페이스에서 엽니 다. 그런 다음보기에서이 유형의 DP 속성을 만들고 생성자에서 설정 한 다음 xaml의 ViewModel.View 속성에 바인딩 할 수 있습니다.

관련 문제