gpt4 book ai didi

c# - 将 ListView 与来自 ViewModel 的 List 绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 10:33:20 26 4
gpt4 key购买 nike

我正在为学校开发一个使用 Mvvm 格式的 UWP 应用程序,需要一些帮助。

所以我正在尝试使用来自我的 ViewModel 中的列表的项目制作一个 ListView。

这是一些代码:

MainScreenViewModel.cs

using EasySleep.Model;
using EasySleep.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Template10.Mvvm;
using Template10.Services.NavigationService;
using Windows.UI.Xaml.Navigation;

namespace EasySleep.ViewModels
{
class MainScreenViewModel : ViewModelBase
{

ServiceApi serviceApi;

public List<Offer> TestList { get; set; }

public MainScreenViewModel()
{
serviceApi = new ServiceApi();
TestList = new List<Offer>();
TestList.Add(new Offer(1, true, null, "Decription de dingue", 3));
TestList.Add(new Offer(3, false, null, "Decription de fou", 6));
TestList.Add(new Offer(7, true, null, "Decription de perdu", 9));
}

public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState)
{
await Task.CompletedTask;
}

public void GoToMainScreen() =>
NavigationService.Navigate(typeof(Views.MainScreen));

public void GotoSettings() =>
NavigationService.Navigate(typeof(Views.SettingsPage), 0);

public void GotoAbout() =>
NavigationService.Navigate(typeof(Views.SettingsPage), 1);

public void Logout()
{
ServiceApi.Token = null;
NavigationService.Navigate(typeof(Views.MainPage));
}

}
}

MainScreenPage.xaml
<ListView x:Name="AllActiveOffersListView" 
ItemsSource="{x:Bind ViewModel.TestList}"
Grid.Row="1">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

报价.cs
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasySleep.Model
{
public class Offer
{

public int Id { get; set; }
public Boolean IsActive { get; set; }
public List<Photo> Photos { get; set; }
public String Description { get; set; }
public int MaxPeople { get; set; }
public int LocationId { get; set; }
public Location Location { get; set; }
public ApplicationUser Owner { get; set; }
public String OwnerId { get; set; }

public Offer (int id, Boolean isActive, List<Photo> photos, string description, int maxPeople)
{
Id = id;
IsActive = isActive;
Photos = photos;
Description = description;
MaxPeople = maxPeople;
}

[JsonConstructor]
public Offer(string description, int id, Boolean isActive, Location loc, int locationId, int maxPeople, ApplicationUser owner, string ownerId)
{
Id = id;
IsActive = isActive;
Description = description;
MaxPeople = maxPeople;
Location = loc;
LocationId = locationId;
Owner = owner;
OwnerId = ownerId;
}

public override string ToString()
{
return Description + LocationId;
}

}
}

你能帮我装订这些东西吗?

我编辑添加了 Offer.cs 模型

最佳答案

简单 <TextBlock Text="{Binding Description}" />应该可以工作,无需指定其他任何内容。
我创建了一个空的 Template10 应用程序并将您的模型放在那里,它可以工作。

这是主页xaml:

<Page.DataContext>
<vm:MainPageViewModel x:Name="ViewModel" />
</Page.DataContext>

<RelativePanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<controls:PageHeader x:Name="pageHeader"
Content="Main Page"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.AlignTopWithPanel="True" />

<RelativePanel EntranceNavigationTransitionInfo.IsTargetElement="True"
RelativePanel.AlignBottomWithPanel="True"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.Below="pageHeader">

<!-- content -->
<ListView x:Name="AllActiveOffersListView"
ItemsSource="{x:Bind ViewModel.TestList}"
Grid.Row="1">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

</RelativePanel>

</RelativePanel>

您可以从 http://personal.sirma.bg/Jogy/download/WindowsApp1.zip 下载完整的工作项目并检查它是否适合你,然后看看你的项目和我的项目有什么不同。

乔吉

关于c# - 将 ListView 与来自 ViewModel 的 List 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45574737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com