在这里,我定义了数据myListOfEmployeeObjects:
public class App : Application
{
public List<Employee> myListOfEmployeeObjects;
public App ()
{
Employee emp1 = new Employee () {
FirstName = "Max",
LastName = "Mustermann",
Twitter = "@fake1"
};
Employee emp2 = new Employee () {
FirstName = "Evy",
LastName = "Mustermann",
Twitter = "@fake2"
};
myListOfEmployeeObjects = new List<Employee> {
emp1, emp2
};
MainPage = new NavigationPage (new EmployeeListPage ());
}
}
比起我的XAML,我在其中设置了ItemsSource
:
<ListView x:Name="listView"
IsVisible="false"
ItemsSource="{x:Static local:App.myListOfEmployeeObjects}"
ItemSelected="EmployeeListOnItemSelected">
应该行吗?因为我得到
Xamarin.Forms.Xaml.XamlParseException:在xmlns中找不到类型的应用程序
public partial class EmployeeListPage : ContentPage {
private ListView listView;
private void InitializeComponent() {
this.LoadFromXaml(typeof(EmployeeListPage)); // here the exception is thrown
listView = this.FindByName <ListView>("listView");
}
}
如何设置ItemsSource
XAML?
编辑:
现在,我尝试了来自user2425632的建议,如果执行以下更改,它将起作用:
xmlns:local="clr-namespace:HelloXamarinFormsWorld;assembly=HelloXamarinFormsWorld"
到我的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:local="clr-namespace:HelloXamarinFormsWorld;assembly=HelloXamarinFormsWorld"
x:Class="HelloXamarinFormsWorld.EmployeeListPage"
Title="Employee List">
<ContentPage.Content>
当然,您必须更改名称以使其适合您的项目。
我删除了IsVisible
和ItemSelected
。
<ListView ItemsSource="{x:Static local:App.myListOfEmployeeObjects}">
它必须是静态的,否则您将得到
找不到local:App.myListOfEmployeeObjects的静态成员
public static List<Employee> myListOfEmployeeObjects { private set; get; }
public static void GetAllEmployees(){
Employee emp1 = new Employee () {
FirstName = "Max",
LastName = "Mustermann",
Twitter = "@fake1"
};
Employee emp2 = new Employee () {
FirstName = "Eva",
LastName = "Mustermann",
Twitter = "@fake2"
};
myListOfEmployeeObjects = new List<Employee> {
emp1, emp2
};
}
public App ()
{
GetAllEmployees ();
MainPage = new NavigationPage (new EmployeeListPage ());
}
所以我实际上并没有自己做,但是从阅读文档中我有一个建议,您可能值得尝试。
ItemsSource="{x:Static local:App.myListOfEmployeeObjects}"
在您的xaml中,您已经说过源是静态的,但是在您的.cs文件中却不是。请尝试以下操作:
public static List<Employee> myListOfEmployeeObjects { private set; get; }
然后尝试使用静态函数设置对象,例如:
static App() {
myListOfEmployeeObjects = something;
}
然后,该列表应在页面上可见。
我使用了以下链接,这些链接可能对您有用:
希望能有所帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句