Gus*_*dor 2 wpf datatemplate dynamicresource staticresource
我是否在从.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正文中的contentcontrol使用staticresource扩展来检索EnterPINDialogTemplate(EnterPINDialogViewModel的视图).EnterPINDialogTemplate和DialogViewTemplate都在同一个文件中(前者当然是先定义的)虽然最初它们是分开的.
在运行时,staticresource扩展会抛出带有消息的XamlParseException; '为'System.Windows.Markup.StaticResourceHolder'提供价值,引发了一个异常.
和内部异常消息;
'找不到名为'EnterPINDialogTemplate'的资源.资源名称区分大小写'
使用dynamicresource返回null并在contentcontrol中显示EnterPINDialogViewModel类型的全名 - 正如资源未解析时所预期的那样.在调用FrameWorkElement.FindResource()时突破我的自定义TemplateSelector会抛出类似的异常(TryFindResource返回null).
我的第一个想法是在构建datatemplate时逻辑树被拆分,我记得早期项目中该区域的问题.我尝试使用ResourceDictionary的MergeDictionaries属性使资源字典可以在DataTemplate中使用,但设计者不喜欢这一点,这里描述了错误:http: //connect.microsoft.com/VisualStudio/feedback/details/498844/WPF的设计师抛出,InvalidCastException的
抓住那个想法.我尝试在Application,Window和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='<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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
帮帮我stackoverflow,你是我唯一的希望!
编辑:在这个问题上做了一些进一步的工作.如果两个模板位于同一文件中,则dynamicresource和staticresource扩展都会解决资源问题.如果它们位于单独的文件中,则无论我如何合并字典,资源都无法解析; 每个扩展名返回null.
显然,解决方案是将两个资源都放入同一个字典中,但就我而言,这是一个hack而不是逻辑资源查找系统的行为.我现在不是一个快乐的兔子.这似乎没有证据......
如果有一个普拉特,那就是我.在星期五晚上试图解决这个问题后4个小时我已经破解了它,不,谢谢我只能称之为片状错误报告.
这是嗡嗡声.
<DataTemplate x:Key="TwitterClientTemplate" DataType="MV:TwitterClientViewModel">
Run Code Online (Sandbox Code Playgroud)
应该
<DataTemplate x:Key="TwitterClientTemplate" DataType="{x:Type MV:TwitterClientViewModel}">
Run Code Online (Sandbox Code Playgroud)
而且,它很有效.
然而,我的大抱怨仍然存在.为什么不正确的语法在设计器中起作用而在运行时不起作用?我的猜测是因为运行时优化只是不打算使用编写得很差的xaml来填充字典,但是得到一个错误的警告会很好.
| 归档时间: |
|
| 查看次数: |
2496 次 |
| 最近记录: |