VS 插件开发实现简单的 ViewModel 和 View 之间的切换
1. 前言
我们在前面一篇中有介绍如何打开文件,如果和 ViewModel 与 View 的切换这个场景结合,那么我们也完全有能力写出一段代码来解决。
2. 代码目标
进行 *ViewModel.cs 和 *View.xaml 之间的切换。
3. 可以预见的缺陷
我们仅仅按照文件名进行区分,必不可少地会出现无法对应的问题,整个的项目的结构需要非常死板的遵循。
4. VCmd 代码
标题:MVVM 切换
using EnvDTE;
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class C : VisualCommanderExt.ICommand
{
static string[] _viewEndNameArray = new string[] { "View.xaml", "View.axaml", "UserControl.xaml", "UserControl.axaml", "Page.xaml", "Page.axaml" };
static string[] _viewCsEndNameArray = new string[] {"View.xaml.cs", "View.axaml.cs", "UserControl.xaml.cs", "UserControl.axaml.cs", "Page.xaml.cs", "Page.axaml.cs" };
static string[] _viewNameArray = new string[] { "UserControl", "Page", "View" };
static string[] _viewEndExtensionArray = new string[] { ".xaml", ".axaml" };
public enum FileType
{
Unknown, View, ViewModel, ViewCS,
}
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
var projectItem = DTE.ActiveDocument.ProjectItem;
var projectPath = projectItem.ContainingProject.FullName;
var projectDir = Path.GetDirectoryName(projectPath);
var itemPath = projectItem.FileNames[0];
var relPath = itemPath.Substring(projectDir.Length);
var fileType = GetFileType(relPath);
var convertPath = GetConvertFilePath(fileType, relPath, projectDir);
if (string.IsNullOrWhiteSpace(relPath)) return;
var fullPath = projectDir + convertPath;
if (File.Exists(fullPath))
{
DTE.Documents.Open(fullPath);
}
}
static FileType GetFileType(string relPath)
{
if (_viewEndNameArray.Any(i => relPath.EndsWith(i, StringComparison.OrdinalIgnoreCase))) return FileType.View;
if (_viewCsEndNameArray.Any(i => relPath.EndsWith(i, StringComparison.OrdinalIgnoreCase))) return FileType.ViewCS;
if (relPath.EndsWith("ViewModel.cs", StringComparison.OrdinalIgnoreCase)) return FileType.ViewModel;
return FileType.Unknown;
}
static string GetConvertFilePath(FileType fileType, string relPath, string projectDir)
{
var result = string.Empty;
switch (fileType)
{
case FileType.View:
result = relPath;
foreach (var item in _viewNameArray)
{
result = result.Replace(item, "ViewModel");
}
result = result.Replace(".xaml", ".cs").Replace(".axaml", ".cs");
break;
case FileType.ViewModel:
result = relPath;
foreach (var viewName in _viewNameArray)
{
result = string.Empty;
foreach (var ext in _viewEndExtensionArray)
{
result = relPath.Replace("ViewModel", viewName).Replace(".cs", ext);
var fullPath = projectDir + result;
if (File.Exists(fullPath)) break;
result = string.Empty;
}
if (string.IsNullOrWhiteSpace(result) == false) break;
}
break;
case FileType.ViewCS:
result = relPath;
foreach (var item in _viewNameArray)
{
result = result.Replace(item, "ViewModel");
}
result = result.Replace(".xaml.cs", ".cs").Replace(".axaml.cs", ".cs");
break;
default:
break;
}
return result;
}
}
标签:插件,string,ViewModel,var,VS,result,cs,View,relPath
From: https://www.cnblogs.com/fanbal/p/18252167