我正在开发一个数字标牌 WPF 应用程序,基本结构设置如下:
我有一个视图,其中有一个网格,绑定到一个播放列表对象。播放列表包含窗格,窗格包含播放列表项,并且每个播放列表项都包含一个内容项。我使用 DataTemplates 为每个部分构建视图,例如
<!-- This template represents a single content pane, no real display here, just empty space with a height and width -->
<DataTemplate DataType="{x:Type entities:ContentPane}">
<ContentControl
Content="{Binding CurrentPlaylistItem, Mode=OneWay}"
Height="{Binding Height, Mode=OneTime}"
Width="{Binding Width, Mode=OneTime}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
/>
</DataTemplate>
<!-- This is a playlist item which is contained within a ContentPane.
This also has no real display, just a placeholder for the content item, and will likely contain some transition information-->
<DataTemplate DataType="{x:Type entities:PlaylistItem}">
<inf:TransitionableContentControl Content="{Binding ContentItem}"></inf:TransitionableContentControl>
</DataTemplate>
<!-- image item template
the path can be any valid uri e.g. http://... or file://... -->
<DataTemplate DataType="{x:Type contentTypes:ImageContentType}">
<Grid Background="Transparent" x:Name="ImageType">
<Image Source="{Binding Bitmap, Mode=OneTime}"></Image>
<inf:BusinessAd
ContainerHeight="{Binding ImageHeight, Mode=OneTime}"
ContainerWidth="{Binding ImageWidth, Mode=OneTime}"
Visibility="{Binding AdText, Converter={StaticResource _VisibilityConverter}, Mode=OneWay}"
Text="{Binding AdText.Text, Mode=OneTime}"
AdFontSize="{Binding AdText.TextStyle.FontSize}"
AdFontFamily="{Binding AdText.TextStyle.FontFamily}">
<ContentControl
Content="{Binding AdText, Mode=OneTime}"
ContentTemplate="{StaticResource BusinessAdTextTemplate}">
</ContentControl>
</inf:BusinessAd>
</Grid>
</DataTemplate>
随着数据上下文的变化,每个窗格中的内容会从一个项目过渡到下一个项目。我遇到了一个问题,即用户连续两次在窗格中放置相同的项目,无论是视频、图像等。发生的情况是未检测到更改并且 UI 未更新。如果是视频,它会在第一个视频的最后一帧冻结,整个应用程序挂起。
我尝试在 PlaylistItem 类中执行 OnPropertyChanged("ContentItem"),尝试在我的数据模板上设置 Shared="False",我尝试更改 ContentItem 对象的属性并引发 PropertyChanged 事件,但似乎没有任何效果。我打开了对数据绑定的跟踪以查看发生了什么,并且一切似乎都正常工作。当我更改 ContentItem 的属性时,它会显示新项目的新哈希,但 UI 上没有更改。
在 PlaylistItem 的 TransitionableContentControl 中,从一个内容项转到同一个内容项时,永远不会命中 OnContentChanged 覆盖。如果我将该控件与常规 ContentControl 交换,则没有任何变化。