Search code examples
xamarinsyncfusion

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


It seems that this line is not enough this line xmlns:local="clr-namespace:Chartsample"> it says ViewModel not found. I read that to use xlmns:local because it's coming the solution/project file.... then we would use "clr-namespace:Chartsample" where Chartsample is the name of the project. Now maybe perhaps it needs to be narrowed down? but how?

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>

Solution

  • Sorry for not too much understanding your question , if you need to bind a ViewModel to Xaml , there are two ways to bind it for MainPage .

    As shared code of 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 }
            };
        }
     }
    

    First , you can bind it in MainPage.cs as follow :

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

    Second , you can bind it in Xaml as follow :

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

    Here is a similar discussion for reference .