xamarin 表单列表视图绘制单元格,但它们是空的
xamarin forms list view draw cells but they are empty
The listview shows the correct number of ideas in the database, but the label won't show idea names like the wfa does.
我的数据模板中的标签绑定到想法名称变量。出于某种原因,它似乎没有在视单元的标签中写下这些想法的名称。
我知道这不是我的 sql 代码的问题,因为它在 wfa 中工作得很好。
我用来找到问题解决方案的步骤:
1.研究了listview,它是如何工作的以及如何使用它
2.我花了4个小时胡思乱想尝试多种方式绑定标签
3. 即使在列表视图之外,标签也不会绑定到我的变量。
4. 我在 google 上搜索了一个解决方案,为我能想到的每个搜索词搜索了 2 页。
这是想法列表所依赖的代码:
创意列表视图
<ListView x:Name="Ideas" SeparatorColor="Accent" SeparatorVisibility="Default" ItemsSource="{Binding idea}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame BackgroundColor="Bisque" Padding="2" Margin="0,1,0,1">
<Label Text="{Binding idea.idea}" VerticalTextAlignment="Center" FontSize="Small" HorizontalOptions="FillAndExpand"></Label>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
主页class(简化为仅显示列表视图使用的代码)
Class Mainpage {
public static bool ShowHiddenIdeas = false;
ListView ideaso;
public static List<Idea> ideas;
public MainPage() {
GetIdeas(0); //get all ideas from the first table in the database
InitializeComponent();
}
void GetIdeas(int id) {
ideas = Kaiosql.GetIdeaSQL(id); //reads from the database and compiles a list of ideas in the table
/*
tried using a list of strings instead of the idea class
List<string> ideasz = new List<string>();
foreach (Idea i in ideas)
{
ideasz.Add(i.idea);
}
It just does the same thing
*/
ideaso.ItemsSource = ideas;
}
}
想法class
public class Idea
{
public int ID;
public string idea;
public string Description;
public string Type;
public string Time;
public bool HasFolder;
public string FolderPath;
public bool IsStarted;
public bool IsComplete;
public bool IsUploaded;
public Idea(int iD, string idea, string description, string type, string time, bool hasFolder = false, string folderPath = null, bool isStarted = false, bool isComplete = false, bool isUploaded = false)
{
ID = iD;
this.idea = idea;
Description = description;
Type = type;
Time = time;
HasFolder = hasFolder;
FolderPath = folderPath;
IsStarted = isStarted;
IsComplete = isComplete;
IsUploaded = isUploaded;
}
}
class 的信息比我的创意库目前可以输出的要多。我希望这不是数据未显示的原因。
非常感谢! - 扎克界王
你的想法太多了。您的 xaml 应该是这样的
编辑
<ListView x:Name="Ideas" SeparatorColor="Accent" SeparatorVisibility="Default" ItemsSource="{Binding idea}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame BackgroundColor="Bisque" Padding="2" Margin="0,1,0,1">
<Label Text="{Binding idea}" VerticalTextAlignment="Center" FontSize="Small" HorizontalOptions="FillAndExpand"></Label>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
为了工作,您需要做一些更改。
第一个,非常重要。您的 Idea
class 需要修改为使用 Properties
而不是 public fields
为此您需要添加 getters 和 setters 像这样:
public class Idea
{
public int ID {get; set;}
public string idea {get; set;}
public string Description {get; set;}
public string Type {get; set;}
public string Time {get; set;}
public bool HasFolder {get; set;}
public string FolderPath {get; set;}
public bool IsStarted {get; set;}
public bool IsComplete {get; set;}
public bool IsUploaded {get; set;}
public Idea(int iD, string idea, string description, string type, string time, bool hasFolder = false, string folderPath = null, bool isStarted = false, bool isComplete = false, bool isUploaded = false)
{
ID = iD;
this.idea = idea;
Description = description;
Type = type;
Time = time;
HasFolder = hasFolder;
FolderPath = folderPath;
IsStarted = isStarted;
IsComplete = isComplete;
IsUploaded = isUploaded;
}
}
如果不进行此更改,您将无法看到列表中的文本,因为绑定需要它。
其次:这可能是一个打字错误,因为它肯定不会 运行 这样。
变化:
ideaso.ItemsSource = ideas;
对于
Ideas.ItemsSource = ideas;
作为想法,x:Name 分配了 XAML 中的 ListView。
第三:改变Constructor中的调用顺序
public MainPage()
{
InitializeComponent();
GetIdeas(0); //get all ideas from the first table in the database
}
GetIdeas
方法将尝试访问 ListView,并且在调用 InitializeComponent
方法时实例化此视图。这就是为什么在尝试访问 XAML.
上的任何 UIElement 之前强制调用此方法的原因
四个:
XAML 应该是这样的:
<ListView x:Name="Ideas" SeparatorColor="Accent" SeparatorVisibility="Default">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame BackgroundColor="Bisque" Padding="2" Margin="0,1,0,1">
<Label Text="{Binding idea}" VerticalTextAlignment="Center" FontSize="Small" HorizontalOptions="FillAndExpand"></Label>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
删除文本绑定上的额外 "ideas" 并删除 ItemSource,因为您已经在后面的代码中设置了此 属性。
进行这些更改后,您应该会看到带有 ideas
的列表。
您还可以进行其他更改以使您的代码更像 MVVM,您可以阅读更多相关内容 here
希望这对您有所帮助。-
The listview shows the correct number of ideas in the database, but the label won't show idea names like the wfa does.
我的数据模板中的标签绑定到想法名称变量。出于某种原因,它似乎没有在视单元的标签中写下这些想法的名称。
我知道这不是我的 sql 代码的问题,因为它在 wfa 中工作得很好。
我用来找到问题解决方案的步骤: 1.研究了listview,它是如何工作的以及如何使用它 2.我花了4个小时胡思乱想尝试多种方式绑定标签 3. 即使在列表视图之外,标签也不会绑定到我的变量。 4. 我在 google 上搜索了一个解决方案,为我能想到的每个搜索词搜索了 2 页。
这是想法列表所依赖的代码:
创意列表视图
<ListView x:Name="Ideas" SeparatorColor="Accent" SeparatorVisibility="Default" ItemsSource="{Binding idea}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame BackgroundColor="Bisque" Padding="2" Margin="0,1,0,1">
<Label Text="{Binding idea.idea}" VerticalTextAlignment="Center" FontSize="Small" HorizontalOptions="FillAndExpand"></Label>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
主页class(简化为仅显示列表视图使用的代码)
Class Mainpage {
public static bool ShowHiddenIdeas = false;
ListView ideaso;
public static List<Idea> ideas;
public MainPage() {
GetIdeas(0); //get all ideas from the first table in the database
InitializeComponent();
}
void GetIdeas(int id) {
ideas = Kaiosql.GetIdeaSQL(id); //reads from the database and compiles a list of ideas in the table
/*
tried using a list of strings instead of the idea class
List<string> ideasz = new List<string>();
foreach (Idea i in ideas)
{
ideasz.Add(i.idea);
}
It just does the same thing
*/
ideaso.ItemsSource = ideas;
}
}
想法class
public class Idea
{
public int ID;
public string idea;
public string Description;
public string Type;
public string Time;
public bool HasFolder;
public string FolderPath;
public bool IsStarted;
public bool IsComplete;
public bool IsUploaded;
public Idea(int iD, string idea, string description, string type, string time, bool hasFolder = false, string folderPath = null, bool isStarted = false, bool isComplete = false, bool isUploaded = false)
{
ID = iD;
this.idea = idea;
Description = description;
Type = type;
Time = time;
HasFolder = hasFolder;
FolderPath = folderPath;
IsStarted = isStarted;
IsComplete = isComplete;
IsUploaded = isUploaded;
}
}
class 的信息比我的创意库目前可以输出的要多。我希望这不是数据未显示的原因。
非常感谢! - 扎克界王
你的想法太多了。您的 xaml 应该是这样的
编辑
<ListView x:Name="Ideas" SeparatorColor="Accent" SeparatorVisibility="Default" ItemsSource="{Binding idea}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame BackgroundColor="Bisque" Padding="2" Margin="0,1,0,1">
<Label Text="{Binding idea}" VerticalTextAlignment="Center" FontSize="Small" HorizontalOptions="FillAndExpand"></Label>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
为了工作,您需要做一些更改。
第一个,非常重要。您的 Idea
class 需要修改为使用 Properties
而不是 public fields
为此您需要添加 getters 和 setters 像这样:
public class Idea
{
public int ID {get; set;}
public string idea {get; set;}
public string Description {get; set;}
public string Type {get; set;}
public string Time {get; set;}
public bool HasFolder {get; set;}
public string FolderPath {get; set;}
public bool IsStarted {get; set;}
public bool IsComplete {get; set;}
public bool IsUploaded {get; set;}
public Idea(int iD, string idea, string description, string type, string time, bool hasFolder = false, string folderPath = null, bool isStarted = false, bool isComplete = false, bool isUploaded = false)
{
ID = iD;
this.idea = idea;
Description = description;
Type = type;
Time = time;
HasFolder = hasFolder;
FolderPath = folderPath;
IsStarted = isStarted;
IsComplete = isComplete;
IsUploaded = isUploaded;
}
}
如果不进行此更改,您将无法看到列表中的文本,因为绑定需要它。
其次:这可能是一个打字错误,因为它肯定不会 运行 这样。
变化:
ideaso.ItemsSource = ideas;
对于
Ideas.ItemsSource = ideas;
作为想法,x:Name 分配了 XAML 中的 ListView。
第三:改变Constructor中的调用顺序
public MainPage()
{
InitializeComponent();
GetIdeas(0); //get all ideas from the first table in the database
}
GetIdeas
方法将尝试访问 ListView,并且在调用 InitializeComponent
方法时实例化此视图。这就是为什么在尝试访问 XAML.
四个:
XAML 应该是这样的:
<ListView x:Name="Ideas" SeparatorColor="Accent" SeparatorVisibility="Default">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame BackgroundColor="Bisque" Padding="2" Margin="0,1,0,1">
<Label Text="{Binding idea}" VerticalTextAlignment="Center" FontSize="Small" HorizontalOptions="FillAndExpand"></Label>
</Frame>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
删除文本绑定上的额外 "ideas" 并删除 ItemSource,因为您已经在后面的代码中设置了此 属性。
进行这些更改后,您应该会看到带有 ideas
的列表。
您还可以进行其他更改以使您的代码更像 MVVM,您可以阅读更多相关内容 here
希望这对您有所帮助。-