1

Trying to get the text from a Xamarin Forms 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 :-) )...

MAINPAGE...
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. :-(

thanks,
Donald.

donaldp
  • 220
  • 1
  • 3
  • 15

2 Answers2

2

You need to set the binding, because this:

LookupBar.SearchCommandParameter=LookupBar.Text;

will always send null as it is the initial value of the LookupBar.Text when the page initialized.

Binding in code:

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

Binding in XAML:

<SearchBar Placeholder="Enter search term" x:Name="LookupBar" SearchCommand="{Binding TestSearchCommand}" 
           SearchCommandParameter="{Binding Source={x:Reference LookupBar}, Path=Text}"/>
mshwf
  • 7,009
  • 12
  • 59
  • 133
  • 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. – donaldp Mar 21 '19 at 22:00
  • Your comment got me thinking, so I tried an experiment - I changed Placeholder to Text, so that there was initially text there, and lo and behold, it came through. Then when I typed something in, back to getting nothing. So that proves that in general my code is correct, just the binding itself isn't working correctly for some reason. – donaldp Mar 21 '19 at 23:51
  • Got it working now. There was just some crossed wires there for a bit. I needed to REPLACE the 1st line with the 2nd, however I had only added the 2nd line. Now that I've deleted the 1st line it works. :-) Thank you! – donaldp Mar 22 '19 at 06:28
  • P.S. I upvoted your answer, but it doesn't show up because I don't have enough reputation. – donaldp Mar 22 '19 at 06:29
  • Well, I was looking for it and couldn't find it - figured you just relied on the upvotes - where is it? – donaldp Mar 23 '19 at 07:04
  • Ok, I found it now. It was a greyed-out tick - not at all obvious! – donaldp Mar 23 '19 at 07:06
0

See answer from @mshwf. Replace 1st line with 2nd.

donaldp
  • 220
  • 1
  • 3
  • 15
  • 1
    Hi friend, you should avoid this kind of comments in the answers, instead if you wand, add your comment to the answer/question you want. – mshwf Mar 22 '19 at 12:26