在视图和视图模型之间跳转 [英] Jump between view and view model

查看:69
本文介绍了在视图和视图模型之间跳转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 Resharper 或只是"Visual Studio(可能使用宏)在 MVVM 模式中的视图和它的视图模型之间跳转.

Is there a way to make Resharper or "just" Visual Studio (perhaps using a macro) jump between the view and the it's view model in the MVVM pattern.

就像 Resharper 可以使用 F7 和 Shift-F7 在 xaml 和它的代码隐藏之间跳转

Like Resharper can jump between the xaml and it's codebehind using F7 and Shift-F7

我遵循文件这样定位的约定

I follow the convention that the files are located like this

\Views\name\AbcView.xaml
\ViewModels\name\AbcViewModel.xaml

\Views\name\AbcView.xaml
\ViewModels\name\AbcViewModel.xaml

推荐答案

也许我应该先用谷歌搜索一下,here 是一个宏,用于对 .cpp 和.cpp 执行相同的操作..h 文件

Perhaps I should have googled first, here is a macro to do the same thing for .cpp and. .h files

我稍微改变了它并将宏分配给 Ctrl+F7.似乎工作正常

I changed it a bit and assigned the macro to Ctrl+F7. Seems to work fine

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

这篇关于在视图和视图模型之间跳转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆