WPF 静态资源对 DataTemplate 中逻辑资源的引用未在运行时解析

发布于 2024-10-07 05:59:09 字数 7753 浏览 9 评论 0原文

我是否在从 .net 3.5 到 .net 4 的过程中错过了一些东西,因为我看到了看似有缺陷的行为,这似乎与系统的目标相悖。

我正在尝试使用一些示例来构建一个简单的 MVVM 库。我在 Twitter 客户端应用程序中使用它来进行一些额外的学习,但遇到了一个很大的障碍。

场景是这样的。我的根 ViewModel (TwitterClientViewModel) 对象被赋予一个 DialogViewModel 对象的实例用于显示。 DialogViewModel 被添加到集合中,并且 bool HasDialogs 设置为 true。如有必要,将为集合和标志调用 PropertyChanged 事件。这部分效果非常好。

TwitterClientViewModel 的视图称为 TwitterClientTemplate,并使 Visible 成为 DialogViewTemplate(DialogViewModel 的视图)托管的覆盖层。托管 ContentControl 的模板引用带有 DynamicResource 扩展的 DialogViewTemplate。这在设计器和运行时都表现得很好。

这就是事情变得奇怪的地方。 DialogViewTemplate 的“主体”托管对话框内容,并进一步绑定到 DialogViewModel.Content(类型对象)的内容控件。希望通过使用 TemplateSelector(我写了一个很好的声明性的,但为了测试目的而注释掉了),我可以显示文本和交互元素。例如,在验证 Twitter 帐户时请求用户提供详细信息。在本例中为 PIN 码。

此时,我有两个用于对话框实现的嵌套内容控件。出于测试目的,DialogViewTemplate 主体中的内容控件使用静态资源扩展来检索 EnterPINDialogTemplate(EnterPINDialogViewModel 的视图)。 EnterPINDialogTemplate 和 DialogViewTemplate 都在同一个文件中(当然前者是首先定义的),尽管它们最初是分开的。

在运行时,静态资源扩展会抛出 XamlParseException 和消息; “为‘System.Windows.Markup.StaticResourceHolder’提供值引发了异常。”

和内部异常消息;

'找不到名为'EnterPINDialogTemplate'的资源。资源名称区分大小写'

使用动态资源会返回 null 并在内容控件中显示 EnterPINDialogViewModel 类型的全名 - 当资源未解析时,如预期的那样。调用 FrameWorkElement.FindResource() 时闯入我的自定义 TemplateSelector 会引发类似的异常(TryFindResource 返回 null)。

我的第一个想法是,在构建数据模板时,逻辑树被分割,我记得早期项目中该领域的一个问题。我尝试使用 ResourceDictionary 的 MergeDictionaries 属性使资源字典在 DataTemplate 中可用,但设计者不喜欢这一点,错误描述如下: http://connect.microsoft.com/VisualStudio/feedback /details/498844/wpf-designer-throws-invalidcastexception

从这个想法开始。我尝试过在应用程序、窗口和 TwitterClientTemplate 级别合并字典,但没有成功。

下面是 xaml 文件。

DialogTemplates.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:VM="clr-namespace:EpicTweet.ViewModel" 
xmlns:ET="clr-namespace:EpicTweet"
xmlns:T="clr-namespace:EpicTweet.Tools"
xmlns:MV="clr-namespace:MVVM;assembly=MVVM"
xmlns:Loc="clr-namespace:EpicTweet.Localization"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<DataTemplate DataType="VM:EnterPINDialogViewModel" x:Key="EnterPINDialogTemplate">
    <Grid d:DesignWidth="453.89" d:DesignHeight="78.92" Loc:ResXManagerProperty.ResourceManager="{x:Static ET:Language.ResourceManager}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Label Content="{Loc:ResxExtension ResourceName=String_PIN, FallbackValue='&lt;PIN&gt;'}"/>
        <TextBox Grid.Column="1"/>
        <TextBlock Grid.Row="1" Grid.RowSpan="2"></TextBlock>
    </Grid>
</DataTemplate>
<DataTemplate x:Key="DialogViewTemplate" DataType="MV:DialogViewModel">
    <Border BorderBrush="Black" BorderThickness="1">
        <Grid d:DesignWidth="277.419" d:DesignHeight="74.96" Background="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" Height="Auto" Width="Auto">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Border d:LayoutOverrides="Width, Height" BorderThickness="0,0,0,1" BorderBrush="Black">
                <Label Content="{Binding DisplayName, FallbackValue=Header}" VerticalAlignment="Center" HorizontalAlignment="Left"/>    
            </Border>
            <ContentControl Content="{Binding Content, FallbackValue=Body}" ContentTemplate="{StaticResource EnterPINDialogTemplate}" HorizontalAlignment="Stretch" d:LayoutOverrides="Height" Grid.Row="1" Margin="5">
                <!--<ContentControl.ContentTemplateSelector>
                    <T:TypeTemplateSelector>
                        <T:TemplateTypeRelationship Type="{x:Type VM:EnterPINDialogViewModel}" ResourceKey="EnterPINDialogTemplate"/>
                    </T:TypeTemplateSelector>
                </ContentControl.ContentTemplateSelector>-->
            </ContentControl>
                <ItemsControl Grid.Row="2" Margin="10" 
                ItemsSource="{Binding Commands, Mode=OneTime, FallbackValue={x:Static VM:TwitterClientViewModel.DEFAULT_DIALOG_COMMANDS}}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button 
                        Content="{Binding DisplayName, FallbackValue=CommandName, Mode=OneWay}"
                        Command="{Binding}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </Grid>
    </Border>
</DataTemplate>

TwitterClientDataTemplate.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:VM="clr-namespace:EpicTweet.ViewModel" 
xmlns:ET="clr-namespace:EpicTweet"
xmlns:MV="clr-namespace:MVVM;assembly=MVVM"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="DialogTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<DataTemplate x:Key="TwitterClientTemplate" DataType="MV:TwitterClientViewModel">
    <ScrollViewer d:DesignWidth="285.083" d:DesignHeight="119.96">
        <Grid>
            <StackPanel d:LayoutOverrides="Width, Height">
                <StackPanel Orientation="Horizontal">
                    <Button Command="{Binding AddAccountCommand.Command}" Content="{Binding AddAccountCommand.DisplayName, FallbackValue=&lt;Add Account&gt;}"/>
                </StackPanel>
                <ContentControl/>
            </StackPanel>
            <Border BorderThickness="1" Background="#80000000" Visibility="{Binding HasDialogs, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=Collapsed, Mode=OneWay}">
                <Grid VerticalAlignment="Stretch" MinWidth="50" MaxWidth="200">
                    <ContentControl Content="{Binding Dialogs[0], Mode=OneWay}" ContentTemplate="{DynamicResource DialogViewTemplate}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
            </Border>
        </Grid>
    </ScrollViewer>
</DataTemplate>

帮助我 stackoverflow,你是我唯一的希望!

编辑:对这个问题做了一些进一步的工作。如果两个模板位于同一文件中,则动态资源和静态资源扩展都可以毫无问题地解析资源。如果它们位于单独的文件中,则无论我如何合并字典,资源都不会解析;每个扩展都返回 null。

显然,解决方案是将这两个资源放入同一个字典中,但就我而言,这是一种黑客行为,而不是逻辑资源查找系统的预期行为。我现在不是一只快乐的兔子。这似乎没有记录......

Have I missed something in the step up from .net 3.5 to .net 4 because I'm seeing seemingly buggy behaviour that seems contrary to the goal of the system.

I am trying to drum up a simple MVVM library for work using a few examples. I am consuming it within a Twitter client application for some additional learning and have hit a big stumbling block.

The scenario is this. My root ViewModel (TwitterClientViewModel) object is given an instance of a DialogViewModel object for display. The DialogViewModel is added to a collection and a bool HasDialogs is set to true. PropertyChanged events are invoked for the collection and the flag if necessary. This part works fabulously.

The view for TwitterClientViewModel is called TwitterClientTemplate and makes Visible an overlay for DialogViewTemplate (DialogViewModel's view) hosting. The hosting ContentControl's template references DialogViewTemplate with a DynamicResource extension. This displays great in the designer and at runtime.

This is where things get strange. The 'body' of DialogViewTemplate hosts dialog content with a further content control bound to DialogViewModel.Content (type object). The hope was that with use of a TemplateSelector (of which I wrote a nice declarative one, but have commented out for testing purposes) I could display both text and interactive elements. For example, requesting details from the user when authenticating a Twitter account. In this case, a PIN number.

At this point I have a two nested contentcontrols for the dialog implementation. For testing purposes, the contentcontrol in the body of DialogViewTemplate uses a staticresource extension to retrieve EnterPINDialogTemplate (view for EnterPINDialogViewModel). Both EnterPINDialogTemplate and DialogViewTemplate are in the same file (the former is defined first of course) although originally they were separate.

At runtime the staticresource extension throws a XamlParseException with the message;
'Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.'

and an inner exception message;

'Cannot find resource named 'EnterPINDialogTemplate'. Resource names are case sensitive'

Using a dynamicresource returns null and displays the Fullname of the EnterPINDialogViewModel type in the contentcontrol - as expected when the resource is not resolved. Breaking into my custom TemplateSelector as calling FrameWorkElement.FindResource() throws a similar exception (TryFindResource returns null).

My first thought was that the logical tree is split when the datatemplates are constructed and I remembered an issue in that area from an earlier project. I tried using the MergeDictionaries property of ResourceDictionary to make the resource dictionaries avaliable from within the DataTemplate but the designer did not like that one bit, and the error is described here:
http://connect.microsoft.com/VisualStudio/feedback/details/498844/wpf-designer-throws-invalidcastexception

Scratch that idea. I have tried merging the dictionaries at Application, Window and TwitterClientTemplate levels but have had no luck.

Below are the xaml files.

DialogTemplates.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:VM="clr-namespace:EpicTweet.ViewModel" 
xmlns:ET="clr-namespace:EpicTweet"
xmlns:T="clr-namespace:EpicTweet.Tools"
xmlns:MV="clr-namespace:MVVM;assembly=MVVM"
xmlns:Loc="clr-namespace:EpicTweet.Localization"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<DataTemplate DataType="VM:EnterPINDialogViewModel" x:Key="EnterPINDialogTemplate">
    <Grid d:DesignWidth="453.89" d:DesignHeight="78.92" Loc:ResXManagerProperty.ResourceManager="{x:Static ET:Language.ResourceManager}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Label Content="{Loc:ResxExtension ResourceName=String_PIN, FallbackValue='<PIN>'}"/>
        <TextBox Grid.Column="1"/>
        <TextBlock Grid.Row="1" Grid.RowSpan="2"></TextBlock>
    </Grid>
</DataTemplate>
<DataTemplate x:Key="DialogViewTemplate" DataType="MV:DialogViewModel">
    <Border BorderBrush="Black" BorderThickness="1">
        <Grid d:DesignWidth="277.419" d:DesignHeight="74.96" Background="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" Height="Auto" Width="Auto">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Border d:LayoutOverrides="Width, Height" BorderThickness="0,0,0,1" BorderBrush="Black">
                <Label Content="{Binding DisplayName, FallbackValue=Header}" VerticalAlignment="Center" HorizontalAlignment="Left"/>    
            </Border>
            <ContentControl Content="{Binding Content, FallbackValue=Body}" ContentTemplate="{StaticResource EnterPINDialogTemplate}" HorizontalAlignment="Stretch" d:LayoutOverrides="Height" Grid.Row="1" Margin="5">
                <!--<ContentControl.ContentTemplateSelector>
                    <T:TypeTemplateSelector>
                        <T:TemplateTypeRelationship Type="{x:Type VM:EnterPINDialogViewModel}" ResourceKey="EnterPINDialogTemplate"/>
                    </T:TypeTemplateSelector>
                </ContentControl.ContentTemplateSelector>-->
            </ContentControl>
                <ItemsControl Grid.Row="2" Margin="10" 
                ItemsSource="{Binding Commands, Mode=OneTime, FallbackValue={x:Static VM:TwitterClientViewModel.DEFAULT_DIALOG_COMMANDS}}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button 
                        Content="{Binding DisplayName, FallbackValue=CommandName, Mode=OneWay}"
                        Command="{Binding}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </Grid>
    </Border>
</DataTemplate>

TwitterClientDataTemplate.xaml

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:VM="clr-namespace:EpicTweet.ViewModel" 
xmlns:ET="clr-namespace:EpicTweet"
xmlns:MV="clr-namespace:MVVM;assembly=MVVM"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="DialogTemplates.xaml"/>
</ResourceDictionary.MergedDictionaries>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<DataTemplate x:Key="TwitterClientTemplate" DataType="MV:TwitterClientViewModel">
    <ScrollViewer d:DesignWidth="285.083" d:DesignHeight="119.96">
        <Grid>
            <StackPanel d:LayoutOverrides="Width, Height">
                <StackPanel Orientation="Horizontal">
                    <Button Command="{Binding AddAccountCommand.Command}" Content="{Binding AddAccountCommand.DisplayName, FallbackValue=<Add Account>}"/>
                </StackPanel>
                <ContentControl/>
            </StackPanel>
            <Border BorderThickness="1" Background="#80000000" Visibility="{Binding HasDialogs, Converter={StaticResource BooleanToVisibilityConverter}, FallbackValue=Collapsed, Mode=OneWay}">
                <Grid VerticalAlignment="Stretch" MinWidth="50" MaxWidth="200">
                    <ContentControl Content="{Binding Dialogs[0], Mode=OneWay}" ContentTemplate="{DynamicResource DialogViewTemplate}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Grid>
            </Border>
        </Grid>
    </ScrollViewer>
</DataTemplate>

Help me stackoverflow, you're my only hope!

EDIT: Did some further work on this issue. If both templates are in the same file dynamicresource and staticresource extensions both resolve the resource without issue. If they are in separate files, the resource will not resolve regardless of how I merge the dictionaries; each extension returns null.

Obviously the solution is to throw both resources into the same dictionary but as far as I'm concerned this is a hack and is not intended behaviour of the logical resource lookup system. I am not a happy bunny right now. This seems pretty undocumented...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞美少年 2024-10-14 05:59:09

如果有一个普拉特,那就是我。在周五晚上花了 4 个小时试图解决这个问题后,我已经破解了它,这要归功于我只能称之为“片状错误报告”的东西。

这是嗡嗡声。

<DataTemplate x:Key="TwitterClientTemplate" DataType="MV:TwitterClientViewModel">

应该是

<DataTemplate x:Key="TwitterClientTemplate" DataType="{x:Type MV:TwitterClientViewModel}">

,然后,它起作用了。

然而,我最大的抱怨仍然存在。为什么不正确的语法在设计器中有效但在运行时无效?我的猜测是,因为运行时优化只是懒得用编写得不好的 xaml 来填充字典,但如果能得到错误的警告,那就太好了。

If ever there was a pratt, it is me. After 4 hours on a Friday night trying to solve this one I have cracked it, no thanks to what I can only call flaky error reporting.

Here is the buzz.

<DataTemplate x:Key="TwitterClientTemplate" DataType="MV:TwitterClientViewModel">

should be

<DataTemplate x:Key="TwitterClientTemplate" DataType="{x:Type MV:TwitterClientViewModel}">

And bam, it works.

My big gripe remains however. Why does the incorrect syntax work in the designer but not at runtime? My guess is because runtime optimization just doesn't bother to populate a dictionary with poorly authored xaml but it would be nice to get a warning that it is wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文