2017-08-31 23 views
1

我目前正在嘗試實現ImageViewer,可以使用觸摸輸入移動和縮放。 我已經在Code Behind的最近項目中實現了這些功能,但我在MVVM的View Model中努力做到這一點。MVVM中的畫布上的TouchEvents

問題是,對於我的代碼工作,我必須知道同時識別了多少個觸摸輸入。 在我的代碼隱藏我用:

canvas.TouchesCaptured.Count() 

視圖模型不應該不知道視圖中的任何控制,所以通過畫布爲命令參數是不是要走的路。

在畫布旁邊,我需要觸發的TouchEvent的TouchEventArgs來確定TouchEvent在畫布上的位置。 使用棱鏡我能夠將TouchEventArgs放入ViewModel中。

<i:Interaction.Triggers> 
     <i:EventTrigger EventName="TouchDown"> 
      <prism:InvokeCommandAction Command="{Binding TouchDownCommand}" 
     </i:EventTrigger> 
</i:Interaction.Triggers> 

棱鏡:InvokeCommandAction自動將EventArgs設置爲CommandParameter以進行說明。

要確定TouchEvent在畫布上的位置,我需要畫布和TouchEvent。 在我的代碼隱藏看起來就像是:

startingPoint = e.GetTouchPoint(canvas); 

任何人有一個想法,我怎麼能解決這個問題,在不違反MVVM模式?

+0

對於「移動並與觸摸輸入放大」看一看的操縱事件,如[在ManipulationDelta](https://msdn.microsoft.com/en-us/library/system.windows.uielement。 manipulationdelta(v = vs.110).aspx)等。另請參閱[觸摸和操作](https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/input-overview#touch-and -manipulation)。 – Clemens

回答

0

你可以嘗試編寫一個Blend行爲來封裝Canvas事件處理和公開命令(特別是ManipulationDelta)。您甚至可以將屬性添加到公開TouchesCaptured值的行爲(在ManipulationDelta事件期間)。

例如

<Canvas> 
    <i:Interaction.Behaviors> 
     <bhv:CanvasBehavior ManipulationDeltaCommand="{Binding MyViewModelCommand}" TouchPointCount="{Binding MyViewModelTouchPointCount}" /> 
    </i:Interaction.Behaviors> 
</Canvas>