We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to trigger viewmodel to call api from xaml.cs page

Thread ID:

Created:

Updated:

Platform:

Replies:

140591 Oct 26,2018 07:09 PM UTC Oct 31,2018 12:33 PM UTC Xamarin.Forms 5
loading
Tags: SfListView
Gourav Roy
Asked On October 26, 2018 07:09 PM UTC

Hello,

I want to refresh my listview. A list is loading by api call. And that is fine.

But i want to apply search functionality, the search parameters are available in xaml.cs page and api calling is taking place in viewmodel.

How can i trigger api call in viewmodel with those xaml.cs parameter?

(How to force to call api in viewmodel with the parameter of xaml.cs page)

Suggest please.

Regards,

Gourav


Jayaleshwari N [Syncfusion]
Replied On October 29, 2018 07:14 AM UTC

Hi Gourav, 
 
Thanks for Contacting Syncfusion Support. 
 
We have checked the reported query “Search parameters to trigger the filtering in ViewModel” from our side. You can achieve your requirement by EventToCommandbehaviour for searchbar to Filter by command which is similar to EventToCommandBehaviour implemented for SfListView in our UG.  
 
You can refer the below link for reference.  
 
We have attached the sample for your reference. You can download from the following location 
 
Code snippet XAML: Behavior  for searchbar  is implemented to filter items by command.   
<ContentPage>   
  <Grid x:Name="MainGrid" Margin="0" RowSpacing="0">   
     <SearchBar Placeholder="Search here to filter" >   
           <SearchBar.Behaviors>   
               <local:EventToCommandBehavior EventName="TextChanged"    
                                           Command="{Binding FilterCommand}"    
                                           CommandParameter="{x:Reference MainGrid}" />   
            </SearchBar.Behaviors>   
     </SearchBar>   
    <sync:SfListView x:Name="listView" ItemsSource="{Binding Items}"/>   
 </Grid>   
</ContentPage>                
 
   
Code snippet C#: ViewModel filters the text entered in command like below.   
public class BookInfoViewModel : INotifyPropertyChanged 
{ 
    private Command<object> filterCommand; 
    private string searchText; 
         
    #region Properties 
 
    public Command<object> FilterCommand 
    { 
        get { return filterCommand; } 
        protected set { filterCommand = value; } 
    } 
 
    #endregion 
 
    #region Constructor 
 
    public BookInfoViewModel() 
    { 
        filterCommand = new Command<object>(OnTextChanged);            
    } 
 
    #endregion 
 
    private void OnTextChanged(object obj) 
    { 
        var grid = (obj as Grid); 
        var searchBar = grid.Children[0] as SearchBar; 
        searchText = searchBar.Text; 
        var listview = grid.Children[1] as SfListView; 
        if (listview.DataSource != null) 
        { 
            listview.DataSource.Filter = FilterBooks; 
            listview.DataSource.RefreshFilter(); 
        }            
    } 
 
    private bool FilterBooks(object obj) 
    { 
        if (searchText == null) 
            return true; 
 
        var taskInfo = obj as BookInfo; 
        return (taskInfo.BookName.ToLower().Contains(searchText.ToLower()) 
                || taskInfo.BookDescription.ToLower().Contains(searchText.ToLower())); 
    } 
} 
 
   
Please let us know whether sample meets your requirement.     
 
Regards, 
Jayaleshwari N. 


Gourav Roy
Replied On October 30, 2018 07:28 AM UTC

Hey Jayaleshwari N,

Here in your provided code, you binded with xaml and viewmodel.But I want to trigger viewModel from xaml.cs page.Because all switch values( which are used as parameter) and other textbox values which will be used as parameter as well for api call are available at xaml.cs page.
In xaml.cs page I have an event named as Search(), And all those parameters are there.I want to trigger viewmodel with those parameters of xaml.cs page when I click on the search button(Or from the "Search()" event)

So would you please provide the sample that how to trigger viewmodel from xaml.cs event.

Regards,
Gourav 



Jayaleshwari N [Syncfusion]
Replied On October 31, 2018 11:19 AM UTC

Hi Gourav, 
 
Thanks for the explanation. 
 
We have checked the reported query “Trigger ViewModel  from xaml.cs” from our side. We have modified the sample by execute the Command from the SearchText changed event from xmal.cs page.  
                                                                                    
Code snippet XAML: TextChanged event wired in xaml page. 
                                                                                           
<SearchBar x:Name="filterText"                                            
                IsVisible="true" 
                TextChanged="OnfilterTextChanged" 
                Placeholder="Search here to filter"/> 
 
Code snippet C#: Call Filter command from xmal.cs page 
 
private void OnfilterTextChanged(object sender, TextChangedEventArgs e) 
{ 
    var grid = (sender as SearchBar).Parent as Grid; 
    (this.BindingContext as BookInfoViewModel).FilterCommand.Execute(grid); 
} 
 
You can pass the parameter or text to command call as your customization. We have attached the modified sample for your reference and you can download the same from the following location. 
 
                                
Please let us know if you would require further assistance. 
 
Regards, 
Jayaleshwari N. 


Gourav Roy
Replied On October 31, 2018 12:08 PM UTC

Hello Jayaleshwari N,

Could You please create an incident for me.I want to share my personal code.And I can't share it in public.
So, Would you be able to do that?

Regards,
Gourav

Jayaleshwari N [Syncfusion]
Replied On October 31, 2018 12:33 PM UTC

Hi Gourav, 

We would request you to contact sales team for assistance. 

Regards, 
Jayaleshwari N. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

or the page will be automatically redirected to sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;