c# - 如何使用静态资源 (.resx) 作为资源字典中具有 DataType 的 DataTemplate 的键?
问题描述
我在包含数据模板的 prism 程序集中定义了一个资源字典。我的目标是DataType=""
在它们上定义属性以确保类型安全,同时还将我的x:Key=""
字符串存储在单独的.resx文件中以供重复使用。我发现this stackOverflow answer讨论了使用{x:Static }
用于访问文本的用途,需要一种更详细的方法才能不以这种方式访问它们。
不幸的是,似乎将上述方法一起使用会导致x:Key
默认为nameof(DataType)
,这在使用不同键定义多种类型时会导致问题。
下面是一个场景示例:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Namespace.Class"
xmlns:resources="clr-namespace:Namespace">
// Key is the value of the .resx string "MyResxString"
<DataTemplate x:Key="{x:Static resources:Text.MyResxString}"/>
// Key is "myClass"
<DataTemplate DataType="{x:Type myClass}"/>
// Key is "example"
<DataTemplate x:Key="Example" DataType="{x:Type myClass}" />
// Key is unexpectedly "myClass" and my static string is ignored in the key attribute
<DataTemplate x:Key="{x:Static resources:Text.MyResxString}"
DataType="{x:Type myClass}"/>
</ResourceDictionary>
这是此绑定的预期结果还是这里有问题?有没有另一种方法来做同样的事情?(我知道 .resx 并不是最好的方法,但它是我们的标准)
解决方案
推荐阅读
- django - Django & intercooler.js : 使用 Intercooler.js 将变量传递给视图
- amazon-web-services - 无法制作 S3 对象,使用 Lambda 从另一个账户上的 S3 存储桶复制,公共
- javascript - 我正在尝试将 JSON 对象发布到 ASP.NET MVC 控制器但得到空 JSON
- python - SMTPAuthenticationError: (535, b'5.7.8 用户名和密码不被接受。了解详情\n5.7.8 https://support.google
- magento - Magento 1.9:获取产品 X 是追加销售的产品
- exception - 为什么要在异常中使用多个捕获?
- windows - windows Jenkins上的groovy命令curl
- hyperledger-fabric - 在 Hyperledger Fabric 中,客户端是向所有订单发送交易还是仅向一个订购者节点发送交易?
- c# - 无法将 int 类型隐式转换为 (namespace.model.class) 类型
- python - 如何用另一个数据框中最接近的日期填充一个数据框中的日期列