如何在XAML中添加ViewModel类的名称空间?

时间:2019-12-11 14:14:24

标签: xamarin syncfusion

我读了这篇,但是没有谈论如何添加名称空间。  Set BindingContext to ViewModel in XAML on Xamarin.Forms。 ////未找到本地ViewModel。如何在Xaml中为Viewmodel添加简单的名称空间?我只是在遵循一个网站上的教程,它说如果我想通过xaml进行绑定,那么我必须添加名称空间。如果在codebehind中完成很容易,但是在xaml文件中,我总是在猜测。

MainPage.cs

namespace Chartsample
{
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    //Now, let us define a simple data model that represents a data point in SfChart.
    public class Person
    {
        public string Name { get; set; }

        public double Height { get; set; }
    }

    //Next, create a view model class and initialize a list of Person objects as shown below,
    public class ViewModel
    {
        public List<Person> Data { get; set; }

        public ViewModel()
        {
            Data = new List<Person>()
        {
            new Person { Name = "David", Height = 180 },
            new Person { Name = "Michael", Height = 170 },
            new Person { Name = "Steve", Height = 160 },
            new Person { Name = "Joel", Height = 182 }
        };
        }
    }
}
}

MainPage.xaml

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:chart="clr-namespace:Syncfusion.SfChart.XForms;assembly=Syncfusion.SfChart.XForms"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
         x:Class="Chartsample.MainPage"
         xmlns:local="clr-namespace:Chartsample">

<ContentPage.BindingContext>
    <local:ViewModel></local:ViewModel>  //local ViewModel is not found
</ContentPage.BindingContext>

<chart:SfChart>

<chart:SfChart.PrimaryAxis>

    <chart:CategoryAxis>

    </chart:CategoryAxis>

</chart:SfChart.PrimaryAxis>

<chart:SfChart.SecondaryAxis>

    <chart:NumericalAxis>

    </chart:NumericalAxis>

</chart:SfChart.SecondaryAxis>

0 个答案:

没有答案