2015-10-01 74 views
2

我有以下非常簡單的repro情況。這是一個通用Windows(電話)8.1應用程序。我有一個非常簡單的模型:行爲SDK的行爲不一致

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> 

當然,行爲SDK加入到兩者(贏& 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:

Windows Phone behaviors work as expected

由於可見性映射到布爾值,所以這是可以的,並且是完全可以預期的,因此如果IsLoading爲false,則應該摺疊TextBlocks。但在Windows上,他們是不是:

Windows behaviors don't work as expected

我的問題是 - 爲什麼?我錯過了什麼?

將WP 8.1行爲與Windows 10 UWP中的行爲進行比較時,這也是有問題的。在UWP中,它的行爲就像在Windows 8.1上一樣,這使得從WP 8.1移植到UWP有點痛苦。

編輯:完整的攝製項目是在這裏:https://github.com/igrali/BehaviorsSDK_Bug

回答

3

這確實是個錯誤。我可以看到兩種解決方法。首先,如果您完全知道TextBlock的初始狀態應該是Collapsed,那麼您可以在XAML中將它們默認爲Collapsed,因爲它只是第一次不起作用。

或者,您可以直接在模板內附加所有Behavior s到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> 
+2

我就意識到可能的解決辦法,但謝謝你幫我確認一下,這其實是一個錯誤/不一致! :) –