2017-02-25 124 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 –