WinRT FlipView绑定可能失败

时间:2013-04-20 11:41:17

标签: c# xaml windows-runtime flipview

我有一个xaml页面:

<Page x:Class="DailyStyleW8.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:DailyStyleW8"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:converters="using:DataTypes"
      mc:Ignorable="d">

  <Page.Resources>
    <converters:PortableImageConverter x:Key="ImageConverter" />
  </Page.Resources>

  <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid>
      <ProgressBar x:Name="loadingViewer"
                   IsIndeterminate="True"
                   Height="20" />
      <FlipView x:Name="displayViewer"
                ItemsSource="{Binding}"
                Visibility="Collapsed">
        <FlipView.ItemTemplate>
          <DataTemplate>
            <Grid>
              <Image Source="{Binding Image,Converter={StaticResource ImageConverter}}" />
              <TextBlock Text="{Binding Name}" />
            </Grid>
          </DataTemplate>
        </FlipView.ItemTemplate>
      </FlipView>
    </Grid>
  </Grid>
</Page>

并在文件后面的代码中:

using DailyStyleApp;
using PortableAPI;
using System;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace DailyStyleW8
{
    /// <summary>
    /// Display a list of recent updates to the user
    /// </summary>
    public sealed partial class MainPage : Page
    {
        Controller controller = new Controller();

        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            LoadContent();
        }

        private async void LoadContent()
        {
            var viewModel = await controller.GetMultiDayAsync(DateTime.Now, PortableAPIProvider.Storage.ReadFromSettings<int>("CacheDuration", 7));
            displayViewer.ItemsSource = viewModel.Items;
            displayViewer.Visibility = Windows.UI.Xaml.Visibility.Visible;
            loadingViewer.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }
    }
}

现在,当我运行代码时,正确调用LoadContent函数并正确形成viewModel对象。如果我注释掉displayViewer.ItemsSource = viewModel.Items;行,则ProgressBar可见性会按预期更改。

当保留该行并逐步执行LoadContent方法中的所有4行时,FlipView不会使用新项目进行更新,ProgressBar可见性不会改变。 viewModel.Items的类型为List<T>

我甚至确定在这里寻找什么。我猜这是XAML和我的绑定有问题吗?

1 个答案:

答案 0 :(得分:0)

与此问题相关的问题实际上与应用程序中的另一部分代码有关。在其他地方,我有一系列异步/等待调用,这些调用锁定了UI线程。

这阻止了调度程序触发异步的回调。解决问题的简短方法:永远不要在从UI线程调用的东西上调用(而不是通过另一个异步调用)。