Bug 27027 - SearchBar.SearchCommandParameter is not passed to the command
Summary: SearchBar.SearchCommandParameter is not passed to the command
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.3
Hardware: PC Windows
: Normal normal
Target Milestone: 1.3.5
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-02-13 02:49 UTC by Michael Rumpler
Modified: 2015-02-19 09:24 UTC (History)
6 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
VERIFIED FIXED

Description Michael Rumpler 2015-02-13 02:49:36 UTC
If you add a SearchBar to your page like this:

	<SearchBar Text="{Binding Value1}"
	  SearchCommand="{Binding SearchCommand}"
	  SearchCommandParameter="SearchBar1" />

Then your ICommand must be defined as a Command<object> and not Command<string> or there will be a runtime error.

With this code in the ViewModel you can see that the Command itself is passed to the method and not the SearchCommandParameter:

		public ICommand SearchCommand { get; protected set; }

		public SearchBarViewModel()
		{
			SearchCommand = new Command<object>(OnSearch);

		}

		private void OnSearch(object obj)
		{
			string parameter = obj as string;

			if (parameter != null)
			{
				AddText("Search button was clicked. Parameter passed to the command is '{0}' - a string as it should be.", parameter);

			}
			else
			{
				AddText("Search button was clicked. Parameter passed to the command is a {0} but it should be a string.", obj.GetType().Name);
			}
		}

This bug was already present in Xamarin.Forms 1.2.3 and it is still there in 1.3.3.


You can also use the GestureSample from https://github.com/MichaelRumpler/GestureSample to reproduce it although there is much more in that project than just the SearchBar. The related view is in Views/SearchBarXaml.xaml and the viewmodel in ViewModels/SearchBarViewModel.cs.
Comment 1 Sadik Ali 2015-02-13 05:53:22 UTC
I checked this issue with the help of given sample and unable to reproduce this behavior given in the bug description.When I deploy sample using Command<object> no error comes but when I used Command<string> then unhandeled exception comes.I checked this issue with Xamarin.Forms version="1.3.4.6331-pre4.

Screencast:http://www.screencast.com/t/Y6cj1mqOMx
Build Output:https://gist.github.com/Arpit360/7b096ef434bd27931eaa
XVS Logs:https://gist.github.com/Arpit360/44a3c2328da0dc799eb2

Please let me know If I missed any step and Can you please provide the XVS logs from Help>Zip Xamarin Logs and run time error comes at your end for Command<object>.

Environment Info:

Microsoft Visual Studio Professional 2013
Version 12.0.31101.00 Update 4
Microsoft .NET Framework
Version 4.5.51641

Installed Version: Professional

LightSwitch for Visual Studio 2013   06177-004-0446016-02758
Microsoft LightSwitch for Visual Studio 2013

Team Explorer for Visual Studio 2013   06177-004-0446016-02758
Microsoft Team Explorer for Visual Studio 2013

Visual Basic 2013   06177-004-0446016-02758
Microsoft Visual Basic 2013

Visual C# 2013   06177-004-0446016-02758
Microsoft Visual C# 2013

Visual C++ 2013   06177-004-0446016-02758
Microsoft Visual C++ 2013

Visual F# 2013   06177-004-0446016-02758
Microsoft Visual F# 2013

Visual Studio 2013 Code Analysis Spell Checker   06177-004-0446016-02758
Microsoft® Visual Studio® 2013 Code Analysis Spell Checker

Portions of International CorrectSpell™ spelling correction system © 1993 by Lernout & Hauspie Speech Products N.V. All rights reserved.

The American Heritage® Dictionary of the English Language, Third Edition Copyright © 1992 Houghton Mifflin Company. Electronic version licensed from Lernout & Hauspie Speech Products N.V. All rights reserved.

Windows Phone SDK 8.0 - ENU   06177-004-0446016-02758
Windows Phone SDK 8.0 - ENU

Application Insights Tools for Visual Studio Package   1.0
Application Insights Tools for Visual Studio

ASP.NET Web Frameworks and Tools 2013   5.2.21010.0
For additional information, visit http://www.asp.net/

Common Azure Tools   1.3
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Microsoft Azure HDInsight HQL Service   1.0.0000.0
Language service for Hive query

Microsoft Azure HDInsight Tools for Visual Studio   1.0.0000.0
An integrated development environment for HDInsight application development.

Microsoft Azure Mobile Services Tools   1.3
Microsoft Azure Mobile Services Tools

Microsoft Azure Tools   2.5
Microsoft Azure Tools for Microsoft Visual Studio 2013 - v2.5.21104.1601

NuGet Package Manager   2.8.50926.663
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

Office Developer Tools for Visual Studio 2013 ENU   12.0.30626
Microsoft Office Developer Tools for Visual Studio 2013 ENU

PowerShell Tools   1.3
Provides file classification services using PowerShell

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SQL Server Data Tools   12.0.41012.0
Microsoft SQL Server Data Tools

Windows Phone 8.1 SDK Integration   1.0
This package integrates the tools for the Windows Phone 8.1 SDK into the menus and controls of Visual Studio.

Workflow Manager Tools 1.0   1.0
This package contains the necessary Visual Studio integration components for Workflow Manager.

Xamarin   3.9.318.0 (b2a3fd2)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   4.20.0.34 (49a04b966feb40dfdba49d57ba16249b66d606a6)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.iOS   8.6.2.0 (09ae75f83eafce1bdc3fa067f281cbdf1ca7b893)
Visual Studio extension to enable development for Xamarin.iOS.

Xamarin.iOS Unified Migration   1.0
Automated migration for Xamarin iOS Classic projects to Unified
Comment 2 Michael Rumpler 2015-02-13 06:48:24 UTC
There is no runtime error because I wrote the OnSearch method in a way that it does not fail.

If I set SearchCommandParameter="SearchBar1", then I expect this string "SearchBar1" to be passed to the OnSearch method. But this is not the case. The SearchCommand is passed instead.

As there is no runtime error if you define the SearchCommand as a Command<object>, the Xamarin Logs would not help you at all.

The bug can easily be fixed in Xamarin.Forms.SearchBar.OnSearchButtonPressed(). This method currently starts like this:

    internal void OnSearchButtonPressed()
    {
      ICommand searchCommand = this.SearchCommand;
      if (searchCommand != null)
        searchCommand.Execute((object) this.SearchCommand);

But the last line should be

        searchCommand.Execute((object) this.SearchCommandParameter);

And I just checked in version 1.3.4.6331-pre4, it is still wrong there.
Comment 3 Jason Smith [MSFT] 2015-02-13 08:39:52 UTC
Thank you for the report Michael, the dev team is now resolving the issue :)
Comment 4 Jason Smith [MSFT] 2015-02-13 08:55:01 UTC
Should be fixed in 1.3.5-pre1
Comment 5 Parmendra Kumar 2015-02-19 09:24:41 UTC
I have checked this issue with Xamarin.Forms.1.3.5.6333-pre1 and now its working fine at my end.

Screencast for the same: http://www.screencast.com/t/EV8idDwvsl

Environment info: 

Microsoft Visual Studio Professional 2013
Version 12.0.31101.00 Update 4
Microsoft .NET Framework
Version 4.5.51641
Installed Version: Professional
Xamarin   3.9.293.0 
Xamarin.Android   4.20.0.34 
Xamarin.iOS   8.6.1.0 

Hence closing this issue.