アイテムソースとデータをバインドしました。データは変更されますが、UIには変更が反映されません。リストビューのセルがデータバインディングを使用して更新されていません
TestUpdateCell
が呼び出されたときに気づきましたが、SetProperty(ref items, value)
も呼び出されません。しかし、私はTestUpdateCell
の終わりに、このステートメントを追加した場合:
var temp = Items;
Items = new List<Item>();
Items = temp;
SetProperty(ref items, value)
は呼び出しですが、UIは、変更を反映しません。 Itemクラス
public class Item : MvvmHelpers.ObservableObject
{
string name;
public string Name
{
get { return name; }
set { SetProperty(ref name, value); }
}
}
:
また、私は(ジェラルドVersluisによって提案されたソリューション)ソリューション
ソリューションが含まれるようにXamarinのマニュアルhttps://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_bindings_to_mvvm/
編集をリストの代わりにのObservableCollectionを使用し、使用してみました
問題:アイテムクラス:
public class Item : MvvmHelpers.ObservableObject
{
public string Name { get; set; }
}
項目データクラス:
public class ItemData : List<Item>
{
public ItemData()
{
Add(new Item
{
Name = "Item One"
});
Add(new Item
{
Name = "Item Two"
});
Add(new Item
{
Name = "Item Three"
});
}
}
ビューモデル:
public class ItemViewModel : MvvmHelpers.BaseViewModel
{
public ItemViewModel()
{
Items = new ItemData();
}
List<Item> items;
public List<Item> Items
{
get { return items; }
set { SetProperty(ref items, value); }
}
public void TestUpdateCell()
{
Items[0].Name = "Item One Updated";
}
}
商品ページCS:
public class ItemPage : ContentPage
{
ItemViewModel ivm;
public ItemPage()
{
BindingContext = ivm = new ItemViewModel();
}
void ItemTapped(object sender, ItemTappedEventArgs e)
{
ivm.TestUpdateCell();
}
}
itempage.XAML:
ListView
ItemsSource="{Binding Items}"
ItemTapped="ItemTapped" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<view:MyItemView/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView
MyItemView:私が正しく表示された場合
<ContentView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyProject;assembly=MyProject"
x:Class="MyProject.MyItemView">
<local:CardFrame
IsClippedToBounds="True"
HasShadow="True" >
<StackLayout
Spacing="0"
Orientation="Horizontal">
<Grid
RowSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<local:MyLabel
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="4"
FontSize="18"
FontAttributes="Bold"
Text="{Binding Name}"/>
<!-- omitted rows and columns -->
</Grid>
</StackLayout>
</local:CardFrame>
</ContentView>
リストの代わりにを使用してください。ObservableCollection –