没有任何样式的app.xaml中的datatemplate是否不会被拾取? [英] datatemplate in app.xaml is not getting picked up without any styles?

查看:67
本文介绍了没有任何样式的app.xaml中的datatemplate是否不会被拾取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

app.xaml中有一个DataTemplate,它将视图绑定到视图模型.

I have a DataTemplate in app.xaml that binds a view to a viewmodel.

<Application.Resources>
    <DataTemplate DataType="{x:Type vm:someviewmodeltype}">
        <vw:somevwcontrol />
    </DataTemplate>
</Application.Resources>

如果没有样式,则不会应用上述模板.我放上样式的那一刻,就像...

the above template doesn't get applied if there are no styles. The moment I put a style, something like ...

<Application.Resources>
    <DataTemplate DataType="{x:Type vm:someviewmodeltype}">
        <vw:somevwcontrol />
    </DataTemplate>
    <Style TargetType="TextBlock">
        <Setter Property="FontSize" Value="20"></Setter>
    </Style>
</Application.Resources>

数据模板被拾取并产生期望的结果...我不确定在那里发生了什么...有人可以澄清吗?

datatemplate gets picked up and produces the desired results ... I am not sure whats happening there ... could anybody clarify this ?

谢谢.

推荐答案

回答了类似的问题

Answered a similar question here. The question is not exactly the same, that one contained merged dictionaries being skipped but it's most likely the same bug.

这是一个优化错误,请参见

This is an optimization bug, see this link.

关于创建每个对象 XAML(如果存在默认样式) (即带有Type的键的样式) 样式应适用.尽你所能 想象有几种表现 优化使其(暗示) 查找尽可能轻的重量.一 其中之一是我们不看里面 资源字典,除非它们是 标记为包含默认值" 样式".有一个错误:如果您所有的 默认样式嵌套在合并中 字典深三级(或 更深)顶级字典不 被标记,因此搜索将其跳过. 解决方法是设置默认值 在任何事物中都具有风格 根字典.

On the creation of every object in XAML, if a default style is present (i.e. style w/ a key of Type) that style should be applied. As you can imagine there are several performance optimizations to make that (implied) lookup a light weight as possible. One of them is that we don’t look inside Resource Dictionaries unless they are flagged as "containing default Styles". There is a bug: if all your default styles are nested in merged dictionaries three levels deep (or deeper) the top dictionary does not get flagged so the search skips it. The work around is to put a default Style to something, anything, in the root Dictionary.

我看到您也已经找到了解决方法,只需在App.xaml中添加默认的虚拟样式即可.它不需要任何setter等,类似的事情也可以

I see you've already found the workaround as well, just add a default dummy style in App.xaml. It doesn't have to have any setters etc, something like this will do as well

<Application.Resources>
    <DataTemplate DataType="{x:Type vm:someviewmodeltype}">
        <vw:somevwcontrol />
    </DataTemplate>
    <Style TargetType="{x:Type Rectangle}" /> 
</Application.Resources>

这篇关于没有任何样式的app.xaml中的datatemplate是否不会被拾取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆