【发布时间】:2010-12-07 04:23:47
【问题描述】:
我正在寻找内置 WPF 命令的完整列表。
目前我找到的最好的列表是here,但它并没有列出所有命令。
一些不错的细节是:
支持命令的控件/组件(例如,TextBox 支持粘贴、复制、剪切、重做和撤消等编辑命令);
默认按键手势和 UI 文本(可从 MSDN Library 提取)。
【问题讨论】:
我正在寻找内置 WPF 命令的完整列表。
目前我找到的最好的列表是here,但它并没有列出所有命令。
一些不错的细节是:
支持命令的控件/组件(例如,TextBox 支持粘贴、复制、剪切、重做和撤消等编辑命令);
默认按键手势和 UI 文本(可从 MSDN Library 提取)。
【问题讨论】:
在所有加载的程序集中显示所有命令的完整列表很容易:
public string[] GetAllCommands()
{
return (
from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
from prop in type.GetProperties()
where
typeof(ICommand).IsAssignableFrom(prop.PropertyType) &&
prop.GetAccessors()[0].IsStatic
orderby type.Name, prop.Name
select type.Name + "." + prop.Name
).ToArray();
}
PresentationFramework 加载后,我得到了这个答案底部的列表,你会看到它是绝对完整的。
如果您还想查看命令类型(例如 RoutedUIComand)和手势,可以将其添加到 LINQ:
let commandType = prop.PropertyType
let gestures =
typeof(UIElement).IsAssignableFrom(commandType) ?
((UIElement)prop.GetValue(null)).InputGestures :
null
那么你的选择可能是这样的:
select type.Name + "." + prop.Name + " " + commandType.Name + " " + gestures
以编程方式找出哪些控件对给定命令执行某些操作也是可能的。基本上这样的事情应该可以工作(没有尝试过,但这会给你这个想法):
var allCommands = (
from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
from prop in type.GetProperties()
where
typeof(ICommand).IsAssignableFrom(prop.PropertyType) &&
prop.GetAccessors()[0].IsStatic
orderby type.Name, prop.Name
select new
{
typeName = type.Name,
propName = prop.Name,
displayAs = type.Name + "." + prop.Name,
}
).ToArray();
var classesReferencingCommand = (
from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
from method in type.GetMethods()
let methodBodyString = ConvertILToString(method.MethodBody.GetILAsByteArray())
let info = new
{
typeName = type.FullName,
referencedCommands =
from cmd in allCommands
where
methodBodyString.Contains(cmd.typeName) &&
methodBodyString.Contains(cmd.propName)
select cmd
}
where info.commands.Any()
select info
).ToArray();
ConvertILToString 可能是这样的:
static string ConvertILToString(byte[] bytes)
{
return new string(bytes.Where(b => b!=0).Select(b => (char)b).ToArray());
}
结果可以以任何你喜欢的方式使用,例如它们可以使用 ItemsControl 显示:
<ItemsControl Source="{Binding classesReferencingCommand}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding typeName}" FontWeight="Bold">
<ItemsControl Source="{Binding referencedCommands}" Margin="10 0 0 0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding displayAs}" />
... close all tags ...
或者,您可以以文本或 XML 格式输出数据或将其添加到数据库中。另请注意,如果您更喜欢按命令列出,则可以使用外部的命令迭代来翻转第二个查询。
上面的代码将为您提供准确的事实并且不会说谎,因为它正在查看 NET Framework 本身。
这是 PresentationFramework 中所有命令的承诺列表:
ApplicationCommands.CancelPrint
ApplicationCommands.Close
ApplicationCommands.ContextMenu
ApplicationCommands.Copy
ApplicationCommands.CorrectionList
ApplicationCommands.Cut
ApplicationCommands.Delete
ApplicationCommands.Find
ApplicationCommands.Help
ApplicationCommands.New
ApplicationCommands.NotACommand
ApplicationCommands.Open
ApplicationCommands.Paste
ApplicationCommands.Print
ApplicationCommands.PrintPreview
ApplicationCommands.Properties
ApplicationCommands.Redo
ApplicationCommands.Replace
ApplicationCommands.Save
ApplicationCommands.SaveAs
ApplicationCommands.SelectAll
ApplicationCommands.Stop
ApplicationCommands.Undo
ComponentCommands.ExtendSelectionDown
ComponentCommands.ExtendSelectionLeft
ComponentCommands.ExtendSelectionRight
ComponentCommands.ExtendSelectionUp
ComponentCommands.MoveDown
ComponentCommands.MoveFocusBack
ComponentCommands.MoveFocusDown
ComponentCommands.MoveFocusForward
ComponentCommands.MoveFocusPageDown
ComponentCommands.MoveFocusPageUp
ComponentCommands.MoveFocusUp
ComponentCommands.MoveLeft
ComponentCommands.MoveRight
ComponentCommands.MoveToEnd
ComponentCommands.MoveToHome
ComponentCommands.MoveToPageDown
ComponentCommands.MoveToPageUp
ComponentCommands.MoveUp
ComponentCommands.ScrollByLine
ComponentCommands.ScrollPageDown
ComponentCommands.ScrollPageLeft
ComponentCommands.ScrollPageRight
ComponentCommands.ScrollPageUp
ComponentCommands.SelectToEnd
ComponentCommands.SelectToHome
ComponentCommands.SelectToPageDown
ComponentCommands.SelectToPageUp
DocumentViewer.FitToHeightCommand
DocumentViewer.FitToMaxPagesAcrossCommand
DocumentViewer.FitToWidthCommand
DocumentViewer.ViewThumbnailsCommand
EditingCommands.AlignCenter
EditingCommands.AlignJustify
EditingCommands.AlignLeft
EditingCommands.AlignRight
EditingCommands.Backspace
EditingCommands.CorrectSpellingError
EditingCommands.DecreaseFontSize
EditingCommands.DecreaseIndentation
EditingCommands.Delete
EditingCommands.DeleteNextWord
EditingCommands.DeletePreviousWord
EditingCommands.EnterLineBreak
EditingCommands.EnterParagraphBreak
EditingCommands.IgnoreSpellingError
EditingCommands.IncreaseFontSize
EditingCommands.IncreaseIndentation
EditingCommands.MoveDownByLine
EditingCommands.MoveDownByPage
EditingCommands.MoveDownByParagraph
EditingCommands.MoveLeftByCharacter
EditingCommands.MoveLeftByWord
EditingCommands.MoveRightByCharacter
EditingCommands.MoveRightByWord
EditingCommands.MoveToDocumentEnd
EditingCommands.MoveToDocumentStart
EditingCommands.MoveToLineEnd
EditingCommands.MoveToLineStart
EditingCommands.MoveUpByLine
EditingCommands.MoveUpByPage
EditingCommands.MoveUpByParagraph
EditingCommands.SelectDownByLine
EditingCommands.SelectDownByPage
EditingCommands.SelectDownByParagraph
EditingCommands.SelectLeftByCharacter
EditingCommands.SelectLeftByWord
EditingCommands.SelectRightByCharacter
EditingCommands.SelectRightByWord
EditingCommands.SelectToDocumentEnd
EditingCommands.SelectToDocumentStart
EditingCommands.SelectToLineEnd
EditingCommands.SelectToLineStart
EditingCommands.SelectUpByLine
EditingCommands.SelectUpByPage
EditingCommands.SelectUpByParagraph
EditingCommands.TabBackward
EditingCommands.TabForward
EditingCommands.ToggleBold
EditingCommands.ToggleBullets
EditingCommands.ToggleInsert
EditingCommands.ToggleItalic
EditingCommands.ToggleNumbering
EditingCommands.ToggleSubscript
EditingCommands.ToggleSuperscript
EditingCommands.ToggleUnderline
MediaCommands.BoostBass
MediaCommands.ChannelDown
MediaCommands.ChannelUp
MediaCommands.DecreaseBass
MediaCommands.DecreaseMicrophoneVolume
MediaCommands.DecreaseTreble
MediaCommands.DecreaseVolume
MediaCommands.FastForward
MediaCommands.IncreaseBass
MediaCommands.IncreaseMicrophoneVolume
MediaCommands.IncreaseTreble
MediaCommands.IncreaseVolume
MediaCommands.MuteMicrophoneVolume
MediaCommands.MuteVolume
MediaCommands.NextTrack
MediaCommands.Pause
MediaCommands.Play
MediaCommands.PreviousTrack
MediaCommands.Record
MediaCommands.Rewind
MediaCommands.Select
MediaCommands.Stop
MediaCommands.ToggleMicrophoneOnOff
MediaCommands.TogglePlayPause
NavigationCommands.BrowseBack
NavigationCommands.BrowseForward
NavigationCommands.BrowseHome
NavigationCommands.BrowseStop
NavigationCommands.DecreaseZoom
NavigationCommands.Favorites
NavigationCommands.FirstPage
NavigationCommands.GoToPage
NavigationCommands.IncreaseZoom
NavigationCommands.LastPage
NavigationCommands.NavigateJournal
NavigationCommands.NextPage
NavigationCommands.PreviousPage
NavigationCommands.Refresh
NavigationCommands.Search
NavigationCommands.Zoom
Slider.DecreaseLarge
Slider.DecreaseSmall
Slider.IncreaseLarge
Slider.IncreaseSmall
Slider.MaximizeValue
Slider.MinimizeValue
此列表已完成。
如果主题中有任何其他手势,可以通过加载主题资源字典并对其执行一些 LINQ 轻松提取它们。查询很简单:只需搜索<InputGesture>。 更新:我认为主题中没有任何手势,因为默认手势是从资源加载的。所以这部分可能没有必要。
【讨论】:
查看ApplicationCommands、ComponentCommands 和NavigationCommands 类。这些类都包含代表各种标准路由命令的静态属性,您可以在自己的应用程序中使用它们并与框架内的内部控件进行交互。
【讨论】:
查看此链接http://en.csharp-online.net/WPF_Concepts%E2%80%94Built-In_Commands
WPF 的内置命令作为五个不同类的静态属性公开:
* ApplicationCommands—Close, Copy, Cut, Delete, Find, Help, New, Open, Paste, Print, PrintPreview, Properties, Redo, Replace, Save, SaveAs, SelectAll, Stop, Undo, and more
* ComponentCommands—MoveDown, MoveLeft, MoveRight, MoveUp, ScrollByLine, ScrollPageDown, ScrollPageLeft, ScrollPageRight, ScrollPageUp, SelectToEnd, SelectToHome, SelectToPageDown, SelectToPageUp, and more
* MediaCommands—ChannelDown, ChannelUp, DecreaseVolume, FastForward, IncreaseVolume, MuteVolume, NextTrack, Pause, Play, PreviousTrack, Record, Rewind, Select, Stop, and more
* NavigationCommands—BrowseBack, BrowseForward, BrowseHome, BrowseStop, Favorites, FirstPage, GoToPage, LastPage, NextPage, PreviousPage, Refresh, Search, Zoom, and more
* EditingCommands—AlignCenter, AlignJustify, AlignLeft, AlignRight, CorrectSpellingError, DecreaseFontSize, DecreaseIndentation, EnterLineBreak, EnterParagraphBreak, IgnoreSpellingError, IncreaseFontSize, IncreaseIndentation, MoveDownByLine, MoveDownByPage, MoveDownByParagraph, MoveLeftByCharacter, MoveLeftByWord, MoveRightByCharacter, MoveRightByWord, and more
【讨论】: