Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

WCF REST Service Data Binding to ListView Template for TabbedPage Async Call

$
0
0

Hi all,

I am working on **sample application (created a login page and navigate to listing page) by consuming data through WCF REST Service.

** I have tried to call the Async functions in constructor and its working fine with Android and iOS platform by simultaneously calling the two view models, but failed for Windows Phone platform**. So i have called the async functions in OnAppearing event of view page and it binds only one model class. Kindly refer the below codes and suggest to bind and display the data to List view control.

**Note: By calling in OnAppearing function Android,iOS & Windows Phone platform not binded the data to list view control. **

The events are fired and the values retrieved to the List_SerachRequest , but the items are not displayed to Listview control. I feel have missed the binding refresh after the data retrieved to List Template from WCF Service or missing an event. Kindly guide me to resolve.

Please find below my steps and codes for reference.

Tabbed Page: // There are two tabs and this will load the listview data (SearchRequestFSearchViewPage ) based on another tab filter values(SearchRequestFilterViewPage).

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            x:Class="Mobile.Views.SearchRequestTabViewPage"            
            xmlns:local="clr-namespace:Mobile.Views;assembly=Mobile" Title="Search Request">
  <TabbedPage.Children>
    <local:SearchRequestFilterViewPage />
    <local:SearchRequestSearchViewPage />
  </TabbedPage.Children>
</TabbedPage>

SearchRequestSearchViewPage : // Search List view binding template for reference
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                            xmlns:local="clr-namespace:Mobile.ViewModels;assembly=Mobile"
                       x:Class="Mobile.Views.SearchRequestSearchViewPage" Title="Search">
  <ContentPage.BindingContext>
    <local:SearchRequestSearchViewModel/>
  </ContentPage.BindingContext>
  <ListView x:Name="SearchRequestListview"
            ItemTapped="OnSearchListviewSource_Tapped"
            ItemsSource="{Binding List_SearchRequestData}">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <ViewCell.View>
            <StackLayout Orientation="Vertical">
              <Label Text="{Binding ID}" />
              <StackLayout Orientation="Horizontal" Spacing="5"  >
                <Label Text="{Binding DateTime}" />
              </StackLayout>
            </StackLayout>
          </ViewCell.View>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</ContentPage>

SearchRequestFilterViewPage OnAppearing Function:

protected async override void OnAppearing()
        {
            try
            {
                base.OnAppearing();
                var _SearchRequestFilterViewModel = (BindingContext as SearchRequestFilterViewModel);
                if (_SearchRequestFilterViewModel != null)
                     await _SearchRequestFilterViewModel.LoadData(); // Here the data binded to all filter page controls like 'picker & Search Box'
                 _searchrequestsearchviewpage = new SearchRequestSearchViewPage(); // Here we called SearchRequestview page  for loading data based on Above called data loading completion 
                 _searchrequestsearchviewpage.LoadDataAfterFilterPageLoad(); // When calling this function data retrieved from service 
            }
            catch (Exception ex)
            {
                throw ex;   
            }
        }

SearchRequestSearchView Page:

public async void  LoadDataAfterFilterPageLoad()
        {
            try
            {
                var _SearchRequestSearchViewModel = (BindingContext as SearchRequestSearchViewModel);
                if (_SearchRequestSearchViewModel != null)
                {
                    await _SearchRequestSearchViewModel.LoadListview();
                }
            }
            catch (Exception ex) { }
        }
        protected async override void OnAppearing()
        {
            try
            {
                base.OnAppearing(); // we have tried to use the below code in OnAppearing but the function called before searchrequestfilterview page controls data loading. So the Listview control will not have values to bind. So we have commented the below code.
                //var _SearchRequestSearchViewModel = (BindingContext as SearchRequestSearchViewModel);  
                //// if (_SearchRequestSearchViewModel  != null ) 
                //await _SearchRequestSearchViewModel .LoadListview();
            }
            catch (Exception ex)
            {
            }
        }

SearchRequestSearchViewModel Class:
class SearchRequestSearchViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public List<HelpDesk> List_SearchRequest = new List<HelpDesk>();
        public List<HelpDesk> List_SearchRequestData
        {
            get { return List_SearchRequest; }
            set
            {
                if (List_SearchRequest != null)
                {
                    List_SearchRequest = value;
                    OnPropertyChanged("List_SearchRequestData"); //This event too fired and data assigned
                }
            }
        }

public async Task LoadListview()
        {
            try
            {             
                _dataaccess = new DataAccess();
                Parameters[0] = SelectedSiteFromSearchRequestFilterViewModel;
                Parameters[1] = SelectedStatusFromSearchRequestFilterViewModel;
                List_SearchRequest = await _dataaccess.GetSerachreqSerachListview(Parameters);
                if (List_SearchRequest.Count != -1)
                {
                    Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
                    {
                        List_SearchRequestData = List_SearchRequest; // Data retrieved from WCF REST Service and assigned to Listview item source. But in display           the values are not assinged to List view Control. 
                    });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

Viewing all articles
Browse latest Browse all 58056

Trending Articles