创建了一个全新的Visual Studio 2012 Ultimate SP2 MVC4项目,但无法让CSS类选择器智能感知起作用吗?
当我输入<p class="m"
....我应该让“myClass”类出现在intellisense下拉列表中,但没有任何反应。
我在下面列出的文件是:\Views\Shared\_Layout.cshtml
任何想法?
编辑:已在全新的Windows 7系统(在Mac OSX并行8上运行)上重新安装VS2012,并且仍然以相同的方式运行。 MVC 3项目似乎也是如此。
已安装扩展程序:
答案 0 :(得分:1)
尝试为Visual Studio 2012添加Web Essentials 2012扩展程序:http://visualstudiogallery.msdn.microsoft.com/07d54d12-7133-4e15-becb-6f451ea3bea6?SRC=VSIDE
和/或
尝试添加Microsoft Web Developer Tools扩展程序。
我有这两个并且使用相同的例子,intellisense就像魅力一样。
答案 1 :(得分:1)
我尝试了上述所有补救措施和建议。这些都不适用于我的环境。根据微软(在Microsoft connect的错误ID 781048下),他们没有为MVC / Razor文件实现CSS类intellisense,但正在努力将其包含在未来的版本中。
我有一个10分钟的网络广播示例,扩展了VS2012 intellisense,增加了一个可以为您的VS2012环境添加智能感知的解决方案:a Visual Studio Intellisense Extension
网络广播使用MEF扩展Visual Studio以添加智能感知完成源,该源扫描当前加载的项目以获取CSS类名作为智能感知完成集。这是css完成源类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Operations;
using Microsoft.VisualStudio.Utilities;
using EnvDTE;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Collections.Specialized;
namespace CssClassIntellisense
{
internal class cssClassList
{
public string cssFileName { get; set; } //Intellisense Statement Completion Tab Name
public HashSet<string> cssClasses { get; set; }
}
internal class CssClassCompletionSource : ICompletionSource
{
private CssClassCompletionSourceProvider m_sourceProvider;
private ITextBuffer m_textBuffer;
private List<Completion> m_compList;
private Project m_proj;
private string m_pattern = @"(?<=\.)[A-Za-z0-9_-]+(?=\ {|{|,|\ )";
private bool m_isDisposed;
//constructor
public CssClassCompletionSource(CssClassCompletionSourceProvider sourceProvider, ITextBuffer textBuffer, Project proj)
{
m_sourceProvider = sourceProvider;
m_textBuffer = textBuffer;
m_proj = proj;
}
public void AugmentCompletionSession(ICompletionSession session, IList<CompletionSet> completionSets)
{
ITextSnapshot snapshot = session.TextView.TextSnapshot;
SnapshotPoint currentPoint = (SnapshotPoint)session.GetTriggerPoint(snapshot);
if (TargetAttribute.Inside(currentPoint))
{
var hash = new List<cssClassList>();
//read any .css project file to get a distinct list of class names
if (m_proj != null)
foreach (ProjectItem _item in m_proj.ProjectItems)
{
getCssFiles(_item, hash);
}
//Scan Current Editor's text buffer for any inline css class names
cssClassList cssclasslist = ScanTextForCssClassName(
"Inline", snapshot.GetText());
//If file had any css class names add to hash of files with css class names
if (cssclasslist != null)
hash.Add(cssclasslist);
var _tokenSpanAtPosition = FindTokenSpanAtPosition(session.GetTriggerPoint(m_textBuffer), session);
foreach (cssClassList _cssClassList in hash)
{
m_compList = new List<Completion>();
foreach (string str in _cssClassList.cssClasses.OrderBy(x => x)) //alphabetic sort
m_compList.Add(new Completion(str, str, str, null, null));
completionSets.Add(new CompletionSet(
_cssClassList.cssFileName, //the non-localized title of the tab
_cssClassList.cssFileName, //the display title of the tab
_tokenSpanAtPosition,
m_compList,
null));
}
}
}
private ITrackingSpan FindTokenSpanAtPosition(ITrackingPoint point, ICompletionSession session)
{
SnapshotPoint currentPoint = (session.TextView.Caret.Position.BufferPosition) - 1;
ITextStructureNavigator navigator = m_sourceProvider.NavigatorService.GetTextStructureNavigator(m_textBuffer);
TextExtent extent = navigator.GetExtentOfWord(currentPoint);
return currentPoint.Snapshot.CreateTrackingSpan(extent.Span, SpanTrackingMode.EdgeInclusive);
}
private void getCssFiles(ProjectItem proj, List<cssClassList> hash)
{
foreach (ProjectItem _item in proj.ProjectItems)
{
if (_item.Name.EndsWith(".css") &&
!_item.Name.EndsWith(".min.css"))
{
//Scan File's text contents for css class names
cssClassList cssclasslist = ScanTextForCssClassName(
_item.Name.Substring(0, _item.Name.IndexOf(".")),
System.IO.File.ReadAllText(_item.get_FileNames(0))
);
//If file had any css class names add to hash of files with css class names
if (cssclasslist != null)
hash.Add(cssclasslist);
}
//recursively scan any subdirectory project files
if (_item.ProjectItems.Count > 0)
getCssFiles(_item, hash);
}
}
private cssClassList ScanTextForCssClassName(string FileName, string TextToScan)
{
Regex rEx = new Regex(m_pattern);
MatchCollection matches = rEx.Matches(TextToScan);
cssClassList cssclasslist = null;
if (matches.Count > 0)
{
//create css class file object to hold the list css class name that exists in this file
cssclasslist = new cssClassList();
cssclasslist.cssFileName = FileName;
cssclasslist.cssClasses = new HashSet<string>();
}
foreach (Match match in matches)
{
//creat a unique list of css class names
if (!cssclasslist.cssClasses.Contains(match.Value))
cssclasslist.cssClasses.Add(match.Value);
}
return cssclasslist;
}
public void Dispose()
{
if (!m_isDisposed)
{
GC.SuppressFinalize(this);
m_isDisposed = true;
}
}
}
}
作为一个FYI,您还可以使用Resharper解决此问题。但这是需要为Visual Studio购买的第三方产品
答案 2 :(得分:0)
只是CSS intellisense失败了还是在整个Visual Studio中完全停止了?
我有一个类似的问题影响了我的整个Visual Studio 2012.它还有一段时间,但我记得从我的appdata中删除了一个文件夹。看看这个链接,希望它会有所帮助: http://www.haneycodes.net/visual-studio-2012-intellisense-not-working-solved/
答案 3 :(得分:0)
对于Razor视图,你不会在VS2012中获得CSS的智能感知。有一种使用intellisense的解决方法。只需使用ASPX视图引擎创建一个测试视图(.aspx)并在其中包含您的css文件。现在intellisense将在新的aspx视图中工作。您所要做的就是将css类从aspx复制粘贴到Razor视图(.cshtml或.vbhtml)。我希望这会有所帮助。