【发布时间】:2015-10-01 22:06:20
【问题描述】:
我有以下非常简单的复制案例。这是一个通用的 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
【问题讨论】:
标签: xaml windows-phone-8.1 windows-8.1 win-universal-app uwp