Hi,
I have used ListView with custom ViewCell's that contain Entry, but Entry text box losing focus immediately after the I focuses it. It also does not bring up a keyboard when pressed.
I have checked several links but I didn't get proper solution, they mansion that it is bug of Xamarin form
I am using Xamarin form version - 2.0.0.6490.
Below is the code -
Test.xaml.cs file code -
List objPendingList1 = new List();
objPendingList1.Add(new PendingUpdate() { Id = "1", Status = "0", CanEdit = true, Due = DateTime.Now, Start = DateTime.Now, Item = new PendingUpdateItem() { Id = "11", Name = "test" } });
objPendingList1.Add(new PendingUpdate() { Id = "2", Status = "0", CanEdit = true, Due = DateTime.Now, Start = DateTime.Now, Item = new PendingUpdateItem() { Id = "12", Name = "test1" } });
var dataTemplate = new DataTemplate(typeof(PendingUpdateCell));
var listView = new ListView();
Device.BeginInvokeOnMainThread(() =>
{
listView.ItemsSource = objPendingList1;
listView.ItemTemplate = dataTemplate;
listView.HasUnevenRows = true;
Content = listView;
});
CustomViewCell -
public class PendingUpdateCell: ViewCell
{
public PendingUpdateCell()
{
int tapCount = 0;
string selectedStatus = "";
string selectedDate = "";
string addedComment = "";
var tabImage = new Image()
{
HeightRequest = 20,
WidthRequest = 30,
Aspect = Aspect.AspectFill,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.End,
Source = "icon_next.png"
};
var nameLabel = new Label()
{
FontSize = 16,
TextColor = Color.Black,
LineBreakMode = LineBreakMode.WordWrap
};
nameLabel.SetBinding(Label.TextProperty, "Item.Name");
var dateLabel = new Label()
{
FontSize = 12,
TextColor = Color.Black,
LineBreakMode = LineBreakMode.CharacterWrap
};
dateLabel.SetBinding(Label.TextProperty, (new Binding(path: "Start", stringFormat: "{0:MM/dd/yyyy}")));
var duedateLabel = new Label()
{
FontSize = 12,
TextColor = Color.Black,
LineBreakMode = LineBreakMode.CharacterWrap
};
duedateLabel.SetBinding(Label.TextProperty, new Binding(path: "Due", stringFormat: "{0:MM/dd/yyyy}"));
var dateStack = new StackLayout()
{
Spacing = 2,
Orientation = StackOrientation.Horizontal,
Children = { dateLabel }
};
var vetDetailsLayout = new StackLayout
{
Padding = new Thickness(10, 0, 0, 0),
Spacing = 0,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
Children = { nameLabel, dateStack, duedateLabel }
};
var status = new Label()
{
FontSize = 14,
TextColor = Color.Black,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
LineBreakMode = LineBreakMode.WordWrap
};
status.SetBinding(Label.TextProperty, "StatusText");
var statusLayout = new StackLayout
{
Spacing = 0,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Padding = new Thickness(5, 5, 5, 5),
Children = { status }
};
var cellLayout = new StackLayout
{
Spacing = 0,
Padding = new Thickness(10, 5, 10, 5),
Orientation = StackOrientation.Horizontal,
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children = { vetDetailsLayout, statusLayout, tabImage }
};
DatePicker datePicker = new DatePicker()
{
Format = "MM/dd/yyyy",
HorizontalOptions = LayoutOptions.FillAndExpand
};
datePicker.DateSelected += (sender, e) =>
{
selectedDate = e.NewDate.ToString();
};
datePicker.SetBinding(DatePicker.DateProperty, new Binding(path: "Start", stringFormat: "{0:MM/dd/yyyy}"));
Picker picker = new Picker
{
Title = "Select Status",
VerticalOptions = LayoutOptions.CenterAndExpand
};
foreach (string statusName in Constants.statusList.Select(s=> s.Value).ToList())
{
picker.Items.Add(statusName);
}
picker.SelectedIndex = Convert.ToInt32(status.Text);
selectedStatus = status.Text;
if (!string.IsNullOrEmpty(status.Text))
status.SetBinding(Label.TextProperty, Constants.statusList.Where(s => s.Key == status.Text).FirstOrDefault().Value);
picker.SelectedIndexChanged += (sender, args) =>
{
if (picker.SelectedIndex == -1)
{
selectedStatus = "0";
}
else
{
string statusName = picker.Items[picker.SelectedIndex];
selectedStatus = Convert.ToString(picker.SelectedIndex);
}
};
var commentEntry = new Entry()
{
Text = "",
Keyboard = Keyboard.Default,
Placeholder = "Comment Here....",
VerticalOptions = LayoutOptions.FillAndExpand,
TextColor = Color.Black
};
var innerLayout = new StackLayout()
{
Padding = new Thickness(10, 10, 10, 10),
Spacing = 0,
BackgroundColor = Color.White,
Orientation = StackOrientation.Vertical,
Children = { datePicker, picker, commentEntry }
};
var saveButton = new Button()
{
Text = "Save",
VerticalOptions = LayoutOptions.End,
HeightRequest = 35,
FontSize = 16,
BackgroundColor = Color.Green
};
saveButton.Clicked += (sender, e) =>
{
PendingUpdate pendingUpdate = BindingContext as PendingUpdate;
Button btnSave = (Button)sender;
var parentLayout = (StackLayout)btnSave.ParentView.ParentView;
StackLayout saveLayout = (StackLayout)parentLayout.Children[0];
DatePicker dtStart = (DatePicker)saveLayout.Children[0];
Entry entryComment = (Entry)saveLayout.Children[2];
CommonFunction objCommonFunction = new CommonFunction();
if (string.IsNullOrEmpty(selectedStatus))
selectedStatus = pendingUpdate.Status;
dynamic addUpdateProgress = new JObject();
addUpdateProgress.id = pendingUpdate.Id;
addUpdateProgress.asOf = selectedDate;
addUpdateProgress.requestedBy = new JObject(new JProperty("fullName", Application.Current.Properties["UserFullName"] as string), new JProperty("id", Application.Current.Properties["UserId"] as string));
addUpdateProgress.status = selectedStatus;
addUpdateProgress.canEdit = pendingUpdate.CanEdit;
addUpdateProgress.start = dtStart.Date;
addUpdateProgress.due = pendingUpdate.Due;
addUpdateProgress.planItemId = pendingUpdate.Item.Id;
addUpdateProgress.comment = new JObject(new JProperty("commentRaw", entryComment.Text));
// string status1 = selectedStatus;
PendingUpdates objPendingUpdates = new PendingUpdates();
CallbackHandler handler = new CallbackHandler(objPendingUpdates.callback_savePendingUpdate);
objCommonFunction.RestServiceCall("api/cardmodal/updateprogressupdate", JsonConvert.SerializeObject(addUpdateProgress), handler);
};
var saveStack = new StackLayout
{
Padding = new Thickness(2, 5, 2, 0),
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.Center,
Children = { saveButton }
};
var updateLayout = new StackLayout
{
Padding = new Thickness(10, 10, 10, 10),
Spacing = 0,
BackgroundColor = Color.FromHex("#D3D3D3"),
Orientation = StackOrientation.Vertical,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
Children = { innerLayout, saveStack },
IsVisible = false
};
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += ((sender, e2) =>
{
tapCount++;
StackLayout detailStack = (StackLayout)sender;
Image image = (Image)detailStack.Children[2];
if (tapCount % 2 == 0)
image.Source = "icon_next.png";
else
image.Source = "icon_down.png";
var parentView = (StackLayout)detailStack.ParentView;
var saveUpdateView = (StackLayout)parentView.Children[1];
if (saveUpdateView.IsVisible)
saveUpdateView.IsVisible = false;
else
saveUpdateView.IsVisible = true;
});
cellLayout.GestureRecognizers.Add(tapGestureRecognizer);
var layout = new StackLayout
{
Orientation = StackOrientation.Vertical,
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
BackgroundColor = Color.White,
Children =
{
cellLayout,
updateLayout,
}
};
View = layout;
}
}
another file contain classes -
public class PendingUpdate
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("start")]
public DateTime Start { get; set; }
[JsonProperty("due")]
public DateTime Due { get; set; }
[JsonProperty("item")]
public PendingUpdateItem Item { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("canEdit")]
public bool CanEdit { get; set; }
public string StatusText { get; set; }
}
public class PendingUpdateItem
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
commentEntry - does not work here.
Please suggest me.