我的数据模板中的标签绑定到提示名称变量。由于某种原因,这些想法的名称似乎没有写在视单元的标签中。
我知道这不是我的sql代码的问题,因为它在wfa中工作得很好。
我用来查找问题解决方案的步骤: 1.研究列表视图,它如何工作以及如何使用它 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>
Mainpage类(简化为仅显示列表视图使用的代码)
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;
}
}
创意课
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;
}
}
该类提供的信息比我的ideabase当前可输出的信息更多。我希望这不是数据不显示的原因。
非常感谢! -Zak Kaioken
答案 0 :(得分:0)
答案 1 :(得分:0)
为了工作,您需要做一些更改。
第一个也是非常重要的。为此,您的Idea
类需要修改为使用Properties
而不是公共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;
Being Ideas x:Name在XAML中分配了ListView。
第三:更改构造函数中调用的顺序。
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>
删除文本绑定上的多余“想法”,并删除 ItemSource ,因为您已经在后面的代码中设置了此属性。
进行这些更改后,您应该会看到包含ideas
的列表。
您还可以进行其他更改以使您的代码更具MVVM风格,您可以详细了解here
希望这会有所帮助。-