Wcf 在Web服务出现时刷新Listview

Wcf 在Web服务出现时刷新Listview,wcf,listview,xamarin.forms,observablecollection,Wcf,Listview,Xamarin.forms,Observablecollection,在2种情况下,我更新联机db连接的listview页面时遇到一些问题 向列表中添加项目并使用PopAsync返回 使用日期选择器更新日期并显示对应于 这个日期 我使用ViewModel从在线数据库检索数据,填充绑定到视图的observablecollection。当我用PushAsync打开页面时,它会加载并显示良好 如果我使用MainPage->View->PushAsync添加项目页面->PopToRootAsync(主页)->PushAsync视图,它也会显示良好。 但我需要在使用po

在2种情况下,我更新联机db连接的listview页面时遇到一些问题

  • 向列表中添加项目并使用PopAsync返回
  • 使用日期选择器更新日期并显示对应于 这个日期
我使用ViewModel从在线数据库检索数据,填充绑定到视图的observablecollection。当我用PushAsync打开页面时,它会加载并显示良好

如果我使用MainPage->View->PushAsync添加项目页面->PopToRootAsync(主页)->PushAsync视图,它也会显示良好。 但我需要在使用popsync添加项之后立即继续查看

但是,当我试图让它更新,即使在出现它不工作。当我调用ViewModel时,OnAppearing被触发,但是listview没有更新

我认为问题在于ClientEnteTAccessCompleted后listview没有更新,当我用PushAsync打开视图时,listview会在ObservableCollection被填充时更新。 通过OnAppearing,它还填充了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);
        }

    }
}
<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。

我注意到视图模型上可能存在的一个故障点:每次更新调用时,您都在创建一个新实例,以
成功列表。绑定是在您设置视图的绑定上下文时给出的第一个实例上进行的,在每次调用
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);
}
公共成功视图模型()
{
//初始化视图模型
成功列表=新的ObservableCollection();
BasicHttpBinding=CreateBasicHttp();
this.client1=新的BienetReserveClient(绑定,端点);
this.instance=((IBienEtreService)client1.InnerChannel);
client1.getsuccesscompleted+=clientngetsccesscompleted;
//更新数据
FillSuccess();
}
公营机构成功
{
client1.GetSuccesAsync(App.UserID,App.Date);
}
私有void客户端已成功完成(对象发送方,GetSuccessCompletedEventArgs e)
{
successlist.Clear();
foreach(e.Result中的成功项)
if(item.Date.ToString(“yyyy-MM-dd”)==App.Date.ToString(“yyyy-MM-dd”))
成功列表。添加(项目);
}
这应该对你有用


希望有帮助。

很好!如图所示,我更改了viewmodel,添加了public SuccesViewModel SuccesViewModel{get;set;}=new SuccesViewModel();在代码隐藏中,单击successViewModel.FillSuccess();在OnAppearing和datapicker selected事件中。现在更新很好,非常感谢!我很乐意帮忙。您没有共享添加新项目的代码,但我打赌应该也是这样。无论如何,如果您还没有完成,我建议您看看。实际上OnAppearing还有另一个问题,列表在第一次启动时填充两次,因为FillSuccess在init+OnAppearings调用了两次,所以不要在ViewModel的构造函数中调用它。我想知道这是否是最好的方法,但它现在可以解决了。好吧,听起来不错,因为构造函数在第一次启动时被调用,然后出现,异步方法被执行了两次=奇怪的行为。不管怎么说,现在一切都很好,会成功的。再次感谢;)