How to respond to clicks in TableView? - xamarin

This sounds quite simple, but I haven't found anything like that in the web. How to add click listeners to data rows in TableView?

You could add a TapGestureRecognizer to the root element within your custom ViewCell. That way you can bind the gesture recognizer to a Command with command parameters.
<Grid.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Command="{Binding CustomCommand}" CommandParameter="{Binding Thing}" />
</Grid.GestureRecognizers>

Okay, what I've found so far, is that while you cannot add any type of input listeners to the TabeView itself, you can add Tapped event listener to the ViewCells inside it. This seems to be working:
var cell = new CustomCell();
cell.Tapped += OnCellTapped;
tableView.Root.LastOrDefault().Add(cell);
....
private void OnWalletTapped(object sender, EventArgs e)
{
Debug.WriteLine(sender.GetHashCode());
}

Related

How to disable Syncfusion's Autocomplete from moving to the next entry when completed?

I'm using Syncfusion's Autocomplete entry in Xamarin.Forms in a way not related to a form. Thus, I don't want it to step to the next Entry object in the UI. However, doesn't matter what I try doing, when finishing putting in the input, it jumps to the next Entry.
I have tried setting both Entrys IsTabStop to False, as well as setting the second one's TabIndex to be smaller the other one's. Nothing worked, the only think which I have found to work is disabling the second Entry while the other one is focused.
XAML:
<ScrollView ...>
// ....
<StackLayout>
// ....
<autocomp:SfAutoComplete x:Name="TagsAutoComplete"
WidthRequest="100"
NoResultsFoundText="New Tag..."
DisplayMemberPath="Name"
Keyboard="Chat"
IsTabStop="False"
IsVisible="False"
Completed="AddTagAutoComplete_Completed" />
// ....
</StackLayout>
// ....
<Frame>
<Grid>
// ....
<Entry FontSize="18" TextColor="Black"
WidthRequest="150" VerticalOptions="Center"
Unfocused="EquValueEntry_Unfocused"
TabIndex="-1" IsTabStop="False" />
// ....
</Grid>
</Frame>
// ....
</ScrollView>
My workaround uses the Focused and Unfocused methods of the Autocomplete, which simply set the IsEnabled property of the second Entry to False and True respectively. Does anyone have a better, more elegant solution?
We would like to let know that you can stop the next Entry focus by Return type and IME option.
UWP:
Setting IsTabStop as false it disables the next Entry focus.
Android:
By changing the input method options of EditText in autocomplete (using ImeOptions property)
protected override void OnElementChanged(ElementChangedEventArgs<SfAutoComplete> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.GetAutoEditText().ImeOptions = Android.Views.InputMethods.ImeAction.Done;
Control.GetAutoEditText().SetImeActionLabel("Send", Android.Views.InputMethods.ImeAction.Done);
}
iOS:
By changing the return type property of UITextField in autocomplete (using ReturnKeyType property)
protected override void OnElementChanged(ElementChangedEventArgs<SfAutoComplete> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.TextField.ReturnKeyType = UIReturnKeyType.Done;
}
}
We have prepared a sample for your reference get it from below link.
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/AutoComplete_EntryNotFocus1409818301
For more information refer the link:
https://www.syncfusion.com/kb/10690/how-to-change-return-button-type-in-sfautocomplete

Xamarin Forms: System.ObjectDisposedException when setting Focus to an Entry control

I have the following Xamarin Forms page that throws an exception on this line...
The first time this page is loaded, the OnAppearing works fine, sets the focus properly, and doesn't throw an exception.
When I navigate back to this page (ie, logout), OnAppearing is throwing the following...
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Xamarin.Forms.Platform.Android.EntryRenderer'.
What is the best way to set focus to a control on a page in Xamarin Forms?
I'm not sure what is in your XAML, but if you define the x:Name="_entry" on the Entry in XAML, and use that name to access the control directly instead of FindByName, it should work fine.
I try to reproduce your issue at my side, but it works fine and there is no issue when I click Button to navigate another page and coming back. Please take a look my code:
<StackLayout>
<Label
HorizontalOptions="CenterAndExpand"
Text="Welcome to Xamarin.Forms!"
VerticalOptions="CenterAndExpand" />
<Entry
x:Name="UserNameentry"
HorizontalOptions="FillAndExpand"
VerticalOptions="CenterAndExpand" />
<Button
x:Name="btn1"
Clicked="btn1_Clicked"
HeightRequest="50"
HorizontalOptions="FillAndExpand"
Text="btn1"
VerticalOptions="CenterAndExpand"
WidthRequest="200" />
</StackLayout>
public Page4()
{
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
var usernameentry = FindByName("UserNameentry") as Entry;
usernameentry.Focus();
}
private async void btn1_Clicked(object sender, EventArgs e)
{
Page3 page = new Page3();
await Navigation.PushModalAsync(page);
}
If you still have this issue, please provide some code about xaml here.

When using a text cell, is it possible to get the value of the text when it is tapped?

The text cell is inside of a listview. I'm populating the text of the text cell with data using Binding. I need to actually get the value of the text when the text cell is clicked on. Is there a way to do this?
XAML file:
<ListView ItemsSource="{Binding MyItems}" ItemTapped="{Binding OnItemTapped}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Key}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListVIew>
You bind the ItemTapped event to a custom event handler using Binding.
In codebehind, e.Item will contain the tapped item, from which you can extract the Key and Value.
Codebehind:
public class MyPage : ContentPage
{
...
private async void OnItemTapped(object sender, ItemTappedEventArgs e)
{
var tappedItem = e.Item;
// do something with tappedItem.Value...
}
}
Edit/Note: You will need to override the ToString() method in your model class in order to display whatever you want. You get the namespace displayed because that's the default behavior of calling ToString() on any object.
You could subscribe to ItemTapped or ItemSelected ListView events and pull your item from ItemTappedEventArgs.Item (in case of ItemTapped event) or SelectedItemChangedEventArgs.SelectedItem (in case of ItemSelected event) . More info in official Xamarin documentation
If you using MVVM approach look at EventToCommandBehavior and Converter.
Xamarin have good example here

How to automatically call Event in Windows Phone 8

How to automatically raise event in windows phone? For example, I have an element <Image name = "image" .... />. I want when a MainPage is loaded, it will automatically raise tap event on that element
If you want to declare tap event dynamically (loads tap event on page load), You can declare it in following way. Here is your xaml.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Image Name="image1"/>
</Grid>
And in constructor,
public MainPage()
{
InitializeComponent();
image1.Tap += image1_Tap;
}
void image1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
//Perform your action here
//This method invokes only when you tap on image
}
Else, try the other way.
Loaded += (s, e) =>
{
//Actions that are performed when image is tapped
}
Add above lines in your constructor.
This probably is not a good solution to whatever you are trying to accomplish. It can be done with reflection - How to manually invoke an event? However I'd just extract your Tap event code to method and then call this method in your Loaded event and also Tap even.

How to open TimeSpanPicker on a Button (Coding4Fun toolkit)

I have button and TimeSpanPicker in XAML.
<Image x:Name="sleepButton" Source="img/Sleep.png"
Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2"
Stretch="None" Tap="sleepButton_Tap"/>
<controls:TimeSpanPicker x:Name="timespanPicker" Step="0:5" Value="0:0"
PickerPageUri="timePickerPage"
Grid.Column="1" Grid.Row="2"
Visibility="Collapsed" IsEnabled="False" />
And here is my code.
private void sleepButton_Tap(object sender, GestureEventArgs e)
{
TimeSpanPicker timespanPicker = new TimeSpanPicker();
timespanPicker.PickerPageUri = new Uri("/MainPage.xaml", UriKind.Relative);
timespanPicker.OpenPicker();
}
Then I click the button do nothing. What I'm doing wrong?
The PickerPageUri is if you have a custom picker control such as a single button to set the date/time, not to show the TimeSpanPicker. You're also creating a new instance of TimeSpanPicker here essentially overriding your xaml:
private void sleepButton_Tap(object sender, GestureEventArgs e)
{
TimeSpanPicker timespanPicker = new TimeSpanPicker();
...
You should just be able to call timespanPicker.OpenPicker();. Not sure if you'll need to enable it, but yes, have the visibility collapsed.
You need to change the PickerPageUri to something other than the MainPage.xaml. The intention is to navigate to another page so that you can use this other page to set the time. The following article should help you with this :-
http://windowsphonegeek.com/articles/WP7-TimeSpanPicker-in-depth

Resources