Behaviors SDK 的行为不一致
Inconsistent behavior of Behaviors SDK
我有以下非常简单的重现案例。这是一款通用 Windows (Phone) 8.1 应用程序。我有一个非常简单的模型:
public class SimpleModel
{
public bool IsLoading { get; set; } = false;
}
以及这些模型的集合,定义为共享 类:
public class SimpleModelCollection : List<SimpleModel>
{
public SimpleModelCollection()
{
this.Add(new SimpleModel());
this.Add(new SimpleModel());
this.Add(new SimpleModel());
this.Add(new SimpleModel());
this.Add(new SimpleModel());
this.Add(new SimpleModel());
}
}
我在 Windows 和 Windows Phone 8.1 项目中只有一个页面。他们有相同的 XAML,一个简单的 ItemsControl:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ItemsControl x:Name="items" HorizontalAlignment="Center">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<interactivity:Interaction.Behaviors>
<core:DataTriggerBehavior Binding="{Binding IsLoading}" Value="True">
<core:ChangePropertyAction TargetObject="{Binding ElementName=text}" PropertyName="Visibility" Value="Visible"/>
</core:DataTriggerBehavior>
<core:DataTriggerBehavior Binding="{Binding IsLoading}" Value="False">
<core:ChangePropertyAction TargetObject="{Binding ElementName=text}" PropertyName="Visibility" Value="Collapsed"/>
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
<StackPanel>
<TextBlock x:Name="text" Text="Should I be visible?" FontSize="26"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
当然,Behaviors SDK 已添加到两个 (Win & WP 8.1) 项目中。
在代码隐藏中,我将 ItemsSource 设置为前面提到的简单模型集合的一个实例。 Windows Phone 构造函数:
public MainPage()
{
this.InitializeComponent();
this.items.ItemsSource = new SimpleModelCollection();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
和Windows构造函数:
public MainPage()
{
this.InitializeComponent();
this.items.ItemsSource = new SimpleModelCollection();
}
如果将 IsLoading 初始化为 false,您期望得到什么结果:
public bool IsLoading { get; set; } = false;
让我向您展示如果 Isloading 在 Windows Phone:
上初始化为 false 时应用程序的外观
这没问题,并且完全符合预期,因为 Visibility 映射到 bool 值,所以如果 IsLoading 为 false,TextBlocks 应该折叠。但在 Windows 上,它们不是:
我的问题是 - 为什么?我错过了什么?
将 WP 8.1 行为与 Windows 10 UWP 中的行为进行比较时,这也是有问题的。在 UWP 中,它的行为就像在 Windows 8.1 上一样,这使得从 WP 8.1 移植到 UWP 有点痛苦。
编辑:完整的重现项目在这里:https://github.com/igrali/BehaviorsSDK_Bug
这确实是一个错误。我可以看到两种修复方法。
首先,如果你完全知道 TextBlock
的初始状态应该是 Collapsed
,你可以将它们默认为 XAML 中的 Collapsed
因为它只第一次不行。
或者,您可以将所有 Behavior
直接附加到模板内的 Textblock
。这也应该有效。
<DataTemplate>
<Grid>
<StackPanel>
<TextBlock x:Name="text" Text="Should I be visible?" FontSize="26">
<interactivity:Interaction.Behaviors>
<core:DataTriggerBehavior Binding="{Binding IsLoading}" Value="True">
<core:ChangePropertyAction TargetObject="{Binding ElementName=text}" PropertyName="Visibility" Value="Visible" />
</core:DataTriggerBehavior>
<core:DataTriggerBehavior Binding="{Binding IsLoading}" Value="False">
<core:ChangePropertyAction TargetObject="{Binding ElementName=text}" PropertyName="Visibility" Value="Collapsed" />
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
</TextBlock>
<Button Click="Button_Click" Content="Visible?" />
</StackPanel>
</Grid>
</DataTemplate>
我有以下非常简单的重现案例。这是一款通用 Windows (Phone) 8.1 应用程序。我有一个非常简单的模型:
public class SimpleModel
{
public bool IsLoading { get; set; } = false;
}
以及这些模型的集合,定义为共享 类:
public class SimpleModelCollection : List<SimpleModel>
{
public SimpleModelCollection()
{
this.Add(new SimpleModel());
this.Add(new SimpleModel());
this.Add(new SimpleModel());
this.Add(new SimpleModel());
this.Add(new SimpleModel());
this.Add(new SimpleModel());
}
}
我在 Windows 和 Windows Phone 8.1 项目中只有一个页面。他们有相同的 XAML,一个简单的 ItemsControl:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ItemsControl x:Name="items" HorizontalAlignment="Center">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<interactivity:Interaction.Behaviors>
<core:DataTriggerBehavior Binding="{Binding IsLoading}" Value="True">
<core:ChangePropertyAction TargetObject="{Binding ElementName=text}" PropertyName="Visibility" Value="Visible"/>
</core:DataTriggerBehavior>
<core:DataTriggerBehavior Binding="{Binding IsLoading}" Value="False">
<core:ChangePropertyAction TargetObject="{Binding ElementName=text}" PropertyName="Visibility" Value="Collapsed"/>
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
<StackPanel>
<TextBlock x:Name="text" Text="Should I be visible?" FontSize="26"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
当然,Behaviors SDK 已添加到两个 (Win & WP 8.1) 项目中。
在代码隐藏中,我将 ItemsSource 设置为前面提到的简单模型集合的一个实例。 Windows Phone 构造函数:
public MainPage()
{
this.InitializeComponent();
this.items.ItemsSource = new SimpleModelCollection();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
和Windows构造函数:
public MainPage()
{
this.InitializeComponent();
this.items.ItemsSource = new SimpleModelCollection();
}
如果将 IsLoading 初始化为 false,您期望得到什么结果:
public bool IsLoading { get; set; } = false;
让我向您展示如果 Isloading 在 Windows Phone:
上初始化为 false 时应用程序的外观这没问题,并且完全符合预期,因为 Visibility 映射到 bool 值,所以如果 IsLoading 为 false,TextBlocks 应该折叠。但在 Windows 上,它们不是:
我的问题是 - 为什么?我错过了什么?
将 WP 8.1 行为与 Windows 10 UWP 中的行为进行比较时,这也是有问题的。在 UWP 中,它的行为就像在 Windows 8.1 上一样,这使得从 WP 8.1 移植到 UWP 有点痛苦。
编辑:完整的重现项目在这里:https://github.com/igrali/BehaviorsSDK_Bug
这确实是一个错误。我可以看到两种修复方法。
首先,如果你完全知道 TextBlock
的初始状态应该是 Collapsed
,你可以将它们默认为 XAML 中的 Collapsed
因为它只第一次不行。
或者,您可以将所有 Behavior
直接附加到模板内的 Textblock
。这也应该有效。
<DataTemplate>
<Grid>
<StackPanel>
<TextBlock x:Name="text" Text="Should I be visible?" FontSize="26">
<interactivity:Interaction.Behaviors>
<core:DataTriggerBehavior Binding="{Binding IsLoading}" Value="True">
<core:ChangePropertyAction TargetObject="{Binding ElementName=text}" PropertyName="Visibility" Value="Visible" />
</core:DataTriggerBehavior>
<core:DataTriggerBehavior Binding="{Binding IsLoading}" Value="False">
<core:ChangePropertyAction TargetObject="{Binding ElementName=text}" PropertyName="Visibility" Value="Collapsed" />
</core:DataTriggerBehavior>
</interactivity:Interaction.Behaviors>
</TextBlock>
<Button Click="Button_Click" Content="Visible?" />
</StackPanel>
</Grid>
</DataTemplate>