![](/img/trans.png)
[英]Xamarin Button Command (inside of ListView.ItemTemplate) Not Firing
[英]ListView.ItemTemplate not being set and events not firing
我正在研究Xamarin項目,以了解其工作原理,但是遇到了一個我似乎無法解決的問題。
我在XAML的listview標記中存儲了一個listview項板,它定義了一個標簽,並且該標簽的文本是來自數據源的綁定集,盡管當我的項目通過itemsource加載時,它使用的是覆蓋的tostring而不是我的綁定,這會導致問題。 我的itemtapped事件處理程序也無法正常工作,似乎也沒有觸發。 我在Mac上使用VS。
這是我的表格的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可以正常運行而變得愚蠢和缺少任何東西。 如果您需要在注釋后面查看我的代碼,我將進行編輯以提供。
編輯:不確定是否有任何區別,但是此頁面位於標簽頁中。
編輯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;
}
}
}
如果要綁定某些東西,它必須是一個屬性。 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" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.