How to get SearchBar text into viewmodel

SmartmanAppsSmartmanApps AUMember ✭✭✭
edited March 22 in Xamarin.Forms

Trying to get the text from a SearchBar into my viewmodel, and missing something, not sure what. Having read a lot of posts, I'm almost there, as intellisense generated the method for me automatically with obj as the parameter already there, but when I used it it was null, so something still missing somewhere. Here are the relevant lines of code (so if you don't see something, then assume that is what I'm missing and tell me :-) )...

Content Page

SearchBar LookupBar;

LookupBar=new SearchBar
{
    Placeholder="Enter search term"
};

vm=new Viewmodel();

LookupBar.SearchCommand  = vm.TestSearchCommand;

LookupBar.SearchCommandParameter=LookupBar.Text;

ViewModel

public ICommand TestSearchCommand { get; }
(in constructor - ) TestSearchCommand=new Command<string>(TestSearch);

private void TestSearch(string obj)
{
     System.Diagnostics.Debug.WriteLine(string.Format("Searchterm is {0}",obj));
}

I then type something into the search text box and press the search button, but obj coming up null. :-(

Best Answer

Answers

  • udaykumar.4017udaykumar.4017 USMember ✭✭
    edited March 21

    @SmartmanApps your viewmodel should be like the following:

    Xaml Ui:

    Viewmodel:

    public class MainPageViewModel
    {
    public string CityName { get; set; }
    public ICommand getSearchText { get; set; }
    public MainPageViewModel()
    {
    getSearchText = new Command (Gettext);
    }

          public void Gettext(object cityname)
          {
    
          }
    }
    

    Note:
    you should pass object through the command.. should not pass string in this searchcommand.

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    @ColeX said:
    Look at this line

    LookupBar.SearchCommandParameter=LookupBar.Text;
    

    You should create binding for SearchCommandParameter with its text to trigger the changes , not simply assign the text value to it, it just get the initial value , so it's always null.

    Modify as

    LookupBar.SetBinding(SearchBar.SearchCommandParameterProperty, new Binding("Text", source: LookupBar));
    

    Thanks. I did wonder about that (one blog said this method saved binding the text box to a property in the VM, so I wasn't sure if it also didn't need the setbinding), but having added that I'm still getting null. I also tried adding LookupBar.BindingContext=vm;, but also still getting null. Still something not quite right somewhere.

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    @udaykumar.4017 I'm aware that one can bind the text box to a property in the VM, however several blogs talk about the ability to pass it with the CommandSearchParameter, so I'm trying to work out the right syntax to do it that way (more elegant solution).

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    P.S. I did have "command<string>(TestSearch)", however the string part disappeared in my post, presumably got treated as a HTML tag (now rectified by putting a \ before the >).

  • SmartmanAppsSmartmanApps AUMember ✭✭✭
    edited March 22

    I tried an experiment, and changed Placeholder to Text, and lo and behold he text came through - then when I typed something else in, back to nothing - so the logic code is correct, just the binding itself which isn't working for some reason.

  • ColeXColeX Member, Xamarin Team Xamurai

    Check my sample below

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    @ColeX said:
    Check my sample below

    Thanks for that. When I click in text box, I get this message...

    2019-03-22 14:50:41.615275+1100 FormsApp.iOS[2442:22754] SecTaskLoadEntitlements failed error=22 cs_flags=200, pid=2442
    2019-03-22 14:50:41.616103+1100 FormsApp.iOS[2442:22754] SecTaskCopyDebugDescription: FormsApp.iOS[2442]/0#-1 LF=0

    ...and nothing further happens. i.e. the Debug message fails to appear.

    I should note this is on a remoted simulator. I probably should've also noted I'm developing on Win10 (so I'm debugging in UWP. The Android version isn't working yet to begin with due to a Dropbox issue which hasn't been resolved yet. Been working on that as well), in case that's relevant. When I change Placeholder to Text (so that there's already some text there), I get same error as soon as I click on the search icon.

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    @ColeX So I went to follow some instructions on how to add a UWP project (normally already there) so that I can test that, and eventually found that the FormsApp.UWP folder already exists, yet UWP isn't showing up in the solution explorer. You've thrown me there. I'm not sure why it's not showing up in solution explorer when the folder is clearly already there, and therefore not sure how to fix that?

  • ColeXColeX Member, Xamarin Team Xamurai

    Sorry, i didn't realize you are developing uwp , i modify the sample configuration , check it again please .

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    @ColeX said:
    Sorry, i didn't realize you are developing uwp , i modify the sample configuration , check it again please .

    Sorry I didn't mention it earlier, in case it's relevant.

    Hmm,that works, so I went line by line through the whole thing, and found the problem! (last line I checked of course) I added the line you said, but didn't delete the line you had referred to. i.e. I missed that you said "modify" (delete what I had and add what you said), and simply added the line you said. Now that I've deleted the other line it works! :-) A bit of crossed wires there for a bit, but got there in the end. Thank you!! :-)

Sign In or Register to comment.