UWP 中 XAML 行为(托管)的不可预测的样式更改
Unpredictable Style Change by XAML Behaviors (Managed) in UWP
我正在使用 Microsoft.Xaml.Behaviors.Uwp.Managed NuGet 包来控制我的 UWP C# 应用中的行为。我有 ListView 和这样的自定义数据模板:
项目:
<ListView
ItemTemplate="{StaticResource ListViewMessageTemplate_Assistant}" x:Name="MessengerView"
BorderThickness="2,2,2,2" SelectionMode="None" Margin="10,10,10,10" IsMultiSelectCheckBoxEnabled="False" ScrollViewer.HorizontalScrollBarVisibility="Auto"
IsDoubleTapEnabled="False" IsHoldingEnabled="False"
IsRightTapEnabled="False" IsTapEnabled="False" ContainerContentChanging="MessengerView_ContainerContentChanging" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
数据模板:
<DataTemplate x:Key="ListViewMessageTemplate_Assistant">
<Grid x:Name="MainGrid" MaxWidth="500" Margin="10,0,10,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle x:Name="BackRect" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Grid.RowSpan="2" VerticalAlignment="Stretch" RadiusX="20" RadiusY="20">
<Rectangle.Fill>
<SolidColorBrush Color="{StaticResource Amazonite}"/>
</Rectangle.Fill>
<interactivity:Interaction.Behaviors>
<core:DataTriggerBehavior x:Name="IsClient" Binding="{Binding Message_style}" ComparisonCondition="Equal" Value="0">
<core:ChangePropertyAction x:Name="Blue" PropertyName="Fill" TargetObject="{Binding ElementName=BackRect}" Value="{StaticResource Cola Blue}"/>
</core:DataTriggerBehavior>
<core:DataTriggerBehavior x:Name="NotIncludesData" Binding="{Binding Message_style}" ComparisonCondition="Equal" Value="1">
<core:ChangePropertyAction x:Name="Red" PropertyName="Fill" TargetObject="{Binding ElementName=BackRect}" Value="{StaticResource Crimson Red}"/>
</core:DataTriggerBehavior>
<core:DataTriggerBehavior x:Name="IsResultBalloon" Binding="{Binding Message_style}" ComparisonCondition="Equal" Value="2">
<core:ChangePropertyAction x:Name="Yellow" PropertyName="Fill" TargetObject="{Binding ElementName=BackRect}" Value="{StaticResource American Orange}"/>
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Rectangle>
<TextBlock x:Name="MessageText" HorizontalAlignment="Stretch" Margin="20,20,20,20" TextWrapping="Wrap" VerticalAlignment="Stretch" Text="{Binding Message}" Grid.Row="1" RequestedTheme="Default" Grid.ColumnSpan="2"/>
<TextBlock x:Name="UserText" HorizontalAlignment="Stretch" Margin="10,10,5,0" TextWrapping="Wrap" VerticalAlignment="Stretch" Text="{Binding User}" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" RequestedTheme="Default"/>
<TextBlock x:Name="DateText" HorizontalAlignment="Stretch" Margin="5,10,10,0" TextWrapping="Wrap" VerticalAlignment="Stretch" Text="{Binding Message_Time}" Grid.Column="1" TextAlignment="Center" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" RequestedTheme="Default"/>
</Grid>
</DataTemplate>
正如您在 xaml 代码中看到的那样,矩形颜色会根据给定的整数值更改其颜色,如下所示:
MessengerView.Items.Add(new MessageBaloon(("Hello", usr: MessageSender.Assistant, 3))); // Print the result.
class MessageBaloon
{
/// <summary>
/// Message sender enum types.
/// </summary>
public enum MessageSender
{
Client,
Assistant
}
private string message;
private MessageSender message_sender;
private string message_time;
private int message_style;
/// <summary>
/// Message.
/// </summary>
public string Message { get => message; }
/// <summary>
/// Get user.
/// </summary>
public MessageSender User { get => message_sender; }
/// <summary>
/// Time.
/// </summary>
public string Message_Time { get => message_time; }
/// <summary>
/// Determines message style. 0 for client, 1 for no data from assistant, 2 result from assistant, 3 for normal assistant prompt.
/// </summary>
public int Message_style { get => message_style; set => message_style = value; }
/// <summary>
/// Creating a message baloon for chat UI based on given datas.
/// </summary>
/// <param name="msg">Message string.</param>
/// <param name="usr">User type. Use MessageSender enum please.</param>
/// <param name="style_number"> Determines message style. 0 for client, 1 for no data from assistant, 2 result from assistant, 3 for normal assistant prompt. </param>
public MessageBaloon(string msg, MessageSender usr, int style_number )
{
message = msg;
message_sender = usr;
message_time = DateTime.Now.ToString();
message_style = style_number;
}
}
但是,正如您在本文中看到的那样,一段时间内行为控制会混合颜色...例如;如果消息气球的颜色必须是基于绑定值的浅蓝绿色,那么它会在一段时间内变成蓝色而不受代码干扰。你能帮我找出导致这个问题的原因吗?谢谢。
将此行为添加到模板中似乎可以解决问题。之前,我不使用蓝绿色的触发器,因为我认为它是模板的默认背景颜色,如果不发生其他情况,它不会改变。
<core:DataTriggerBehavior x:Name="IsAssistant" Binding="{Binding Message_style}" ComparisonCondition="Equal" Value="3">
<core:ChangePropertyAction x:Name="Green" PropertyName="Fill" TargetObject="{Binding ElementName=BackRect}" Value="{StaticResource Amazonite}"/>
</core:DataTriggerBehavior>
我正在使用 Microsoft.Xaml.Behaviors.Uwp.Managed NuGet 包来控制我的 UWP C# 应用中的行为。我有 ListView 和这样的自定义数据模板:
项目:
<ListView
ItemTemplate="{StaticResource ListViewMessageTemplate_Assistant}" x:Name="MessengerView"
BorderThickness="2,2,2,2" SelectionMode="None" Margin="10,10,10,10" IsMultiSelectCheckBoxEnabled="False" ScrollViewer.HorizontalScrollBarVisibility="Auto"
IsDoubleTapEnabled="False" IsHoldingEnabled="False"
IsRightTapEnabled="False" IsTapEnabled="False" ContainerContentChanging="MessengerView_ContainerContentChanging" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
数据模板:
<DataTemplate x:Key="ListViewMessageTemplate_Assistant">
<Grid x:Name="MainGrid" MaxWidth="500" Margin="10,0,10,10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Rectangle x:Name="BackRect" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Grid.RowSpan="2" VerticalAlignment="Stretch" RadiusX="20" RadiusY="20">
<Rectangle.Fill>
<SolidColorBrush Color="{StaticResource Amazonite}"/>
</Rectangle.Fill>
<interactivity:Interaction.Behaviors>
<core:DataTriggerBehavior x:Name="IsClient" Binding="{Binding Message_style}" ComparisonCondition="Equal" Value="0">
<core:ChangePropertyAction x:Name="Blue" PropertyName="Fill" TargetObject="{Binding ElementName=BackRect}" Value="{StaticResource Cola Blue}"/>
</core:DataTriggerBehavior>
<core:DataTriggerBehavior x:Name="NotIncludesData" Binding="{Binding Message_style}" ComparisonCondition="Equal" Value="1">
<core:ChangePropertyAction x:Name="Red" PropertyName="Fill" TargetObject="{Binding ElementName=BackRect}" Value="{StaticResource Crimson Red}"/>
</core:DataTriggerBehavior>
<core:DataTriggerBehavior x:Name="IsResultBalloon" Binding="{Binding Message_style}" ComparisonCondition="Equal" Value="2">
<core:ChangePropertyAction x:Name="Yellow" PropertyName="Fill" TargetObject="{Binding ElementName=BackRect}" Value="{StaticResource American Orange}"/>
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
</Rectangle>
<TextBlock x:Name="MessageText" HorizontalAlignment="Stretch" Margin="20,20,20,20" TextWrapping="Wrap" VerticalAlignment="Stretch" Text="{Binding Message}" Grid.Row="1" RequestedTheme="Default" Grid.ColumnSpan="2"/>
<TextBlock x:Name="UserText" HorizontalAlignment="Stretch" Margin="10,10,5,0" TextWrapping="Wrap" VerticalAlignment="Stretch" Text="{Binding User}" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" RequestedTheme="Default"/>
<TextBlock x:Name="DateText" HorizontalAlignment="Stretch" Margin="5,10,10,0" TextWrapping="Wrap" VerticalAlignment="Stretch" Text="{Binding Message_Time}" Grid.Column="1" TextAlignment="Center" Style="{StaticResource CaptionTextBlockStyle}" FontWeight="Bold" RequestedTheme="Default"/>
</Grid>
</DataTemplate>
正如您在 xaml 代码中看到的那样,矩形颜色会根据给定的整数值更改其颜色,如下所示:
MessengerView.Items.Add(new MessageBaloon(("Hello", usr: MessageSender.Assistant, 3))); // Print the result.
class MessageBaloon
{
/// <summary>
/// Message sender enum types.
/// </summary>
public enum MessageSender
{
Client,
Assistant
}
private string message;
private MessageSender message_sender;
private string message_time;
private int message_style;
/// <summary>
/// Message.
/// </summary>
public string Message { get => message; }
/// <summary>
/// Get user.
/// </summary>
public MessageSender User { get => message_sender; }
/// <summary>
/// Time.
/// </summary>
public string Message_Time { get => message_time; }
/// <summary>
/// Determines message style. 0 for client, 1 for no data from assistant, 2 result from assistant, 3 for normal assistant prompt.
/// </summary>
public int Message_style { get => message_style; set => message_style = value; }
/// <summary>
/// Creating a message baloon for chat UI based on given datas.
/// </summary>
/// <param name="msg">Message string.</param>
/// <param name="usr">User type. Use MessageSender enum please.</param>
/// <param name="style_number"> Determines message style. 0 for client, 1 for no data from assistant, 2 result from assistant, 3 for normal assistant prompt. </param>
public MessageBaloon(string msg, MessageSender usr, int style_number )
{
message = msg;
message_sender = usr;
message_time = DateTime.Now.ToString();
message_style = style_number;
}
}
但是,正如您在本文中看到的那样,一段时间内行为控制会混合颜色...例如;如果消息气球的颜色必须是基于绑定值的浅蓝绿色,那么它会在一段时间内变成蓝色而不受代码干扰。你能帮我找出导致这个问题的原因吗?谢谢。
将此行为添加到模板中似乎可以解决问题。之前,我不使用蓝绿色的触发器,因为我认为它是模板的默认背景颜色,如果不发生其他情况,它不会改变。
<core:DataTriggerBehavior x:Name="IsAssistant" Binding="{Binding Message_style}" ComparisonCondition="Equal" Value="3">
<core:ChangePropertyAction x:Name="Green" PropertyName="Fill" TargetObject="{Binding ElementName=BackRect}" Value="{StaticResource Amazonite}"/>
</core:DataTriggerBehavior>