0

我有一个 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 和我的绑定有问题?

4

1 回答 1

0

与此问题相关的问题实际上与应用程序中的另一段代码有关。在其他地方,我有一系列锁定 UI 线程的 async / await 调用。

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

于 2013-11-26T15:56:17.737 回答