如果在等待的异步调用

时间:2017-08-01 17:23:37

标签: c# xaml uwp async-await

我的UWP应用程序中有一个公共布尔值,用于显示/隐藏ProgressBar。我在其他地方使用了相同的模式,它似乎工作正常,但我在特定页面上遇到问题,只有在中它似乎才会更新 我在等待异步调用之后设置了布尔值(与工作页面相同的模式)。

这是我的XAML视图:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Text="{ x:Bind Vm.IsLoaded }" Margin="112,272,-112,-272"></TextBlock>
</Grid>

代码隐藏:

public sealed partial class MainPage : Page
{
    public MainPageViewModel Vm => DataContext as MainPageViewModel;
    public MainPage()
    {
        this.InitializeComponent();
        this.DataContext = new MainPageViewModel();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        Vm.GetProjectData();
    }
}

这是我的MainPageViewModel.cs

public class MainPageViewModel : ViewModel
{
    public ObservableCollection<Project> Projects { get; set; } = new ObservableCollection<Project>();

    private bool _isLoaded;
    public bool IsLoaded
    {
        get { return _isLoaded; }
        set
        {
            _isLoaded = value;
            OnPropertyChanged();
        }
    }

    public async Task GetProjectData()
    {
        // If I put `IsLoaded = true;` here it displays `true` in the UI
        var projectsResponse = await HttpUtil.GetAsync(StringUtil.ProjectsUrl());
        // If I put `IsLoaded = true;` here it still displays `false` in the UI
        if (projectsResponse.IsSuccessStatusCode)
        {
            var projectsResponseString = await projectsResponse.Content.ReadAsStringAsync();
            var projects = JsonUtil.SerializeJsonToObject<List<Project>>(projectsResponseString);

            foreach (var project in projects)
            {
                Projects.Add(project);
            }

            IsLoaded = true;
       }
    }
}

我的ViewModel.cs

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

无论我把IsLoaded = true;放在哪里,它总是会击中OnPropertyChanged()。

这是我的工作代码:

ProjectViewViewModel.cs:

public class ProjectViewViewModel : ViewModel
{
    public ObservableCollection<Story> MyData { get; set; } = new ObservableCollection<Story>();
    private bool _dataIsLoaded;
    public bool DataIsLoaded
    {
        get { return _dataIsLoaded; }
        set
        {
            _dataIsLoaded = value;
            OnPropertyChanged();
        }
    }

    public async Task GetData(Project project)
    {
        DataIsLoaded = false;
        var stringResponse = await HttpUtil.GetAsync(StringUtil.Query(project.Id, "MB"));
        if (stringResponse.IsSuccessStatusCode)
        {
            // Do Stuff

            DataIsLoaded = true;
        }
    }
}

ProjectView.xaml.cs

public sealed partial class ProjectView : Page
{
    public Project Project { get; set; }
    public bool IsLoaded { get; set; }
    public ProjectViewViewModel Vm => DataContext as ProjectViewViewModel;
    public ProjectView()
    {
        this.InitializeComponent();
        this.DataContext = new ProjectViewViewModel();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        Project = e.Parameter as Project;
        Vm.GetData(Project);
    }
}

我觉得我错过了一些非常明显的东西,但我看不到树林里的树木,它让我疯狂。非常感谢任何帮助!

1 个答案:

答案 0 :(得分:6)

我相信你遇到的问题在于你的标记。 x:Bind的默认绑定模式为OneTime;因此,文本块中的文本在应用程序启动时绑定到IsLoaded的值,或者在文本块的数据上下文发生更改时绑定。

将绑定模式设置为OneWay会导致异步函数返回后文本块更新中的值。

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock Text="{ x:Bind Path=Vm.IsLoaded, Mode=OneWay }" Margin="112,272,-112,-272"></TextBlock>
</Grid>

如果您有兴趣,this article会详细介绍x:Bind的使用。此外,this article涵盖了BindingMode枚举中的值。