有没有办法让Resharper或“只是”Visual Studio(可能使用宏)在视图和MVVM模式中的视图模型之间跳转。
像Resharper可以使用F7和Shift-F7在xaml和它的代码隐藏之间跳转
我遵循文件所在的约定
\视图\名\ AbcView.xaml
\的ViewModels \名\ AbcViewModel.xaml
答案 0 :(得分:2)
也许我应该先用Google搜索,here是一个宏来为.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
答案 1 :(得分:1)
如果将多个ViewModel关联到同一个View,该怎么办? :)
你可以简单地使用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"
属性)。
答案 2 :(得分: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 旁边。 ^ _ ^