xaml 无法识别模型的 属性 (WPF MVVM)

Model's property not recognized by xaml (WPF MVVM)

使用 mvvmCross,.net (5.0),visual studio 19

查看模型:

using System.ComponentModel;
using System.Threading.Tasks;
using MvvmCross.ViewModels;
using MvxR_M_S.Core.API;
using MvxR_M_S.Core.Models;

namespace MvxR_M_S.Core.ViewModels
{
    public class ArticlePresentationViewModel : MvxViewModel
    {
        public ArticlePresentationViewModel()
        {
        }

        public override async void ViewAppeared()
        {
            base.ViewAppeared();
            await LoadArticles(new ArticleEndpoint(new APIHelper()));
        }

        public async Task LoadArticles(ArticleEndpoint articleEndpoint)
        {
            var articleList = await articleEndpoint.GetAll();
            Articles = new BindingList<ArticleModel>(articleList);
        }

        private BindingList<ArticleModel> _articles;

        public BindingList<ArticleModel> Articles
        {
            get { return _articles; }
            set
            {
                _articles = value;
                SetProperty(ref _articles, value);
            }
        }
    }
}

型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MvxR_M_S.Core.Models
{
    public class ArticleModel
    {
        public string ArticleName { get; set; }
    }
}

查看:

<views:MvxWpfView
    xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
    xmlns:mvx="clr-namespace:MvvmCross.Platforms.Wpf.Binding;assembly=MvvmCross.Platforms.Wpf"
    x:Class="MvxR_M_S.Wpf.Views.ArticlePresentationView"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:models="clr-namespace:MvxR_M_S.Core.Models;assembly=MvxR_M_S.Core"
                 xmlns:viewModels="clr-namespace:MvxR_M_S.Core.ViewModels;assembly=MvxR_M_S.Core"
                 mc:Ignorable="d" FontSize="20" Background="Wheat"
            d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="20" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="20" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="20" />
        </Grid.RowDefinitions>

        <TextBlock Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Center" Text="Article list" />

        <ListBox x:Name="Articles" ItemsSource="{Binding Articles}" Grid.Column="2" Grid.Row="2" MinHeight="300" MinWidth="300" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding ArticleName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</views:MvxWpfView>

问题出在列表框中,它引用了 ItemsSource,我也在 Articles 属性 进行了调试在 ViewModel 内部,它保存着正确的信息,但启动时屏幕上没有显示任何内容。

所以我想这是有约束力的问题。

由于您是在单独的线程中检索文章,我认为您必须这样做:

Dispatcher.BeginInvoke(new Action(() =>  Articles = new BindingList<ArticleModel>(articleList);));

我不确定这是否正确,但 Dispatcher.BeginInvoke 根据我的发现似乎需要

删除文章 setter 中的 _articles = value;。如果该字段已设置,SetProperty 不执行任何操作

public BindingList<ArticleModel> Articles
    {
        get { return _articles; }
        set
        {
            SetProperty(ref _articles, value);
        }
    }