Xamarin Autocomplete view: Display dropdown even when there is no text - xamarin

I am implementing an autocomplete view in my mobile app that i am developing using Xamarin.
I have set threshold to 1 and set the focus to autocomplete view in onCreate event of activity.
autoCompleteView.FocusChange += delegate(object sender, View.FocusChangeEventArgs args)
{
if (args.HasFocus)
{
autoCompleteView.ShowDropDown();
}
};
It displays the dropdown with all the suggestions when my app is loaded. Whenever i type text, it filters based on condition and everything is working fine. But when i clear all the text in the autocomplete view, the dropdown is closed. But i want to display the dropdown with all the suggestions.
Also whenever i touch/click the autocomplete view, the dropdown is closed. So i have added the below code to display the dropdown, but there is a flicker(dropdown is closed and is opened again).
autoCompleteView.Click += delegate(object sender, EventArgs args) {
autoCompleteView.ShowDropDown ();
};
Thanks in advance.

It displays the dropdown with all the suggestions when my app is
loaded
You can call autoCompleteView.ShowDropDown(); in you OnCreate (independant of focus), this way the app loads with the dropdown showing.
Also whenever i touch/click the autocomplete view, the dropdown is
closed. So i have added the below code to display the dropdown, but
there is a flicker(dropdown is closed and is opened again).
Subscribe to the TextChanged event and call autoCompleteView.ShowDropDown(); whenever the text in the EditText is String.Empty.

Related

How not to reload CollectionView after clicking on Back button

Hi I'm facing a problem with my small xamarin app.
I have my small xamarin-form app that everytime the user enter a product Id click OK button retrieves a single (data related to that product Id only) data thru Web API from page A, and then pass it to a collectionview in another page B.
then there is a back button to go back to page A and the user must enter another product Id again and the app must fetch data thru web api again and pass it again to page B in the collectionview.
So the problem is now, when I click on back button in Page B, it's clear the collectionview data, whereas I need the collectionview to keep the state even If I pop back.
I'm not using any MVVM pattern or complicated stuff from now as I'm new to xamarin; it's just a simple architecture.
Or maybe I'm using wrong technics to achieve this and can advise me a better way to achieve that. I'm please open to your suggestion !!!
In brief, I need to be able not to reload CollectionView after clicking on Back button.
Below is the code that Bind to the View on Page B
public async void OnGetProductDetailButton(object sender, EventArgs e)
{
await Navigation.PushAsync(new ProductDetailPage
{
BindingContext = productDetails
});
}

Xamarin.Forms: close DatePicker in code, when custom renderer is used

I have a custom DatePicker, which is opened if the user taps on it. It uses the same mechanics like the Xamarin.Forms DatePicker. The scenario is like the following:
user taps on the custom DatePicker
instead of picking a date he opens another dialog
two dialogs are now opened at the same time
I tried to Unfocus() if the other element get's the focus, but nothing happens. The DatePicker is still displayed.
What else can I do? Should I throw manually the Unfocus event? Can I lock the UI somehow so that the user has to press the Finish button before moving on?
First, the issue doesn't occur if I switch between DatePicker and TimePicker. It only occurs if I switch from DatePicker/TimePicker to my Entry (which triggers a custom dialog in Focused event).
And Unfocus() does work, if you do it on the UI thread:
private async void someEntry_Focused(object sender, FocusEventArgs e)
{
Device.BeginInvokeOnMainThread(() =>
{
this.datePicker.Unfocus();
});
// custom dialog shown to user ...
}

Two way databinding from TextBox doesn't update when button in ApplicationBar is clicked

I have a TextBox in my app, and an ApplicationBarIconButton in the ApplicationBar which acts as a "submit" for the contents of the TextBox.
When editing the TextBox using the virtual keyboard, the ApplicationBarIconButton is still visible below the SIP, so you can submit straight away without dismissing the keyboard: nice!
However, when clicking the button, the viewmodel to which the TextBox is bound does not update.
I found someone else with the same problem here, and they have used the pretty nasty workaround of manually updating the viewmodel on the TextBox's TextChanged event.
Removes all the elegance of using databound view models!
Is this a bug in WP7?
Or is there a nicer way around this that I haven't found yet?
The problem is that silverlight bindings do not support the PropertyChanged value for UpdateSourceTrigger. This means that by default a TextBox will update the property bound to Text when the TextBox loses focus and the only other possibility is to update it explicitly in code as is done in the example from your link.
You only really have two options here: Update the binding when the button is clicked or remove focus from the TextBox when the button is clicked.
I usually update the binding on the TextChanged event. I use an extension method to do this:
public static void UpdateBinding(this TextBox textBox)
{
BindingExpression bindingExpression =
textBox.GetBindingExpression(TextBox.TextProperty);
if (bindingExpression != null)
{
bindingExpression.UpdateSource();
}
}
allowing me to just call this in code behind:
textBox.UpdateBinding();
You may also be able to use a custom behaviour for this.
Summary of the steps to allow binding to work for each keypress of a textbox, instead of just when the text box loses focus. Uses Prism. It is a slightly indirect solution to the original problem.
In NuGet package manager, search for Prism. Add "Prism.Phone" created by "Microsoft patterns & practices"
Add the following to the page's phone:PhoneApplicationPage tag
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:prismInteractivity="clr-namespace:Microsoft.Practices.Prism.Interactivity;assembly=Microsoft.Practices.Prism.Interactivity"
Give your textbox a separate closing tag, and add the following between the opening and closing TextBox tags
<i:Interaction.Behaviors>
<prismInteractivity:UpdateTextBindingOnPropertyChanged/>
</i:Interaction.Behaviors>
I guess this will work but you should check if it is really necessary having an ApplicationBarIconButton (or simply a button on the page) for that.
Often you should avoid this when you like to have a good Metro design in your app you may prefer using InputScope="Search" + Hiding the SIP is easily done using Page.Focus()
e.g. (an old article; InputScope="Search" worked for me)
http://4mkmobile.com/2011/02/wp7-devs-stop-adding-search-buttons/
See also: http://forums.create.msdn.com/forums/p/70506/619517.aspx#619517
private void SearchTextBox_KeyUp(object sender, KeyEventArgs e) handler:
Using InputScope="Search" for my search field
Using DataBinding Mode=TwoWay
Focus(); // hides the SIP
UpdateBinding(SearchTextBox); // the trick mentioned in here
App.ViewModel.ExecuteSearch();
Works fine in my MVVM app.

Pickerboxdialog Customizing

So, I'm attempting to use this:
http://blogs.msdn.com/b/priozersk/archive/2010/09/17/customizing-picker-box-dialog.aspx
However, I just want a normal pickerboxdialog (just text) but I'd like to attach an id to it, so I can easily reference the selection the user picked. However, even after building my own class to pass in, I still cannot get the text to display properly (IE at all) within the pickerbox.
Does anyone have an experience? I basically copied his code and still no luck...
if you want a normal picker box then you shouldn't have to worry about customizing the template (unless you want to display the ID too).
The way you reference the object selected by the user is just in the Closed event handler:
void Dialog_Closed(object sender, EventArgs e)
{
var picker = (PickerBoxDialog)sender;
var selected = (YourCustomObject)picker.SelectedItem;
}
In other words you shouldn't need the ID of the selected object because you can get a reference select object directly.
The Silverlight toolkit includes a ListPicker control which provides the functionality you're after.
It displays like the so called "picker box" but also includes a SelectedItem property and a SelectionChanged event.

windows phone 7 button in longlistselector

I've read somewhere that longlistselector from the toolkit is better in performance than the existing listbox. So, I changed the listbox to longlistselector. Now I have a image button control to keep in the longlistselector (that acts like a checkbox). When I click on the button, the list selection changed event is fired along with the button click. The button in listbox works fine as expected but not in longlistselector. How can I stop the list selection changed event? I searched a lot on this but couldn't find anything useful. First of all is it possible?
I wouldn't take it for granted that the long list selector performs better than the listbox. The listbox uses a virtualizing stack panel when binding is involved and is pretty performant. I went down the road of using the list picker from the toolkit and ended up regretting it due to some bad performance problems. If it works with the listbox I'd say stick with the listbox and only move away if you find you have perfomance issues in the future.
When a button is clicked a button event handler is triggered and when a item in the long list selector is changed the corresponding selection changed event is triggered IF it is also registered. But the button is clicked on the same selected item, only the button event handler is triggered. I suggest to have only a button event handler and get the selected item from it.
private void ButtonEvent_Click(object sender, RoutedEventArgs e)
{
HoldingClass clicked=((sender as Button).DataContext as HoldingClass);
//Do something with the HoldingClass as this is the binding element to the long list selector
}
Change the ClickMode to Press in XAML
ClickMode="Press"
and inside you Click event handler make (YourListName).SelectedItem = null;
private void deleteButton_Click(object sender, RoutedEventArgs e)
{
MainLongListSelector.SelectedItem = null;
..
}

Resources