如何在 xaml 中添加命名空间以便我可以在 MainPage.cs 中使用名为 ViewModel 的 class?

how to add a namespace in xaml so i can use a class called ViewModel in MainPage.cs?

这行好像不够这行xmlns:local="clr-namespace:Chartsample"> 它说未找到 ViewModel。我读到它是为了使用 xlmns:local,因为它来自 solution/project 文件....然后我们将使用 "clr-namespace:Chartsample",其中 Chartsample 是项目的名称。现在也许需要缩小范围?但是如何呢?

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

<?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>
</ContentPage.BindingContext>

如何在 xaml

中为其命名空间
xmlns:local="clr-namespace:Chartsample">  //"clr-namespace:nameofproject">

要访问 xaml 中的 class 它需要单独存在而不是在另一个 class 中,在我的 .cs 文件中它位于 public 部分 class主页

   public partial class MainPage : ContentPage
    {
       public MainPage()
       {
           InitializeComponent();
       }
    }  //this is missing in code above

    //class view model on it's own
    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 }
            };
        }
     }

所有需要的其他 class 都已添加到 MainPage class 的范围内。仅此而已,无法通过使用 XAML 中的命名空间 Chartsample 来访问它。请找到修改后的代码片段来解决问题,

修改后的代码片段 [C#]:

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

抱歉没有太理解你的问题,如果你需要绑定一个ViewModelXaml,有两种方法可以绑定MainPage

作为ViewModel的共享码:

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.cs中绑定如下:

public partial class MainPage : ContentPage
{
   public MainPage()
   {
       InitializeComponent();
       ViewModel viewmodel = new ViewModel();
       BindingContext = viewmodel.Data; // Here bind the model to MainPage.cs , then can use it in Xmal of MainPage
   }
}

其次,可以在Xaml中绑定如下:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:viewModels="clr-App18; assembly=App18"
             x:Class="App18"
             Title="MainPage">
  <ContentPage.BindingContext>
    <viewModels:ViewModel/>
  </ContentPage.BindingContext>
  <StackLayout BindingContext="Data">
    <Label Text="{Binding test}"/>
  </StackLayout>
</ContentPage> 

这里有个类似的discussion供参考。