listview - 当 Xamarin 表单中的可观察集合更新时,视图未更新
问题描述
一旦从 Pop Up 添加了新项目,我正在尝试从 RG Pop Plugin 更新我的视图。虽然我在添加新项目时可以看到 Observable Collection 的变化,但视图没有根据 Observable Collection 更新。
这是我的视图模型,
public class ReceiptViewModel : AddRecieptModel
{
//public ObservableCollection<AddRecieptModel> RecieptsData { get; set; }
private ObservableCollection<AddRecieptModel> _addRecieptModel;
public ObservableCollection<AddRecieptModel> RecieptsData
{
get { return _addRecieptModel; }
set
{
if (_addRecieptModel == value) return;
_addRecieptModel = value;
this.NotifyPropertyChanged();
}
}
public ReceiptViewModel()
{
Task.Run(async () => { await GetReciepts(); }).Wait();
this.AddRecieptCommand = new Command(this.AddRecieptClicked);
this.AddRecieptPopUpCommand = new Command(this.AddRecieptPopUpClicked);
this.SelectedRecieptCommand = new Command(this.SelectedRecieptClicked);
}
private async Task<GetRecieptResponse> GetReciepts()
{
JsonValue GetRecieptResponse = await HttpRequestHelper<GetRecieptRequest>.GetRequest(ServiceTypes.GetReciepts, SessionHelper.AccessToken);
GetRecieptResponse getRecieptResponse = JsonConvert.DeserializeObject<GetRecieptResponse>(GetRecieptResponse.ToString());
if (getRecieptResponse.IsSuccess)
{
List<AddRecieptModel> lstGetReciepts = getRecieptResponse.LstReciepts.Select(dc => new AddRecieptModel()
{
Name = dc.Name,
Price = dc.Price,
RecieptID = dc.RecieptID,
Status = dc.Status
}).ToList();
RecieptsData = new ObservableCollection<AddRecieptModel>();
foreach (AddRecieptModel getReciept in lstGetReciepts)
{
RecieptsData.Add(getReciept);
}
}
else
{
}
return getRecieptResponse;
}
private async void AddRecieptClicked(object obj)
{
Task.Run(async () => { await AddReciept(); }).Wait();
}
public async Task<AddRecieptResponse> AddReciept()
{
AddRecieptRequest addRecieptRequest = new AddRecieptRequest();
addRecieptRequest.AuthToken = SessionHelper.AccessToken;
RecieptDTO recieptDTO = new RecieptDTO();
recieptDTO.AddedOn = DateTime.Now.ToString();
recieptDTO.Name = Name;
recieptDTO.Price = Price;
recieptDTO.RecieptID = RecieptID;
recieptDTO.Status = ReceiptStatusEnum.New.ToString();
recieptDTO.StoreID = 0;
addRecieptRequest.recieptDTO = recieptDTO;
JsonValue AddRecieptResponse = await HttpRequestHelper<AddRecieptRequest>.POSTreq(ServiceTypes.AddReciept, addRecieptRequest);
AddRecieptResponse addRecieptResponse = JsonConvert.DeserializeObject<AddRecieptResponse>(AddRecieptResponse.ToString());
if (addRecieptResponse.IsSuccess)
{
List<AddRecieptModel> lstGetReciepts = addRecieptResponse.LstReciept.Select(dc => new AddRecieptModel()
{
Name = dc.Name,
Price = dc.Price,
RecieptID = dc.RecieptID,
Status = dc.Status
}).ToList();
RecieptsData = new ObservableCollection<AddRecieptModel>();
foreach (AddRecieptModel getReciept in lstGetReciepts)
{
RecieptsData.Add(getReciept);
}
}
return addRecieptResponse;
}
public void List1CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
{
// Your logic here
}
private async void SelectedRecieptClicked(object obj)
{
int recieptID = Convert.ToInt32(((RecieptDTO)obj).RecieptID);
var mdp = (Application.Current.MainPage as MasterDetailPage);
var navPage = mdp.Detail as NavigationPage;
if (navPage.Navigation.NavigationStack.Count == 0 ||
navPage.Navigation.NavigationStack.Last().GetType() != typeof(ProductList))
{
await navPage.PushAsync(new ProductList(recieptID), true);
}
}
private void AddRecieptPopUpClicked(object obj)
{
PopupNavigation.PushAsync(new CreateList());
}
}
这是我的列表视图,
<ListView x:Name="ListReceipt"
SelectionMode="Single"
HasUnevenRows="True"
ItemsSource="{Binding RecieptsData}">
<ListView.Behaviors>
<behaviors:ItemTappedBehavior Command="{Binding SelectedRecieptCommand}" />
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Spacing="0">
<Frame
HasShadow="True"
Padding="0"
BackgroundColor="White">
<Frame.Margin>
<OnPlatform x:TypeArguments="Thickness"
Android="0"
iOS="0"/>
</Frame.Margin>
<Grid RowSpacing="0" ColumnSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Margin="17,8" VerticalOptions="Center" Grid.Row="0" Grid.RowSpan="3"
Text=""
FontSize="75"
TextColor="Teal"
FontFamily="{StaticResource FontAwesomeSolid}" />
<!-- Document Name -->
<Label Grid.Column="1" Grid.ColumnSpan="5" FontFamily="{StaticResource Montserrat-SemiBold}"
Grid.Row="0"
Margin="0,13,0,0"
Text="{Binding Name}"
TextColor="{DynamicResource Gray-900}"
FontSize="14"
/>
<!-- Time -->
<Label Grid.Row="1"
Grid.Column="1"
Margin="0,0,0,0"
Padding="5"
Text="{Binding Status}"
TextColor="{DynamicResource Gray-700}"
FontSize="14"
FontFamily="{StaticResource Montserrat-Medium}"
/>
<!-- Document Size -->
<Label Grid.Row="1" Grid.ColumnSpan="5"
Grid.Column="2"
Margin="0,0,15,0"
Padding="5"
Text="{Binding CreatedOn}"
TextColor="{DynamicResource Secondary}"
FontSize="14"
FontFamily="{StaticResource Montserrat-Medium}"
/>
<Label Grid.Row="2"
Grid.Column="5"
Margin="0,0,0,10"
Padding="5"
Text=""
HorizontalOptions="End"
TextColor="{DynamicResource LightGreen}"
FontSize="20"
FontFamily="{StaticResource FontAwesomeSolid}">
</Label>
</Grid>
</Frame>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
提前致谢。
解决方案
推荐阅读
- angular - 在角度 4 中将 tinymce 编辑器内容设置为只读
- laravel - 如何在 Laravel 中绑定服务?
- python - django.contrib.gis.geos.error.GEOSException:无法解析版本信息字符串
- php - 当用户在 woocommerce 上单击添加到购物车时,我想添加一个弹出确认
- xml - XSL:FO 应用 html 定义规则生成 PDF 的问题
- javascript - vue路由器组件变量未定义
- go - 得到错误未定义:使用数学/兰德库时go lang中的数学
- python - NameError:名称“状态”未定义
- c# - MongoDB C# - 不调用 ISupportInitialize 方法
- python - 运行 PyInstaller (python 2.7) 创建的可执行文件时出错