Xaml 行为的不一致行为
我有一个非常简单的例子。这是一个通用的Windows(Phone)8.1应用程序。我有一个非常简单的模型:Xaml 行为的不一致行为,xaml,windows-phone-8.1,windows-8.1,win-universal-app,uwp,Xaml,Windows Phone 8.1,Windows 8.1,Win Universal App,Uwp,我有一个非常简单的例子。这是一个通用的Windows(Phone)8.1应用程序。我有一个非常简单的模型: public class SimpleModel { public bool IsLoading { get; set; } = false; } 以及这些模型的集合,定义为共享类: public class SimpleModelCollection : List<SimpleModel> { public SimpleModelCollection()
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构造函数:
public MainPage()
{
this.InitializeComponent();
this.items.ItemsSource = new SimpleModelCollection();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
public MainPage()
{
this.InitializeComponent();
this.items.ItemsSource = new SimpleModelCollection();
}
如果IsLoading被初始化为false,您会期望得到什么结果:
public bool IsLoading { get; set; } = false;
如果Windows Phone上的Isloading初始化为false,我将向您展示应用程序的外观:
这是正常的,也是完全可以预料的,因为可见性映射到bool值,所以如果IsLoading为false,则文本块应该折叠。但在Windows上,它们不是:
我的问题是——为什么?我错过了什么
当将WP 8.1行为与Windows 10 UWP中的行为进行比较时,这也是一个问题。在UWP中,它的行为就像在Windows8.1上一样,这使得从WP8.1移植到UWP有点痛苦
编辑:完整的复制项目在这里:这确实是一个bug。我有两种方法可以解决这个问题 首先,如果您完全知道
文本块
的初始状态应该是折叠
,您可以在XAML中将它们默认为折叠
,因为它只是第一次不起作用
或者,您可以将所有行为
直接附加到模板内部的文本块
。这也应该奏效
<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>
我知道可能的解决方案,但感谢您帮助我确认这实际上是一个bug/不一致性!:)
<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>