1

我正在开发一个数字标牌 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 交换,则没有任何变化。

4

2 回答 2

1

这里的问题是 ContentControl(或者更确切地说是底层的 DependencyProperty 框架)不会在 .Equals 对新旧内容为真时触发 OnContentChanged。一种解决方法是在 CoerceValue 上拥有自己的依赖属性和转换。我会将代码通过电子邮件发送给您,您可以在此处发布。

于 2013-06-09T15:16:37.017 回答
1

前雇员的诊断是正确的。此外,除非您设法重新分配内部数据上下文,否则您的模板不会从头开始重新启动,正如您所注意到的。您必须首先将您分配CurrentPlaylistItem给一些默认的空项目才能一直重置内容。为避免争用和讨厌的闪烁,请在调度程序优先级高于 UI 渲染的情况下执行此操作。尝试这个:

// CurrentPlaylistItem = pli ; -- not good
Application.Current.Dispatcher.Invoke (new Action (() =>
{
    // clear item
    // null doesn't work, because TransitionableContentControl's
    // inner ContentControl would be empty and the brush created 
    // from it would be a null brush (no visual effect)
    CurrentPlaylistItem = new PlaylistItem () ;
    this.OnPropertyChanged ("CurrentPlaylistItem") ;
    Application.Current.Dispatcher.Invoke (new Action (() =>
    {
        // set new item, possibly same as old item
        // but WPF will have forgotten that because
        // data bindings caused by the assignment above
        // have already been queued at the same DataBind 
        // level and will execute before this assignment
        CurrentPlaylistItem = pli ; 
        this.OnPropertyChanged ("CurrentPlaylistItem") ;
    }), DispatcherPriority.DataBind) ;
})) ;
于 2013-06-09T18:13:31.147 回答