【问题标题】:UWP Before Converter Event转换器事件之前的 UWP
【发布时间】:2020-01-07 11:19:08
【问题描述】:

我正在寻找在转换器之前发生的事件。

我的意思是,我有一个自定义的UserControl,它包含一个ListView。它的ListViewItem 有一些元素,例如<TextBlock Foreground="{x:Bind IsPlaying, Converter={StaticResource RowColorConverter}, Mode=OneWay} />"

这个控件被多次使用。我有一个名为 CurrentTheme 的静态 ElementTheme 变量,该变量在加载该控件时设置。

问题就在这里。 TextBlockConverter 使用那个CurrentTheme 来判断Foreground。然而,Loading 事件并不总是在Converter 之前触发,这意味着TextBlockForeground 将根据旧的CurrentTheme 进行判断。正确地说,当Converter 首次加载时,加载事件发生在转换器之前。加载后,转换器几乎总是在加载之前被调用。

我应该如何解决这个问题?

---更新---

我使用的转换器:

class RowColorConverter : Windows.UI.Xaml.Data.IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return value.Equals(true) ? Helper.GetHighlightBrush() :
                                    PlaylistControl.CurrentTheme == ElementTheme.Dark ? Helper.WhiteSmokeBrush : Helper.BlackBrush;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return null;
    }
}

我的使用代码:Interaction.Behaviors

                    <Interactivity:Interaction.Behaviors>
                        <Interactions:DataTriggerBehavior
                            Binding="{x:Bind IsPlaying}"
                            ComparisonCondition="Equal"
                            Value="True">
                            <Interactions:ChangePropertyAction
                                PropertyName="Foreground"
                                TargetObject="{Binding ElementName=ArtistTextBlock}"
                                Value="{StaticResource SystemColorHighlightColor}" />
                        </Interactions:DataTriggerBehavior>
                        <Interactions:DataTriggerBehavior
                            Binding="{x:Bind IsPlaying}"
                            ComparisonCondition="Equal"
                            Value="False">
                            <Interactions:DataTriggerBehavior
                                Binding="{Binding CurrentTheme}"
                                ComparisonCondition="Equal"
                                Value="Dark">
                                <Interactions:ChangePropertyAction
                                    PropertyName="Foreground"
                                    TargetObject="{Binding ElementName=ArtistTextBlock}"
                                    Value="White" />
                            </Interactions:DataTriggerBehavior>                                
                            <Interactions:DataTriggerBehavior
                                Binding="{Binding CurrentTheme}"
                                ComparisonCondition="NotEqual"
                                Value="Dark">
                                <Interactions:ChangePropertyAction
                                    PropertyName="Foreground"
                                    TargetObject="{Binding ElementName=ArtistTextBlock}"
                                    Value="Black" />
                            </Interactions:DataTriggerBehavior>
                        </Interactions:DataTriggerBehavior>
                    </Interactivity:Interaction.Behaviors>

【问题讨论】:

    标签: c# xaml uwp win-universal-app


    【解决方案1】:

    转换器事件之前的 UWP

    根据您的要求,我们建议您使用可以比较先前值的DataTriggerBehavior。例如。

    <Rectangle x:Name="DataTriggerRectangle">
        <Interactivity:Interaction.Behaviors>
            <Interactions:DataTriggerBehavior Binding="{Binding Value, ElementName=slider}" ComparisonCondition="GreaterThan" Value="50">
                <Interactions:ChangePropertyAction TargetObject="{Binding ElementName=DataTriggerRectangle}" PropertyName="Fill" Value="{StaticResource PaleYellowBrush}"/>
            </Interactions:DataTriggerBehavior>
            <Interactions:DataTriggerBehavior Binding="{Binding Value, ElementName=slider}" ComparisonCondition="LessThanOrEqual" Value="50">
                <Interactions:ChangePropertyAction TargetObject="{Binding ElementName=DataTriggerRectangle}" PropertyName="Fill" Value="{StaticResource RoyalBlueBrush}"/>
            </Interactions:DataTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
    </Rectangle>
    

    这是官方code sample,您可以参考。

    【讨论】:

    • 我应该使用哪个版本的Microsoft.Xaml.Behaviors.UwpNativeManaged?
    • 请使用管理版。
    • 它会导致处理异常。可以去我更新的代码区看看哪里出错了吗?
    • @SeakyLuo 我检查了你的代码,它看起来是正确的。我可以用上面的方法重现你的问题,可以为我们分享一个迷你项目吗?
    • 啊,我之前的评论有错字。异常未处理。希望这不会有关系。既然你可以复制它,为什么你还需要一个迷你项目?由于我的项目的复杂性,我的代码被纠缠在一起。我觉得下载whole project比较方便。我希望您的 PC 中有一个音乐文件列表。上面发布的代码的 xaml 位于 Controls/PlaylistControls.xaml 中。使用该控件的地方:NowPlayingPage.xaml(其他不重要)。转换器:MusicConverter.cs.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 2018-01-12
    • 1970-01-01
    相关资源
    最近更新 更多