I'm working on a Xamarin.Forms MVVM mobile app, with a page that has a ListView
.
I'm trying to access one of the ViewCell
's in the ListView
, and then update content inside that ViewCell
right after the page loads. Ultimately, I'm trying to animate (via TranslateTo()
), but for testing purposes, I'm just trying to make something within the ViewCell
invisible (by setting IsVisible = false
).
It works on my iOS project, but not on my UWP one. Nothing happens; the ViewCell
content is never affected.
The basic code is:
----------------
MyPage.cs
----------------
public MyPage() // constructor
{
_viewModel = new MyViewModel();
...
...
}
// on page load, subscribe to a message that modifies a ViewCell in the ListView;
// when that message is called in the ViewModel, it correctly modifies the ViewCell
// in iOS, but in UWP it does nothing;
protected override async void OnAppearing()
{
MessagingCenter.Subscribe<MyViewModel>(this, "ModifyViewCell", (sender) =>
{
// get item cell to modify (for testing, just get first one);
// see code in ListViewExt.cs a little farther down for GetCell() method
MyViewCellView viewCell = GetCell(0); // MyViewCellView : ViewCell
// modify cell - try different methods
viewCell.IsEnabled = false;
viewCell.View.IsVisible = false;
viewCell.MyViewInsideCell.IsVisible = false; // <<<==== breakpoint
}
}
-------------------
MyViewModel.cs
-------------------
public MyViewModel() // constructor
{
// send message to update ViewCell in ListView
Device.BeginInvokeOnMainThread(() => {
MessagingCenter.Send(this, "ModifyViewCell");
});
}
ViewCell
s of a ListView
are stored in a Dictionary in a custom ListView
class (ListViewExt
) when the ListView.SetupContent()
method is called (overridden below):
ListViewExt.cs
------------------------
// store ListView ViewCell's in dictionary for access later
public Dictionary<int, ViewCell> CellDict { get; set; } = new Dictionary<int, ViewCell>();
protected override void SetupContent(Cell cell, int index)
{
base.SetupContent(cell, index);
// add ViewCell to dictionary on SetupContent()
CellDict.Add(index, (ViewCell)cell);
}
public GetCell(int index)
{
return CellDict[index];
}
I've also tried setting a breakpoint in the code on the line where I'm trying to modify the ViewCell
(I've noted the breakpoint in a comment in the code above), and when I hover over viewCell
at that point, Intellisense seems to indicate I have a valid reference to the ViewCell
view (the viewCell.MyViewInsideCell
- it says it's a Xamarin.Forms.Grid, which it is). But even with a valid reference, I can't seem to affect any of it's properties.
Is this a Xamarin UWP bug? It appears to be.
I've logged a bug in Bugzilla here.
Here's a link to a sample project where the issue is reproduced.
BindingContext
or share a sample simple to reproduce this issue?