[英]C# UWP The property was not found in viewmodel
我的CollectionViewSource在VS中带有下划线,并显示以下消息:“在MainPageViewModel中找不到属性“课程””,我似乎无法找出为什么找不到该属性,因此我正在寻求帮助。 我的MainPage.xaml看起来像这样:
<Page x:Class="CourseStudent.UWP.Views.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:controls="using:Template10.Controls"
xmlns:converters="using:Template10.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:CourseStudent.UWP.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:CourseStudent.UWP.ViewModels" mc:Ignorable="d">
<Page.DataContext>
<vm:MainPageViewModel x:Name="ViewModel" />
</Page.DataContext>
<Page.Resources>
<converters:StringFormatConverter x:Key="CoursesHeaderConverter" Format="Courses ({0})"/>
<!-- Collection of courses displayed by this page -->
<CollectionViewSource
x:Name="coursesViewSource"
Source="{x:Bind ViewModel.Courses}"
d:Source="{d:DesignData /SampleData/CoursesSampleData.xaml}"/>
</Page.Resources>
<RelativePanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<controls:PageHeader x:Name="pageHeader" Content="Main Page"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.AlignTopWithPanel="True" />
<TextBlock x:Name="mainTextBlock" Margin="16,12,0,0"
RelativePanel.AlignLeftWithPanel="True"
RelativePanel.Below="pageHeader" Text="Courses" />
</RelativePanel>
</Page>
我的MainPageViewModel.cs看起来像这样
using CourseStudent.Model;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Template10.Mvvm;
using Template10.Services.NavigationService;
using Windows.UI.Xaml.Navigation;
namespace CourseStudent.UWP.ViewModel
{
public class MainPageViewModel : ViewModelBase
{
public MainPageViewModel()
{
if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
{ }
}
ObservableCollection<Course> courses;
public ObservableCollection<Course> Courses { get { return courses; } set { Set(ref courses, value); } }
public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState)
{
if (Courses == null) // get courses from the data API
{
Courses = new ObservableCollection<Course>
{
new Course() { CourseName = "blabla"},
new Course() { CourseName = "bloblo"},
new Course() { CourseName = "jojo"},
};
}
if (suspensionState.Any())
{
}
await Task.CompletedTask;
}
public override async Task OnNavigatedFromAsync(IDictionary<string, object> suspensionState, bool suspending)
{
if (suspending)
{
}
await Task.CompletedTask;
}
public override async Task OnNavigatingFromAsync(NavigatingEventArgs args)
{
args.Cancel = false;
await Task.CompletedTask;
}
}
}
我想知道这里是否有人可以看到我哪里出问题了?
尝试将绑定调整为
Source="{x:Bind Courses}"
(删除ViewModel.
部分)
首先,对于命名空间MainPageViewModel
是CourseStudent.UWP.ViewModel
在你身边,但你引用什么是using:CourseStudent.UWP.ViewModels
具有额外的s
加入。 您可以先检查一下。
除此之外,我无法一面再现您的问题。 由于您的代码段尚未完成,因此我提供了一个可供参考的最小工作示例。
MainPage.xaml
<Page.DataContext>
<local:MainPageViewModel x:Name="ViewModel" />
</Page.DataContext>
<Page.Resources>
<!-- Collection of courses displayed by this page -->
<CollectionViewSource x:Name="coursesViewSource" Source="{x:Bind ViewModel.Courses}" />
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView ItemsSource="{Binding Source={StaticResource coursesViewSource}}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Course">
<TextBlock Text="{x:Bind CourseName}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
MainPageViewModel
public class MainPageViewModel
{
public MainPageViewModel()
{
courses = new ObservableCollection<Course>()
{
new Course() { CourseName = "blabla"},
new Course() { CourseName = "bloblo"},
new Course() { CourseName = "jojo"}
};
}
ObservableCollection<Course> courses;
public ObservableCollection<Course> Courses { get => courses; set => courses = value; }
}
public class Course
{
public string CourseName { get; set; }
}
尝试在正在运行的小型演示中添加更多代码,以缩小问题的范围。 如果仍有问题,请提供最小的复制项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.