2017-02-25 5 views
1

私はXamarinプロジェクトの仕組みを理解するためにXamarinプロジェクトに取り組んでいますが、うまくいかない問題がありました。ListView.ItemTemplateが設定されておらず、イベントが起動しない

XAMLのlistviewタグ内にlistview itemplateが格納されています。これはラベルを定義しており、そのラベルのテキストはデータソースのバインディングセットです。バインドではなくtostringをオーバーライドして、問題を引き起こします。私のitemtappedイベントハンドラも動作しておらず、発砲していないようです。私はMac用のVSを使用しています。

ここに私のXAMLは、罰金構築するようだと、私のitemsourceが正常に動作するようですと私は、愚かさと何かが欠けていないよフォーム

<?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です。私のコードをちょうどコメントの背後に見る必要があるなら、私は提供するために編集します。

EDIT:違いがあるかどうかはわかりませんが、このページはタブページにあります。

EDIT 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:

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übeAdded :)この問題を修正した結果、修正可能なイベントハンドラを削除しました。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 –

関連する問題