2017-02-25 93 views
1

我正在研究一个Xamarin项目,以了解它是如何工作的,但是我遇到了一个我似乎无法解决的问题。ListView.ItemTemplate没有被设置和事件没有被触发

我有一个列表视图itemplate存储在我的XAML中的listview标签内,它定义了一个标签,并且该标签的文本是从数据源绑定的集合,但是当我的项目通过itemsource加载时,重写tostring而不是我的绑定,这会导致问题。我的itemtapped事件处理程序也没有工作,似乎并没有解雇。我正在使用VS for mac。

这里是我的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> 

希望我不只是愚蠢和缺少的东西,因为我的XAML似乎建立罚款和我的ItemSource似乎很好地工作。如果你需要看我的代码只是评论,我会编辑提供。

编辑:不确定它是否有任何区别,但此页面位于Tab键页面。

编辑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)); 
    } 
} 

编辑3:

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; 
     } 
    } 
} 
+0

请显示ViewModel以及如何设置您的BindingContext。 –

+0

@ Sven-MichaelStübe新增:)注意我删除了事件处理程序,因为我认为可以修复此问题的结果:P – Jaxi

+0

和'NoteItem'? –

回答

2

如果你要绑定的东西,它必须是一个属性。 title是无财产。

public class NoteItem 
{ 
    public int id {get;} 
    public string title {get;} 
    public string content {get;} 
    public DateTime createdat {get;} 
    public DateTime updatedat {get;} 
} 

注意

通常在C#中,属性开始一个大写字母。

public class NoteItem 
{ 
    public int Id {get;} 
    public string Title {get;} 
    public string Content {get;} 
    public DateTime CreateDate {get;} 
    public DateTime UpdateDate {get;} 
} 

但是不要忘记更新你的绑定。

<Label Text="{Binding Title}" FontSize="14" /> 
+0

不幸的是,这似乎并未解决问题。我删除了我的ToString(),以确定它正在使用什么,现在文本是“Brainstorageapp.NoteItem” – Jaxi

+0

找到它为什么不起作用,但如果你可以,我想要一些关于它的信息。在我的列表视图中,XAML删除了“IsGroupingEnabled =”true“”,并修复了它。为什么会发生这种情况? – Jaxi

+0

是的,如果你使用分组,你的ItemsSource必须是'ObservableCollection >'或者换句话说:列表项目列表。这里是一个分组的例子:https://github.com/xamarin/xamarin-forms-samples/tree/master/LabelledSectionsList –