WinRT FlipView绑定可能失败

本文关键字:失败 绑定 FlipView WinRT | 更新日期: 2023-09-27 18:10:19

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

WinRT FlipView绑定可能失败

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

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