使用 webservice 在 OnAppearing 上刷新列表视图
Listview refreshing on OnAppearing with webservice
我在 2 次更新我的在线数据库连接列表视图页面时遇到了一些问题
- 将项目添加到列表并使用 PopAsync 返回到它
- 使用 DatePicker 更新日期并显示对应的数据
这个日期
我使用 ViewModel 从我的在线数据库中检索数据,填充绑定到视图的 observablecollection。当我使用 PushAsync 打开页面时,它加载并显示良好。
如果我执行 MainPage -> View -> PushAsync 添加项目页面 -> PopToRootAsync(主页) -> PushAsync View 它也显示正常。
但是我需要在使用 PopAsync 添加项目后立即继续我的视图。
但是当我尝试更新它时,即使使用 OnAppearing,它也不起作用。当我调用 ViewModel 时,触发了 OnAppearing 但列表视图没有更新。
我认为问题是 ClientOnGetSuccesCompleted 后列表视图未更新,当我使用 PushAsync 打开视图时,列表视图在填充 ObservableCollection 时更新。
使用 OnApprearing 它也填充 ObservableCollection 但没有显示更新。
尝试过 MessagingCenter 但无法使其正常工作..
谢谢
视图模型:
public SuccesViewModel()
{
FillSuccess();
}
public void FillSuccess()
{
SuccesList = new ObservableCollection<Succes>();
var date = App.Date;
BasicHttpBinding binding = CreateBasicHttp();
this.client1 = new BienEtreServiceClient(binding, EndPoint);
this.instance = ((IBienEtreService)client1.InnerChannel);
client1.GetSuccesCompleted += ClientOnGetSuccesCompleted;
client1.GetSuccesAsync(App.UserID, date);
}
private void ClientOnGetSuccesCompleted(object sender, GetSuccesCompletedEventArgs e)
{
SuccesList.Clear();
foreach (Succes item in e.Result)
{
if (item.Date.ToString("yyyy-MM-dd") == App.Date.ToString("yyyy-MM-dd"))
{
SuccesList.Add(item);
}
}
}
XAML:
<ListView x:Name="lv_Succes" ItemsSource="{Binding SuccesList}" HasUnevenRows="True" ItemTapped="Tapped_Succes">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical"
Margin="20,8"
Padding="5"
BackgroundColor="#fcf3a8"
MinimumHeightRequest="40"
Opacity="0.7">
<Label x:Name="succes_txt"
Text="{Binding Text}"
FontAttributes="Bold"
TextColor="Black"
FontSize="Medium"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
视图背后的代码:
SuccesViewModel SuccesViewModel = new SuccesViewModel();
public Online_Succes()
{
InitializeComponent();
BindingContext = new SuccesViewModel();
}
protected override void OnAppearing()
{
SuccesViewModel.FillSuccess();
base.OnAppearing();
}
执行了OnAppearing中的方法,ObservableCollection填充的很好,但是没有刷新ListView。
我注意到您的视图模型可能存在一个故障点:您在每次更新调用时都会为 SuccesList
创建一个新实例。绑定是在您设置视图的绑定上下文时给出的第一个实例进行的,那些在每次调用 FillSuccess
时创建的新实例未绑定。
因此将您的视图模型更改为:
public SuccesViewModel()
{
// Initializing viewModel
SuccesList = new ObservableCollection<Succes>();
BasicHttpBinding binding = CreateBasicHttp();
this.client1 = new BienEtreServiceClient(binding, EndPoint);
this.instance = ((IBienEtreService)client1.InnerChannel);
client1.GetSuccesCompleted += ClientOnGetSuccesCompleted;
// Updating data
FillSuccess();
}
public void FillSuccess()
{
client1.GetSuccesAsync(App.UserID, App.Date);
}
private void ClientOnGetSuccesCompleted(object sender, GetSuccesCompletedEventArgs e)
{
SuccesList.Clear();
foreach (Succes item in e.Result)
if (item.Date.ToString("yyyy-MM-dd") == App.Date.ToString("yyyy-MM-dd"))
SuccesList.Add(item);
}
这应该适合你。
希望对您有所帮助。
我在 2 次更新我的在线数据库连接列表视图页面时遇到了一些问题
- 将项目添加到列表并使用 PopAsync 返回到它
- 使用 DatePicker 更新日期并显示对应的数据 这个日期
我使用 ViewModel 从我的在线数据库中检索数据,填充绑定到视图的 observablecollection。当我使用 PushAsync 打开页面时,它加载并显示良好。
如果我执行 MainPage -> View -> PushAsync 添加项目页面 -> PopToRootAsync(主页) -> PushAsync View 它也显示正常。 但是我需要在使用 PopAsync 添加项目后立即继续我的视图。
但是当我尝试更新它时,即使使用 OnAppearing,它也不起作用。当我调用 ViewModel 时,触发了 OnAppearing 但列表视图没有更新。
我认为问题是 ClientOnGetSuccesCompleted 后列表视图未更新,当我使用 PushAsync 打开视图时,列表视图在填充 ObservableCollection 时更新。 使用 OnApprearing 它也填充 ObservableCollection 但没有显示更新。
尝试过 MessagingCenter 但无法使其正常工作..
谢谢
视图模型:
public SuccesViewModel()
{
FillSuccess();
}
public void FillSuccess()
{
SuccesList = new ObservableCollection<Succes>();
var date = App.Date;
BasicHttpBinding binding = CreateBasicHttp();
this.client1 = new BienEtreServiceClient(binding, EndPoint);
this.instance = ((IBienEtreService)client1.InnerChannel);
client1.GetSuccesCompleted += ClientOnGetSuccesCompleted;
client1.GetSuccesAsync(App.UserID, date);
}
private void ClientOnGetSuccesCompleted(object sender, GetSuccesCompletedEventArgs e)
{
SuccesList.Clear();
foreach (Succes item in e.Result)
{
if (item.Date.ToString("yyyy-MM-dd") == App.Date.ToString("yyyy-MM-dd"))
{
SuccesList.Add(item);
}
}
}
XAML:
<ListView x:Name="lv_Succes" ItemsSource="{Binding SuccesList}" HasUnevenRows="True" ItemTapped="Tapped_Succes">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical"
Margin="20,8"
Padding="5"
BackgroundColor="#fcf3a8"
MinimumHeightRequest="40"
Opacity="0.7">
<Label x:Name="succes_txt"
Text="{Binding Text}"
FontAttributes="Bold"
TextColor="Black"
FontSize="Medium"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
视图背后的代码:
SuccesViewModel SuccesViewModel = new SuccesViewModel();
public Online_Succes()
{
InitializeComponent();
BindingContext = new SuccesViewModel();
}
protected override void OnAppearing()
{
SuccesViewModel.FillSuccess();
base.OnAppearing();
}
执行了OnAppearing中的方法,ObservableCollection填充的很好,但是没有刷新ListView。
我注意到您的视图模型可能存在一个故障点:您在每次更新调用时都会为 SuccesList
创建一个新实例。绑定是在您设置视图的绑定上下文时给出的第一个实例进行的,那些在每次调用 FillSuccess
时创建的新实例未绑定。
因此将您的视图模型更改为:
public SuccesViewModel()
{
// Initializing viewModel
SuccesList = new ObservableCollection<Succes>();
BasicHttpBinding binding = CreateBasicHttp();
this.client1 = new BienEtreServiceClient(binding, EndPoint);
this.instance = ((IBienEtreService)client1.InnerChannel);
client1.GetSuccesCompleted += ClientOnGetSuccesCompleted;
// Updating data
FillSuccess();
}
public void FillSuccess()
{
client1.GetSuccesAsync(App.UserID, App.Date);
}
private void ClientOnGetSuccesCompleted(object sender, GetSuccesCompletedEventArgs e)
{
SuccesList.Clear();
foreach (Succes item in e.Result)
if (item.Date.ToString("yyyy-MM-dd") == App.Date.ToString("yyyy-MM-dd"))
SuccesList.Add(item);
}
这应该适合你。
希望对您有所帮助。