[英]ListView.ItemTemplate not being set and events not firing
I'm working on a Xamarin project in order to learn how it works, but i've ran into a problem I can't seem to work out. 我正在研究Xamarin项目,以了解其工作原理,但是遇到了一个我似乎无法解决的问题。
I have a listview itemplate stored inside the listview tag in my XAML, which defines a label and the text for that label is a binding set from the data source, although when my items are loaded in through the itemsource it's using my overridden tostring rather than my binding, which causes a problem. 我在XAML的listview标记中存储了一个listview项板,它定义了一个标签,并且该标签的文本是来自数据源的绑定集,尽管当我的项目通过itemsource加载时,它使用的是覆盖的tostring而不是我的绑定,这会导致问题。 My itemtapped event handlers are also not working and don't seem to be firing.
我的itemtapped事件处理程序也无法正常工作,似乎也没有触发。 I am using VS for mac.
我在Mac上使用VS。
Here's my XAML for the form 这是我的表格的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"
x:Class="BrainStorageApp.MainPage"
Title="View Notes">
<ListView ItemsSource="{Binding Items}"
x:Name="MainListView"
HasUnevenRows="true"
IsGroupingEnabled="true"
IsPullToRefreshEnabled="true"
IsEnabled="true"
CachingStrategy="RecycleElement"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
RefreshCommand="{Binding RefreshDataCommand}">
<ListView.Header>
<StackLayout Padding="40"
Orientation="Horizontal"
HorizontalOptions="FillAndExpand"
BackgroundColor="{StaticResource Primary}}">
<Label Text="Your Notes"
HorizontalTextAlignment="Center"
HorizontalOptions="FillAndExpand"
TextColor="White"
FontAttributes="Bold"/>
</StackLayout>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding title}" FontSize="14" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
Hope i'm not just being stupid and missing something, as my XAML seems to build fine and my itemsource seems to work fine. 希望我不仅因为自己的XAML可以正常运行而且我的itemsource可以正常运行而变得愚蠢和缺少任何东西。 If you need to see my code behind just comment and i'll edit to provide.
如果您需要在注释后面查看我的代码,我将进行编辑以提供。
EDIT: Unsure whether it makes any difference, but this page is located in a tabbing page. 编辑:不确定是否有任何区别,但是此页面位于标签页中。
EDIT 2: Here's the code for my main xaml.cs file, as requested. 编辑2:这是我的主要xaml.cs文件的代码,根据要求。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace BrainStorageApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MainPage : ContentPage
{
private BrainstorageApiClass BrainstorageClass;
private UserClass User;
public MainPage(string username)
{
InitializeComponent();
BrainstorageClass = new BrainstorageApiClass();
User = new UserClass();
User.Username = username;
BindingContext = new ListViewPageViewModel(BrainstorageClass.LoadNotes(User.Username));
}
void Handle_ItemTapped(object sender, Xamarin.Forms.ItemTappedEventArgs e)
{
Console.WriteLine(sender.ToString());
}
}
class ListViewPageViewModel : INotifyPropertyChanged
{
public ObservableCollection<NoteItem> Items { get; }
public ListViewPageViewModel(List<NoteItem> list)
{
Items = new ObservableCollection<NoteItem>(list);
RefreshDataCommand = new Command(
async () => await RefreshData());
}
public ICommand RefreshDataCommand { get; }
async Task RefreshData()
{
IsBusy = true;
//Load Data Here
await Task.Delay(2000);
IsBusy = false;
}
bool busy;
public bool IsBusy
{
get { return busy; }
set
{
busy = value;
OnPropertyChanged();
((Command)RefreshDataCommand).ChangeCanExecute();
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName]string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
EDIT 3: 编辑3:
NoteItem NoteItem
namespace BrainStorageApp
{
public class NoteItem
{
public int id;
public string title;
public string content;
public DateTime createdat;
public DateTime updatedat;
public NoteItem(JToken Token)
{
id = int.Parse(Token["id"].Value<string>());
title = Token["Note_Title"].Value<string>();
content = Token["Note_Content"].Value<string>();
createdat = DateTime.Parse(Token["created_at"].Value<string>());
updatedat = DateTime.Parse(Token["updated_at"].Value<string>());
}
public override string ToString()
{
return title;
}
}
}
If you want to bind something, it has to be a property. 如果要绑定某些东西,它必须是一个属性。
title
is no property. title
是没有财产。
public class NoteItem
{
public int id {get;}
public string title {get;}
public string content {get;}
public DateTime createdat {get;}
public DateTime updatedat {get;}
}
Note 注意
Usually in C#, Properties start with an upper case letter. 通常在C#中,属性以大写字母开头。
public class NoteItem
{
public int Id {get;}
public string Title {get;}
public string Content {get;}
public DateTime CreateDate {get;}
public DateTime UpdateDate {get;}
}
But don't forget to update your binding then. 但是不要忘了更新绑定。
<Label Text="{Binding Title}" FontSize="14" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.