C#UWP在viewmodel中找不到该属性

时间:2017-11-06 19:05:28

标签: c# viewmodel uwp-xaml

我的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;
        }
    }
}

我想知道这里是否有人可以看到我出错的地方?

2 个答案:

答案 0 :(得分:0)

尝试将绑定调整为

Source="{x:Bind Courses}"

(删除ViewModel.部分)

答案 1 :(得分:0)

首先,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; }
}

尝试在工作小型演示中添加更多代码以缩小问题范围。如果您仍有问题,请提供最小的转载项目。