如何解决Xamarin表单中的可观察集合更新时,视图未更新
一旦从Pop Up添加了新项目,我正在尝试通过RG Pop插件更新我的视图。尽管我在添加新项目时可以看到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,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,
<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"
Text="{Binding Name}"
TextColor="{DynamicResource Gray-900}"
FontSize="14"
/>
<!-- Time -->
<Label Grid.Row="1"
Grid.Column="1"
Margin="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,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,10"
Padding="5"
Text=""
HorizontalOptions="End"
TextColor="{DynamicResource LightGreen}"
FontSize="20"
FontFamily="{StaticResource FontAwesomeSolid}">
</Label>
</Grid>
</Frame>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。