wpf 有没有办法注释掉包含注释的 XAML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4474892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to comment out XAML that contains comments?
提问by tmoltzan
I am fairly new to WPF and using XAML. I get really frustrated that I can not select a chunk of XAML and comment it out in Visual Studio 2010 using the comment button in the toolbar if the highlighted section already includes some comments.
我对 WPF 和使用 XAML 相当陌生。如果突出显示的部分已经包含一些注释,我无法选择一大块 XAML 并在 Visual Studio 2010 中使用工具栏中的注释按钮将其注释掉,这让我感到非常沮丧。
Other languages allow you to nest comments inside of comments with no issue. Is there a way to comment out a comment in XAML using Visual Studio 2010?
其他语言允许您在评论中嵌套评论而不会出现问题。有没有办法使用 Visual Studio 2010 在 XAML 中注释掉注释?
采纳答案by devdigital
No, there is no way of having nested comments in XAML.
不,没有办法在 XAML 中嵌套注释。
You could use the mc:Ignorable attribute on your root element, and any attribute or element prefixed with that value will be ignored E.g:
您可以在根元素上使用 mc:Ignorable 属性,并且任何以该值为前缀的属性或元素都将被忽略,例如:
<UserControl ...
mc:Ignorable="i">
<!-- Ignore Text attribute -->
<TextBlock i:Text="Hello" />
<!-- Ignore entire button -->
<i:Button>
</i:Button>
</UserControl>
Note that blend sets the mc:Ignorable attributes value to 'd', so you'll need to use e.g. mc:Ignorable="d i"
请注意,混合将 mc:Ignorable 属性值设置为 'd',因此您需要使用例如 mc:Ignorable="d i"
回答by Louis
It is very unfortunate that the comment feature is not smarter than this when it comes to a block that already contains some commented out lines in XML.
非常不幸的是,当涉及到一个已经包含一些 XML 中注释掉的行的块时,注释功能并不比这更智能。
A fairly painless workaround to this problem can be to use regular expressions:
这个问题的一个相当轻松的解决方法是使用正则表达式:
- Select the block of XAML code you want to comment out.
- Click on the comment button from Visual Studio tool bar
- Keeping your commented out block of text selected:
- Open the Find/Replace dialog box (CTRL + SHIFT + H)
- In the Find Options, select the "Use regular expression" check box.
- Ensure the "Look In:" combo box is set with "Selection".
- In your "Find" field, enter:
\<\!\-\-(.*)\-\-\>
- In your "Replace" field, enter:
--><!--$1--><!--
- Click the "replace all" button
- 选择要注释掉的 XAML 代码块。
- 单击 Visual Studio 工具栏中的注释按钮
- 保持选中的注释掉的文本块:
- 打开查找/替换对话框 (CTRL + SHIFT + H)
- 在“查找选项”中,选中“使用正则表达式”复选框。
- 确保“查找范围:”组合框设置为“选择”。
- 在“查找”字段中,输入:
\<\!\-\-(.*)\-\-\>
- 在“替换”字段中,输入:
--><!--$1--><!--
- 单击“全部替换”按钮
This will wrap any commented out lines within your block with the closing comment tag at the begining and the opening comment tag at the end, ensuring the block of text preceding this comment is valid and the one following it is too.
这将用开头的结束注释标记和末尾的开头注释标记将块中的所有注释掉的行包裹起来,确保此注释前面的文本块有效,后面的也是有效的。
To remove the comments and return to your original block of XAML, use the regular expression first, but with the reverse logic:
要删除注释并返回到原始 XAML 块,请先使用正则表达式,但使用相反的逻辑:
- Find field:
\-\-\>\<!\-\-(.*)\-\-\>\<\!\-\-
- Replace field:
<!--$1-->
- 查找字段:
\-\-\>\<!\-\-(.*)\-\-\>\<\!\-\-
- 替换字段:
<!--$1-->
Then, keeping the block of XAML selected, click the Uncomment button from Visual Studio.
然后,保持选中 XAML 块,单击 Visual Studio 中的取消注释按钮。
NOTE:Depending on the version of Visual Studio you are using, the syntax of the regular expression may vary. I am using VS 2012. Previous versions would use the curly braces '{}' to isolate an expression and the backslash '\' to use it back in the replace field. Now, it is the parenthesis '()' and the dollar sign '$', respectively.
注意:根据您使用的 Visual Studio 版本,正则表达式的语法可能会有所不同。我正在使用 VS 2012。以前的版本将使用花括号“{}”来隔离表达式,使用反斜杠“\”将其重新用于替换字段。现在,它分别是括号“()”和美元符号“$”。
回答by Clint StLaurent
Select the comment block Hit cntrl-K, control-c (The same shortcut as on the C# side for commenting out a block of code). The designer will shift your comment markers to comment the entire block.
选择注释块 Hit cntrl-K, control-c(与 C# 端相同的用于注释掉代码块的快捷方式)。设计器将移动您的评论标记以评论整个块。
cntrol-k, cntrol-u (Kode Uncomment) will unshift things back to making it live XAML code again. This removes all the comment markings, so you make have to re-comment your original comments again.
cntrol-k, cntrol-u (Kode Uncomment) 将把事情恢复原状,使其再次成为活动的 XAML 代码。这会删除所有评论标记,因此您必须再次重新评论您的原始评论。
its not perfect, but they are easy shortcuts you probably already know.
它并不完美,但它们是您可能已经知道的简单快捷方式。
回答by Tijo Tom
There is no commenting/uncommenting button in expression blend, if you want to try to comment your code block, you can type include the below symbols in the begin and the end of the code block manually
表达式混合中没有注释/取消注释按钮,如果您想尝试注释您的代码块,您可以手动在代码块的开头和结尾键入包含以下符号
<!-- your code comes here..
Next Line-->
See a sample screenshot below
请参阅下面的示例屏幕截图