这行似乎还不够,这行xmlns:local =“ clr-namespace:Chartsample”> 它说找不到ViewModel。我读到它使用xlmns:local,因为它是解决方案/项目文件。...然后我们将使用“ 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>
答案 0 :(得分:0)
如何在xaml中对其命名空间
xmlns:local="clr-namespace:Chartsample"> //"clr-namespace:nameofproject">
要访问xaml中的一个类,它必须是自己的,而不是在我的.cs文件中的另一个类中,该类在公共部分类MainPage中
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 }
};
}
}
答案 1 :(得分:0)
所有需要的其他类都添加到MainPage类的范围中。因此,只能使用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 }
};
}
}
}
答案 2 :(得分:0)
很抱歉,您对问题的理解不多,如果您需要将ViewModel
绑定到Xaml
,则有两种方法将其绑定到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供参考。