2011-09-06 27 views
4

是否有一種方法可以在視圖和MVVM模式中的視圖模型之間創建Resharper或「只是」Visual Studio(可能使用宏)跳轉。在視圖和視圖模型之間跳轉

ReSharper的一樣可以在XAML之間跳轉,它使用F7的代碼隱藏和Shift-F7

我按照約定,該文件位於這樣

\查看\用戶名\ AbcView.xaml
\ ViewModels \ name \ AbcViewModel.xaml

+0

F7和Shift-F7是來自VisualStudio的關鍵手勢,而不是來自ReSharper,據我所知 – WaltiD

+0

我真的不知道,但我喜歡使用「最近的文件」視圖(out-of-我的盒子映射是Ctrl-,) – Carsten

回答

2

也許我應該先GOOGLE一下,here是一個宏,它爲.cpp和.cpp做同樣的事情。 .h文件

我改了一下,並將宏指定給Ctrl + F7。似乎工作正常

Public Sub SwitchBetweenViewAndViewModel() 
    '===================================================================== 
    ' If the currently open document is a view or viewmodel, attempts to 
    ' switch between them 
    '===================================================================== 
    Dim currentDocument As String 
    Dim targetDocument As String 

    currentDocument = ActiveDocument.FullName 

    If currentDocument.ToLower().Contains("\views\") And _ 
     (currentDocument.EndsWith("View.xaml", StringComparison.InvariantCultureIgnoreCase) Or _ 
     currentDocument.EndsWith("View.xaml.cs", StringComparison.InvariantCultureIgnoreCase)) Then 
     targetDocument = currentDocument 
     targetDocument = targetDocument.Replace(".xaml.cs", "") 
     targetDocument = targetDocument.Replace(".xaml", "") 
     targetDocument = targetDocument.Replace("\Views\", "\ViewModels\") 
     targetDocument = targetDocument + "Model.cs" 
    ElseIf currentDocument.ToLower().Contains("\viewmodels\") And currentDocument.EndsWith(".cs", StringComparison.InvariantCultureIgnoreCase) Then 
     targetDocument = currentDocument 
     targetDocument = targetDocument.Replace("\ViewModels\", "\Views\") 
     targetDocument = targetDocument.Replace("ViewModel.cs", "View.xaml") 
    End If 

    If System.IO.File.Exists(targetDocument) Then 
     OpenDocument(targetDocument) 
    End If 
End Sub 

'===================================================================== 
' Given a document name, attempts to activate it if it is already open, 
' otherwise attempts to open it. 
'===================================================================== 
Private Sub OpenDocument(ByRef documentName As String) 
    Dim document As EnvDTE.Document 
    Dim activatedTarget As Boolean 
    activatedTarget = False 

    For Each document In Application.Documents 
     If document.FullName = documentName And document.Windows.Count > 0 Then 
      document.Activate() 
      activatedTarget = True 
      Exit For 
     End If 
    Next 
    If Not activatedTarget Then 
     Application.Documents.Open(documentName, "Text") 
    End If 
End Sub 
+0

對於VisualStudo> 2012宏默認情況下不可用。這是一個擴展名:https://blogs.msdn.microsoft.com/visualstudio/2016/05/11/macros-extension-open-sourced-in-visual-studio-2015/ – Stefan

1

如果多個ViewModel被關聯到相同的視圖怎麼辦? :)

U可以簡單地使用DesignInstance標記擴展來獲得R·導航和代碼完成在結合工作做好:

namespace WpfApplication1.ViewModels 
{ 
    public class PersonViewModel 
    { 
    public string Name { get; set; } 
    public int Age  { get; set; } 
    } 
} 

以及相應的視圖:

<Window x:Class="WpfApplication1.Views.PersonView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="PersonInfo" Height="300" Width="300" 

     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 

     xmlns:vms="clr-namespace:WpfApplication1.ViewModels" 

     d:DataContext="{d:DesignInstance Type=vms:PersonViewModel}"> 

    <UniformGrid> 
    <TextBlock Grid.Row="0" Grid.Column="0" Text="Name" /> 
    <TextBlock Grid.Row="0" Grid.Column="1" Text="Age" /> 
    <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Name}" /> 
    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Age}" /> 
    </UniformGrid> 

</Window> 

現在R·可以驗證所有引用ViewModel屬性的綁定,幫助您完成屬性名稱,並且還可以直接導航到此屬性。此外,使用R#快速修復上述所有代碼。在...中指定DataContext類型...快速修復(在未解析的綁定表達式中)。

p.s. 查看仍然不依賴於ViewModel,因爲所有設計時屬性將在編譯後被刪除(通過mc:Ignorable="d"屬性)。

+0

這是R#6功能嗎? – Karsten

+0

'{d:DesignerInstance}'/'{d:DesignData}'僅受R#6支持。0,但早期版本可以在其他場景中推斷'DataContext'類型,比如通過'ElementName'綁定到資源或其他元素。 – ControlFlow

1

使用@Karstens回答我改變了文件之間切換到子都能夠的.cpp和.h文件,以及視圖和視圖模型文件來回切換!

如果你想要做的一樣,遵循the instructions in his link但使用這個子代替:

'===================================================================== 
' If the currently open document is a CPP or an H file, attempts to 
' switch between the CPP and the H file. 
' 
' If the currently open document is a View.xml or an ViewModel.cs file, attempts to 
' switch between the View and the ViewModel file. 
'===================================================================== 
Sub SwitchBetweenAssociatedFiles() 
    Dim currentDocument As String 
    Dim targetDocument As String 

    currentDocument = ActiveDocument.FullName 

    If currentDocument.EndsWith(".cpp", StringComparison.InvariantCultureIgnoreCase) Then 
     targetDocument = Left(currentDocument, Len(currentDocument) - 3) + "h" 
    ElseIf currentDocument.EndsWith(".h", StringComparison.InvariantCultureIgnoreCase) Then 
     targetDocument = Left(currentDocument, Len(currentDocument) - 1) + "cpp" 
    ElseIf currentDocument.EndsWith("View.xaml", StringComparison.InvariantCultureIgnoreCase) Then 
     targetDocument = currentDocument.Replace("\Views\", "\ViewModels\") 
     targetDocument = targetDocument.Replace("\View\", "\ViewModel\") 
     targetDocument = targetDocument.Replace("View.xaml", "ViewModel.cs") 
    ElseIf currentDocument.EndsWith("ViewModel.cs", StringComparison.InvariantCultureIgnoreCase) Then 
     targetDocument = currentDocument.Replace("\ViewModels\", "\Views\") 
     targetDocument = targetDocument.Replace("\ViewModel\", "\View\") 
     targetDocument = targetDocument.Replace("ViewModel.cs", "View.xaml") 
    End If 

    If System.IO.File.Exists(targetDocument) Then 
     OpenDocument(targetDocument) 
    End If 

End Sub 

我賦予它Alt +§這是在我的配置自由。方便在旁邊Alt + Tab。^_^