WinUI SelectorBar 控件:功能规格、源码实现与交互模型详解 WinUI SelectorBar 控件功能规格、源码实现与交互模型详解【免费下载链接】microsoft-ui-xamlWinUI: a modern UI framework with a rich set of controls and styles to build dynamic and high-performing Windows applications.项目地址: https://gitcode.com/GitHub_Trending/mi/microsoft-ui-xaml本文基于 microsoft-ui-xaml 仓库中的设计规格文档 selectorBar-functional-spec.md系统讲解 WinUI 中 SelectorBar 控件的设计动机、公共 API、视觉状态与主题资源、键盘/手柄/指针输入模型、可访问性设计并结合 controls/dev/SelectorBar 下的真实 C 源码剖析其内部 ItemsView 双向选择同步的核心架构。读完本文你将能够正确地在 XAML 与 C# 中集成 SelectorBar 实现轻量级视图切换并理解其选中同步、焦点自动选择与 UI Automation 底层机制。控件定位与设计动机SelectorBar 属于Basic Input类别的控件用于通过让用户在若干组数据集中选择并切换来改变所显示的内容。它本质上是 Top NavigationView 的一个轻量版本默认配置下会选中一个选项作为初始状态。规格文档列出了引入该控件的四项动机支持资源管理器File Explorer类场景切换文件分类视图Recent、Shared、Favorites 等为 Top NavigationView 的导航模式提供一个更轻量、性能更好的替代方案Top NavView 是 Pivot 控件被弃用后的推荐方案回应社区前三大第三方诉求之一——替代已被弃用的 Pivot 控件达到与 Fluent 设计工具包中现有 UI 元素的对齐同时对标 Windows Community Toolkit 中Segmented的PivotSegmentedStyle的功能。一个关键的架构决策记录在规格文档的 Spec note 中SelectorBar 内部会复用并实现一个 ItemsView但它并不从 ItemsView 派生原因是 SelectorBar 不支持多选且不希望向调用方暴露多选相关 API。这一点在源码中得到直接印证// controls/dev/SelectorBar/SelectorBar.h class SelectorBar : public ReferenceTrackerSelectorBar, winrt::implementation::SelectorBarT, public SelectorBarProperties { // ... tracker_refwinrt::ItemsView m_itemsView{ this }; // ... };SelectorBar类自身继承自Control对应 IDL 中unsealed runtimeclass SelectorBar : Microsoft.UI.Xaml.Controls.Control内部通过tracker_refwinrt::ItemsView持有一个模板部件part的跟踪引用——这正是组合优于继承的体现。这是正确的控件吗选择 SelectorBar 的判据是当你想通过在小数量选项之间切换来改变显示的内容且同一时刻只能选中一个选项时适合使用 SelectorBar。典型例子包括在Recent、Shared、Favorites页面之间切换每页展示各自独立的内容列表在All、Unread、Flagged、Urgent页面之间切换每页展示不同类别的邮件列表。而以下场景应选用其他控件场景推荐控件视图之间的轻量级导航SelectorBar应用需要一致的顶层导航NavigationView数据的规则分页PipsPager选项默认不被选中、且上下文与页面导航无关RadioButtons示例场景XAML 声明 Items 与初始选中规格文档给出的标准用法是在 XAML 中声明SelectorBarItem集合并通过SelectionChanged事件驱动内容切换SelectorBar x:NameselectorBar SelectionChangedSelectorBar_SelectionChanged SelectorBarItem x:NameliteNavItemRecent TextRecent IconRecent/ SelectorBarItem x:NameliteNavItemShared TextShared IconShare/ SelectorBarItem x:NameliteNavItemFavorites TextFavorites IconFavorite IsSelectedTrue/ /SelectorBar Frame x:NameContentFrame/C#private void SelectorBar_SelectionChanged(SelectorBar sender, SelectorBarSelectionChangedEventArgs args) { SelectorBarItem item sender.SelectedItem; System.Type pageType; if (item liteNavItemRemote) pageType typeof(PageRemote); else if (item liteNavItemShared) pageType typeof(PageShared); else pageType typeof(PageFavorites); ContentFrame.Navigate(pageType); }说明原文档示例中 C# 代码引用的liteNavItemRemote与上方 XAML 中声明的liteNavItemRecent存在命名不一致这是规格文档自身的笔迹实际使用时请以 XAML 中x:Name声明的元素为准。公共 API 全览SelectorBar 类表示由一小组选项构成、其中恰有一个被选中的控件。命名空间Microsoft.UI.Xaml.Controls声明public class SelectorBar : ControlSelectorBar.Items 属性获取当前由SelectorBar表示的SelectorBarItem实例列表。public IVectorSelectorBarItem Items { get; }属性值作为SelectorBar的默认内容属性content property既可以在 XAML 标记中填充也可以在代码中填充。备注该集合的元素必须由SelectorBarItem或其子类组成。规格文档特别指出SelectorBar.Items是通过 SelectorBar.xaml 中的模板绑定注入到内部 ItemsView 的ItemsSource属性的。这一点在仓库源码中可以逐行对应!-- controls/dev/SelectorBar/SelectorBar.xaml -- controls:ItemsView x:NamePART_ItemsView ItemsSource{TemplateBinding Items} TabNavigation{TemplateBinding TabNavigation} MaxWidth{TemplateBinding MaxWidth} MaxHeight{TemplateBinding MaxHeight} Padding{TemplateBinding Padding} controls:ItemsView.Layout controls:StackLayout OrientationHorizontal/ /controls:ItemsView.Layout /controls:ItemsView内部 ItemsView 使用横向StackLayout排布选项并被模板命名为PART_ItemsView。SelectorBar.SelectedItem 属性获取或设置当前选中的项。public SelectorBarItem SelectedItem { get; set; }属性值表示Items列表中处于选中状态的SelectorBarItem默认值为null。备注规格定义的行为契约当SelectorBar获得焦点且SelectedItem仍为null时若Items中存在可聚焦元素该属性会被自动设置为列表中第一个可聚焦项并且SelectionChanged事件按常规触发若SelectorBar持有焦点期间将SelectedItem置为null则控件无选中项但保持焦点当被选中的SelectorBarItem从Items列表中移除时SelectedItem自动置为null将SelectedItem设置为不在集合中的元素、或在元素加入集合之前设置会抛出异常将选中项的IsEnabled设为False不会影响SelectedItem将其Visibility设为Collapsed同理。SelectorBar.SelectionChanged 事件每当SelectedItem发生变化时触发。public event EventHandlerSelectorBar, SelectorBarSelectionChangedEventArgs SelectionChanged;该事件在通过以下任一途径选中项时触发UI AutomationTab 键焦点移动且新选中了一个项在SelectorBar内的左右方向键导航鼠标或触控的Tapped事件通过SelectorBar.SelectedItem属性进行的程序化选择。规格文档说明输入由SelectorBarItem所派生的ItemContainer统一处理焦点与左右导航的细节见输入处理章节。SelectorBarItem 类表示SelectorBar中呈现的单个选项项。命名空间Microsoft.UI.Xaml.Controls声明public class SelectorBarItem : ItemContainerSelectorBar x:NameselectorBar SelectionChangedSelectorBar_SelectionChanged SelectorBarItem x:NameliteNavItemRemote TextRemote IconRemote/ SelectorBarItem x:NameliteNavItemShared TextShared IconShared/ SelectorBarItem x:NameliteNavItemFavorites TextFavorites IconFavorite IsSelectedTrue/ /SelectorBarSelectorBarItem.Text 属性获取或设置项的文本标签默认值为null。public string Text { get; set; }SelectorBarItem.Icon 属性获取或设置项的图标默认值为null。public IconElement Icon { get; set; }MIDL3 完整 API 声明规格文档中 API Review 部分给出的完整接口定义Microsoft.UI.Xaml.Controls命名空间如下与仓库中实际的 SelectorBar.idl 完全一致namespace Microsoft.UI.Xaml.Controls { [MUX_PUBLIC_V6] [webhosthidden] [MUX_PROPERTY_CHANGED_CALLBACK(TRUE)] [MUX_PROPERTY_CHANGED_CALLBACK_METHODNAME(OnPropertyChanged)] unsealed runtimeclass SelectorBarItem : Microsoft.UI.Xaml.Controls.ItemContainer { SelectorBarItem(); String Text { get; set; }; IconSource Icon { get; set; }; static Microsoft.UI.Xaml.DependencyProperty TextProperty{ get; }; static Microsoft.UI.Xaml.DependencyProperty IconProperty{ get; }; } [MUX_PUBLIC_V6] [webhosthidden] [contentproperty(Items)] [MUX_PROPERTY_CHANGED_CALLBACK(TRUE)] [MUX_PROPERTY_CHANGED_CALLBACK_METHODNAME(OnPropertyChanged)] unsealed runtimeclass SelectorBar : Microsoft.UI.Xaml.Controls.Control { SelectorBar(); Windows.Foundation.Collections.IVectorSelectorBarItem Items { get; }; SelectorBarItem SelectedItem { get; set; }; event Windows.Foundation.TypedEventHandlerSelectorBar, SelectorBarSelectionChangedEventArgs SelectionChanged; static Microsoft.UI.Xaml.DependencyProperty ItemsProperty{ get; }; static Microsoft.UI.Xaml.DependencyProperty SelectedItemProperty{ get; }; } runtimeclass SelectorBarSelectionChangedEventArgs { } } namespace Microsoft.UI.Xaml.Automation.Peers { [MUX_PUBLIC_V6] [webhosthidden] unsealed runtimeclass SelectorBarItemAutomationPeer : Microsoft.UI.Xaml.Automation.Peers.ItemContainerAutomationPeer { [method_name(CreateInstanceWithOwner)] SelectorBarItemAutomationPeer(Microsoft.UI.Xaml.Controls.SelectorBarItem owner); } }两个值得注意的 IDL 细节[contentproperty(Items)]使Items成为默认内容属性因此 XAML 中的子元素直接作为SelectorBarItem写入集合[MUX_PROPERTY_CHANGED_CALLBACK(TRUE)]指定了统一走OnPropertyChanged的属性变更回调这正是源码中实现选择同步的挂载点下文详述。SelectorBarItemAutomationPeer 类将SelectorBarItem类型暴露给 UI Automation。命名空间Microsoft.UI.Xaml.Automation.Peers声明public class SelectorBarItemAutomationPeer : ItemContainerAutomationPeerItemContainerAutomationPeer实现了ISelectionItemProvider与IInvokeProvider两种模式。规格文档指出SelectorBarItemAutomationPeer::GetNameCore按以下优先级返回名称SelectorBarItem的AutomationProperties.NameSelectorBarItem的Text属性值SelectorBarItem的Child属性值的文本表示SelectorBarItem的本地化默认名。源码剖析内部 ItemsView 与双向选择同步SelectorBar.cpp 实现了规格文档中所有关键行为契约核心是SelectedItem与内部ItemsView.SelectedItem之间的双向同步。模板应用与部件获取// controls/dev/SelectorBar/SelectorBar.cppOnApplyTemplate约 L44-L73 void SelectorBar::OnApplyTemplate() { m_itemsView.set(GetTemplateChildTwinrt::ItemsView(s_itemsViewPartName, controlProtected)); if (const auto itemsView m_itemsView.get()) { m_itemsViewSelectedItemPropertyChangedRevoker RegisterPropertyChanged( itemsView, winrt::ItemsView::SelectedItemProperty(), { this, SelectorBar::OnItemsViewSelectedItemPropertyChanged }); if (auto scrollView itemsView.ScrollView()) { // 默认 ScrollView 内容方向是纵向滚动对 SelectorBar 无用 // 因此允许在水平方向空间不足时横向滚动选项。 scrollView.ContentOrientation(winrt::ScrollingContentOrientation::Horizontal); } } if (SelectedItem() ! nullptr) { ValidateSelectedItem(); UpdateItemsViewSelectionFromSelectedItem(); } }从源码结构看OnApplyTemplate中完成三件事——拿到PART_ItemsView、对内部ItemsView::SelectedItemProperty注册属性变更监听、将内部ScrollView的内容方向改为Horizontal保证选项过多且控件被压缩时仍可横向滚动。正向同步SelectedItem → ItemsView当外部XAML 初始化或代码设置SelectedItem时经由 IDL 声明的属性变更回调进入// SelectorBar.cppOnPropertyChanged约 L75-L87 void SelectorBar::OnPropertyChanged(winrt::DependencyPropertyChangedEventArgs const args) { if (args.Property() s_SelectedItemProperty) { ValidateSelectedItem(); UpdateItemsViewSelectionFromSelectedItem(); RaiseSelectionChanged(); } }UpdateItemsViewSelectionFromSelectedItem约 L174-L194在Items中索引出选中项的位置后调用itemsView.Select(selectedIndex)若SelectedItem为null则调用itemsView.DeselectAll()。而ValidateSelectedItem约 L212-L227实现了规格中设置集合外元素即抛错的契约if (!result) { throw winrt::hresult_invalid_argument(LSelectedItem must be an element of Items.); }反向同步ItemsView → SelectedItem键盘导航、点击等由内部 ItemsView 产生的选中变化经OnItemsViewSelectedItemPropertyChanged转发到UpdateSelectedItemFromItemsView约 L196-L210仅当两者不一致时才回写SelectedItem避免无意义的属性变更与事件循环。焦点时的自动选择规格文档获得焦点且无选中项时自动选中第一个可聚焦项的行为由 SelectorBar.h 声明的OnGotFocus与SelectFirstFocusableItem实现// SelectorBar.cppOnGotFocus约 L98-L127 const auto selectedItem SelectedItem(); const auto selectedItemIsFocusable selectedItem nullptr ? false : SharedHelpers::IsFocusableElement(selectedItem); if (selectedItem nullptr || !selectedItemIsFocusable) { if (const auto itemsView m_itemsView.get()) { const int currentItemIndex itemsView.CurrentItemIndex(); if (currentItemIndex ! -1) { itemsView.Select(currentItemIndex); // 优先选中当前项索引 } } if (SelectedItem() nullptr) { SelectFirstFocusableItem(); // 兜底选择第一个可聚焦项 } }SelectFirstFocusableItem约 L153-L172遍历Items用SharedHelpers::IsFocusableElement找到第一个可聚焦项并赋值SelectedItem。此外OnLoaded约 L129-L139在控件加载且无选中项时也会从内部 ItemsView 反向同步一次保证 XAML 中IsSelectedTrue声明的项能正确反映到SelectedItem上。这套双通道同步 焦点兜底的实现使得SelectionChanged事件的五种触发途径UIA、Tab、方向键、Tapped、程序化在实现层面统一收敛为对SelectedItem属性的变更。测试印证仓库中为 SelectorBar 提供了 API 与交互两类测试可验证上述行为契约controls/dev/SelectorBar/APITests/SelectorBarTests.csVerifyDefaultSelectorBarItemPropertyValues、VerifyDefaultSelectorBarPropertyValues、VerifySelectorBarItems验证默认属性值与 Items 集合行为controls/dev/SelectorBar/InteractionTests/SelectorBarTests.csSelectorBarItemSelection验证项选择的交互行为。视觉状态与主题资源视觉组与视觉状态SelectorBar控件本身只有一个视觉组视觉组CommonStates默认状态Normal控件的默认状态。SelectorBarItem有两个正交的视觉组视觉组Combined States合并状态组视觉状态进入时机UnselectedNormal默认未选中且无指针交互UnselectedPointerOver鼠标悬停在项上UnselectedPressed鼠标在项上按下SelectedNormalIsSelectedtrueSelectedPointerOverIsSelectedtrue且鼠标悬停SelectedPressedIsSelectedtrue且鼠标按下视觉组Disabled States视觉状态进入时机Enabled默认控件启用Disabled控件IsEnabledfalse这些状态在 SelectorBar.xaml 的模板中逐一定义。值得关注的实现细节是选中态的药丸pill动画!-- SelectorBar.xamlSelectedNormal 状态 -- DoubleAnimationUsingKeyFrames Storyboard.TargetNamePillTransform Storyboard.TargetPropertyScaleX SplineDoubleKeyFrame KeyTime{StaticResource ComboBoxItemScaleAnimationDuration} Value4 KeySpline0,0,0,1/ /DoubleAnimationUsingKeyFrames DoubleAnimationUsingKeyFrames Storyboard.TargetNamePART_SelectionVisual Storyboard.TargetPropertyOpacity SplineDoubleKeyFrame KeyTime{StaticResource ComboBoxItemScaleAnimationDuration} Value1 KeySpline0,0,0,1/ /DoubleAnimationUsingKeyFrames选中项底部会呈现一条由PillTransform横向缩放ScaleX动画至 4 倍并淡入的PART_SelectionVisual矩形这正是规格文档Composition animation sources一节所述复用 Top NavigationView 同一 storyboard 思路的左右滑动药丸动画的落地。主题资源表规格文档定义了完整的主题资源默认值Default/Light/Dark与高对比度High-Contrast取值如下SelectorBar 控件级资源资源高对比度亮/暗模式说明SelectorBarBackgroundSystemControlTransparentBrushControlFillColorTransparentBrush所有视觉状态下父容器的背景SelectorBarBorderBrushSystemControlTransparentBrushControlFillColorTransparentBrush所有视觉状态下父容器的边框画刷SelectorBarItem 项级资源资源高对比度亮/暗模式说明SelectorBarItemBorderBrushSystemControlTransparentBrushControlFillColorTransparentBrush项的边框画刷SelectorBarItemBorderBrushPointerOverSystemControlTransparentBrushControlFillColorTransparentBrush悬停时的边框画刷SelectorBarItemBorderBrushPressedSystemControlTransparentBrushControlFillColorTransparentBrush按下时的边框画刷SelectorBarItemBorderBrushSelectedSystemControlTransparentBrushControlFillColorTransparentBrush选中时的边框画刷SelectorBarItemBorderBrushDisabledSystemControlTransparentBrushControlFillColorTransparentBrush禁用时的边框画刷SelectorBarItemForegroundSystemColorButtonTextColorTextFillColorPrimaryBrush项的前景色SelectorBarItemForegroundPointerOverSystemColorGrayTextColorTextFillColorSecondaryBrush悬停时的前景色SelectorBarItemForegroundPressedSystemColorGrayTextColorSubtleFillColorTertiaryBrush按下时的前景色SelectorBarItemForegroundSelectedSystemColorHighlightTextColorTextFillColorPrimaryBrush选中时的前景色SelectorBarItemForegroundDisabledSystemColorGrayTextColorTextFillColorDisabledBrush禁用时的前景色SelectorBarItemBackgroundSystemControlTransparentBrushControlFillColorTransparentBrush项的背景SelectorBarItemBackgroundPointerOverSystemControlTransparentBrushControlFillColorTransparentBrush悬停时的背景SelectorBarItemBackgroundPressedSystemControlTransparentBrushControlFillColorTransparentBrush按下时的背景SelectorBarItemBackgroundSelectedSystemControlTransparentBrushControlFillColorTransparentBrush选中时的背景SelectorBarItemBackgroundDisabledSystemControlTransparentBrushControlFillColorTransparentBrush禁用时的背景SelectorBarItemPillFillSystemColorHighlightColorAccentFillColorDefaultBrush项的药丸视觉填充SelectorBarItemDisabledPillFillAccentFillColorDisabledBrushAccentFillColorDisabledBrush禁用时项的药丸填充上述键值在 SelectorBar_themeresources.xaml 中按Default、HighContrast、Light三个主题字典实际定义并额外提供了若干尺寸类资源SelectorBarItemPillHeight3、SelectorBarItemPillWidth4、SelectorBarItemIconScale0.8、SelectorBarItemSpacing8以及SelectorBarItemPill样式基于ComboBoxItemPill派生。模板设置与延迟加载规格文档明确SelectorBar没有模板设置Template settings也没有延迟加载Deferred loading方面的考虑。可访问性设计Automation Peer 类型与模式SelectorBar依赖内部 ItemsView 的ItemsViewAutomationPeer不覆盖GetAutomationControlTypeCoreSelectorBarItem使用SelectorBarItemAutomationPeer派生自ItemContainerAutomationPeer后者实现ISelectionItemProvider与IInvokeProvider模式ItemContainerAutomationPeer的控件类型返回AutomationControlType::ListItemClassName 约定SelectorBar返回SelectorBarSelectorBarItem的 peer 返回SelectorBarItem源码中 SelectorBarItemAutomationPeer.cpp 的GetClassNameCore即winrt::hstring_name_ofwinrt::SelectorBarItem()。Name 解析逻辑的源码印证SelectorBarItemAutomationPeer.cpp 的GetNameCore与规格文档的四级优先级一一对应// 第一优先AutomationProperties.Name winrt::hstring returnHString winrt::AutomationProperties::GetName(Owner()); if (returnHString.empty()) { // 第二优先SelectorBarItem.Text if (const auto selectorBarItem Owner().try_aswinrt::SelectorBarItem()) { returnHString selectorBarItem.Text(); } } if (returnHString.empty()) { // 第三优先ItemContainer.Child 的文本表示 if (const auto itemContainer Owner().try_aswinrt::ItemContainer()) { returnHString SharedHelpers::TryGetStringRepresentationFromObject(itemContainer.Child()); } } if (returnHString.empty()) { // 第四优先本地化控件名 returnHString ResourceAccessor::GetLocalizedStringResource(SR_SelectorBarItemDefaultControlName); }AutomationProperties 与 Tooltip标记中设置的AutomationProperties无代码中设置的AutomationProperties无Tooltip 约定若SelectorBarItem的文本被截断应添加 tooltip。规格文档同时保留了一个开放讨论点——由于SelectorBarItem自带Text属性截断时应否由控件自身以 Tooltip 处理参考AppBarButton类似的 Icon Text 模式。输入处理模型键盘导航SelectorBar 依赖ItemsView的键盘处理具体交互如下按键行为左方向键焦点移动到左侧相邻项若已在最左则停留在最左项。选择跟随焦点右方向键焦点移动到右侧相邻项依赖ItemsView键盘处理若已在最右则停留在最右项。选择跟随焦点Home焦点移动到最左项。选择跟随焦点End焦点移动到最右项。选择跟随焦点其余按键无特殊处理无功能键与键盘加速键处理。Tab 键行为进出控件Tab 进入SelectorBar时当前选中项获得焦点若无任何选中项则第一个可聚焦元素被选中并聚焦。项组不接收宽泛键盘焦点例如 SelectorBar 自身不会出现包裹整个控件的焦点框。Tab 循环配置规格明确规定了以下取值SelectorBarUIElement.TabFocusNavigationOnceControl.TabNavigationOnceControl.IsTabStopFalseControl.TabIndex默认索引SelectorBarItemUIElement.TabFocusNavigationLocalControl.TabNavigationLocalControl.IsTabStopTrueControl.TabIndex默认索引这与 SelectorBar.xaml 中默认样式对IsTabStopFalse、TabNavigationOnce的 Setter 设置以及模板内ItemsView上TabNavigation{TemplateBinding TabNavigation}的透传一致。手柄Gamepad输入行为GamepadDPadLeft/GamepadRightThumbstickLeft/GamepadLeftTrigger焦点移到左侧相邻项已在最左则停留。选择跟随焦点GamepadDPadRight/GamepadRightThumbstickRight/GamepadRightTrigger焦点移到右侧相邻项已在最右则停留。选择跟随焦点其余手柄输入无行为。鼠标、笔与触控规格说明以下交互全部由SelectorBarItem所派生的ItemContainer内部统一处理鼠标按键左键支持 Hover、Key uppointer up、Presspointer down三种反馈鼠标滚轮无行为笔笔触应选中对应项触控UIElement.Tapped应选中对应项UIElement.Swipe、UIElement.DoubleTapped、UIElement.RightTapped无行为拖放无。屏幕阅读器播报当项获得焦点Tab 或方向键导航时播报格式为Selected{x 项索引}/{n 项总数}选择、焦点提示与数据绑定选择单选Single Selection项焦点使用默认的系统焦点视觉UseSystemFocusVisualsAnchor 不适用。规格备注选择跟随单项焦点不存在组级焦点焦点提示Focus cuesUseSystemFocusVisuals为 true即默认不使用自定义焦点提示ElementSoundPlayer无数据绑定SelectorBar.Items支持IVector集合类型集合元素必须为SelectorBarItem及其子类该集合经由模板绑定注入内部ItemsView.ItemsSource即前述组合优于继承架构的数据通路。与相似控件的对比附录规格文档附录总结了与 SelectorBar 相似的控件及取舍Top 模式的 NavigationView共享类似的单选可视化但控件复杂度不同。SelectorBar 的目标是提供不处理内容content的轻量替代PipsPager同样是默认选中一项的单选行为差异在可视化与交互选项分页 vs 视图切换RadioButtons单选行为相似但可视化不同且不涉及页面导航语义Segmented 控件团队曾探索将 SelectorBar 与 Segmented 的功能合并最终决定二者独立。区别在于Segmented 用于过滤并操作一个已定义的列表而 SelectorBar 用于切换并显示不同内容Segmented 将提供触控优先touch-first体验并可能支持多选。规格备注指出触控优先体验与暂定多选意味着不同的实现细节这是两个控件保持分离的主要原因。Segmented 的详细规格可参考同目录下的 segmented-functional-spec.md。小结SelectorBar 是 WinUI 面向少量选项、单选、视图切换场景的轻量控件公共 API 面刻意收窄为Items/SelectedItem/SelectionChanged三要素多选能力被有意排除在外实现上以派生自 Control 组合内部 ItemsView 双向选择同步的架构复用 ItemsView 的键盘导航与 ItemContainer 的指针输入同时在焦点获取时提供自动选择兜底。其完整的视觉状态、主题资源、可访问性 Name 解析与手柄/触控交互模型均已在 controls/dev/SelectorBar 的模板、主题资源与源码中落地并与 InteractionTests、APITests 中的自动化测试相互印证。对于需要在 Top NavigationView 之外提供轻量级内容切换如文件分类、邮件视图筛选的 WinUI 应用SelectorBar 是该规格文档推荐的直接方案。【免费下载链接】microsoft-ui-xamlWinUI: a modern UI framework with a rich set of controls and styles to build dynamic and high-performing Windows applications.项目地址: https://gitcode.com/GitHub_Trending/mi/microsoft-ui-xaml创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考