我已经在 wpf 中实现了 MVVM 模式。但是结果没有显示在视图中。有人可以帮帮我吗?

I have implemented MVVM pattern in wpf. But the result doesnt showed in the view. Can some one please help me?

使用 getAllCars() 函数,我从后端 webapi 获取所有汽车,并将它们保存在 ObservableCollection Cars 中。当我 运行 应用程序时,它确实显示任何 data.I 不知道为什么数据没有显示,请任何人帮忙?...................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... .........

MainWindow.xaml

image of window

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CarsUIWpfApp"
        xmlns:ViewModels="clr-namespace:CarsUIWpfApp.ViewModels" x:Class="CarsUIWpfApp.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        WindowStartupLocation="CenterScreen">

        <ListView Margin="10" Name="carsList" ItemsSource="{Binding Cars}">
            <ListView.DataContext>
                <ViewModels:CarViewModel/>
            </ListView.DataContext>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="CarBrand" Width="auto" DisplayMemberBinding="{Binding CarBrand}" />
                    <GridViewColumn Header="CarModel" Width="auto" DisplayMemberBinding="{Binding CarModel}" />
                    <GridViewColumn Header="CarVin" Width="auto" DisplayMemberBinding="{Binding CarVin}" />
                    <GridViewColumn Header="Color" Width="auto" DisplayMemberBinding="{Binding Color}" />
                </GridView>
            </ListView.View>
        </ListView>

    </Grid>
</Window>

MainWindow.xaml.cs

 public partial class MainWindow : Window

    {

        public MainWindow()
        {
            InitializeComponent();
        }
    }

CarViewModel

class CarViewModel : INotifyPropertyChanged
{
    private const string path = "https://localhost:44363/api/Cars";
   private ObservableCollection<Car> cars = new ObservableCollection<Car>();


    public CarViewModel()
    {
        LoadCars();
    }
    public ObservableCollection<Car> Cars
    {
        get { return cars; }
        set { SetProperty<ObservableCollection<Car>>(ref cars, value);
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public async void LoadCars()
    {
        Cars = await getAllCars();
    }

    public async Task<ObservableCollection<Car>> getAllCars()
    {
        

        using (HttpClient client = new HttpClient())
        {
            HttpResponseMessage response = await client.GetAsync(path);

            cars = await response.Content.ReadAsAsync<ObservableCollection<Car>>();
            return cars;
        }

    }

}

Debuging image

您不得在视图模型中声明和调用 async void 方法。

声明为async Task

public async Task LoadCars()
{
    Cars = await getAllCars();
}

并从视图模型构造函数中删除调用,或者删除整个 CarViewModel 构造函数。

然后在视图中,将 DataContext 分配移动到 MainWindow 并附加一个异步 Loaded 事件处理程序:

<Window ... Loaded="WindowLoaded">
    <Window.DataContext>
        <ViewModels:CarViewModel/>
    </Window.DataContext>
    ...
</Window>

在 MainWindow.xaml.cs 中使用此处理程序方法:

private async void WindowLoaded(object sender, RoutedEventArgs e)
{
    await ((CarViewModel)DataContext).LoadCars();
}

另请注意,Cars 不需要是 ObservableCollection,只要您只创建和分配完整的集合,并且从不在现有 Cars 集合中添加或删除元素。

<ListView.DataContext>
      <ViewModels:CarViewModel/>
</ListView.DataContext>

这样做意味着列表视图将此 viewModel 作为数据上下文,但在您的项目源中您绑定到汽车集合,因此解决方案是这样做:

<Window.DataContext>
      <ViewModels:CarViewModel/>
</Window.DataContext>