c# - 关于 UserControl 中可配置资源的建议?
问题描述
我想听听一些关于资源和UserControls
...的建议
我意识到UserControls
有时有我希望可配置的属性 - 但在全球范围内而不是 per-instance。
让我给你举个例子:
我已经实现了一个UserControl
基本上代表一个选项卡的(我称之为RibbonTab
):
每个选项卡都有自己的图标和文本(以及一些其他属性),它们都是DependencyProperties
,所以我可以像这样实例化我的选项卡:
<!-- Time Tracking Tab -->
<UserControls:RibbonTab
Icon="{StaticResource TimeTrackingTabIcon}"
Text="{StaticResource TimeTrackingTabHeaderString}"
LabelMode="AutoResize" ResizePriority="0"
Command="{Binding LoadTimeTrackingViewModelCommand}"/>
<!-- Vacation Tab -->
<UserControls:RibbonTab
Icon="{StaticResource VacationTabIcon}"
Text="{StaticResource VacationTabHeaderString}"
LabelMode="AutoResize" ResizePriority="1"
Command="{Binding LoadVacationViewModelCommand}"/>
<!-- Payout Tab -->
<UserControls:RibbonTab
Icon="{StaticResource PayoutTabIcon}"
Text="{StaticResource PayoutTabHeaderString}"
LabelMode="AutoResize" ResizePriority="2"
Command="{Binding LoadPayoutViewModelCommand}"/>
如您所见,所选选项卡的背景颜色为白色。这是目前的固定值ControlTemplate
。我希望这是可配置的。
我想出了各种可能的解决方案,但我不确定我是否想得太多:
1.) 使用 DependencyProperty
我可以引入另一个DependencyProperty
类似"SelectedTabColor"
并实例化这样的选项卡:
<!-- Time Tracking Tab -->
<UserControls:RibbonTab
<!-- All of the previous properties go here... -->
SelectedTabColor="White"/>
<!-- Vacation Tab -->
<UserControls:RibbonTab
<!-- All of the previous properties go here... -->
SelectedTabColor="White"/>
<!-- Payout Tab -->
<UserControls:RibbonTab
<!-- All of the previous properties go here... -->
SelectedTabColor="White"/>
很容易看出,这种技术需要一遍又一遍地附加值,但您真的只想(全局)设置一次此属性并完成。
2.) 使用资源字典
我可以从应用程序中UserControl
引用:StaticResource
ResourceDictionary
<UserControl x:Class="MyApp.View.UserControls.RibbonTab">
<Grid>
<!-- Lots of stuff omitted here... -->
<Rectangle Fill="{StaticResource RibbonTab_SelectedTabColor}"/>
<!-- More stuff omitted here... -->
</Grid>
</UserControl>
但是我UserControl
要求应用程序/集成有一个StaticResource
完全像这样命名的。将其移植UserControl
到另一个项目将是一场噩梦......
3.) 使用 UserControl.Resources + StaticResources
另一种可能性是利用 的Resources
属性UserControl
并将其所需的定义StaticResources
直接放在那里:
<UserControl x:Class="MyApp.View.UserControls.RibbonTab">
<UserControl.Resources>
<!--#region Configurable properties -->
<Color x:Key="SelectedTabColor">White</Color>
<!--#endregion Configurable properties -->
</UserControl.Resources>
<Grid>
<!-- Lots of stuff omitted here... -->
<Rectangle Fill="{StaticResource SelectedTabColor}"/>
<!-- More stuff omitted here... -->
</Grid>
</UserControl>
这有一个很好的接触,因为每个StaticResource
元素都与实际使用它的元素一起存储,而不是对在ResourceDictionary
哪里使用的东西进行大而丢失的跟踪。
但是,缺点是您不能在多个 中重用一个通用定义(假设应用程序在其 中定义了某种强调色ResourceDictionary
)UserControls
,而必须在任何地方重新定义它,这使得它难以维护。
4.) 使用 UserControl.Resources + ResourceKeys
到目前为止,我能想到的最好StaticResources
方法是ResourceKeys
在. 集成商必须将 设置为应用程序的现有资源:Resources
UserControl
ResourceKeys
<UserControl x:Class="MyApp.View.UserControls.RibbonTab">
<UserControl.Resources>
<!--#region Configurable properties -->
<!-- Integration hint: Set ResourceKey to your own definition! -->
<StaticResource x:Key="SelectedTabColor"
ResourceKey="AppHighlightedElementAccentColor"/>
<!--#endregion Configurable properties -->
</UserControl.Resources>
<Grid>
<!-- Lots of stuff omitted here... -->
<Rectangle Fill="{StaticResource SelectedTabColor}"/>
<!-- More stuff omitted here... -->
</Grid>
</UserControl>
我还没有详细评估这一点。我现在可以看到的唯一缺点是仅在运行时检查引用资源的类型,而不是在设计时检查。
我错过了明显的......?有没有一种优雅的方法来解决这个问题?很高兴听到一些意见,因为现在我有点不确定如何进行......
解决方案
推荐阅读
- swift - 工作应用程序中出现意外错误 -> 未找到“Private/FIRComponentContainerInternal.h”文件
- javascript - 使用中间件处理承诺后无法识别获取的 API 数据
- android - 如何在drawerlayout中调用imageview
- dataset - Tesseract(tess-two)识别符号并重新训练几个字符的数据集?
- jmx - 如何在同一进程中使用 Spring 读取 jmx mbean
- reactjs - 如何在 AOR 中使用 Loopback 的关系,如 hasAndBelongsToMany(REST 上的管理员)
- python - Pandas Dataframe 替换系列中的值
- c# - 在多宿主 Windows 10 计算机上接收 UDP 多播消息
- docker - 未知:访问被拒绝:频道 [] creator org [Org1MSP] - docker-compose 中的 Hyperledger
- office365 - Office 加载项中的拖放功能