My table view does not update whenever its source property is changed. The code is as follows:
public override void ViewDidLoad()
{
base.ViewDidLoad();
viewmodel = this.ViewModel as ListViewModel;
viewmodel.PropertyChanged += HandlePropertyChangedEventHandler;;
var source = new MvxSimpleTableViewSource( TableView, LaborCell.Key, LaborCell.Key);
TableView.Source = source;
var set = this.CreateBindingSet<ListView, ListViewModel>();
set.Bind(source).To(vm => vm.LaborTransactions);
set.Apply();
TableView.ReloadData();
}
ViewModel:
public class ListViewModel :MaxRawBaseViewModel
{
public ListViewModel():base()
{
LoadLaborTransactions();
}
private Collection<LaborTransaction> _laborTransactions;
public Collection<LaborTransaction> LaborTransactions
{
get { return _laborTransactions; }
}
public void LoadLaborTransactions()
{
_laborTransactions = DataService.GetLaborTransactions(somenumber);
RaisePropertyChanged(() => LaborTransactions);
}
}
When ever the change in Transactions am calling the tablview.reolad() on propertychanged method. but it is not reloading my tableview
void HandlePropertyChangedEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e){
if (e.PropertyName.Equals("LaborTransactions"))
{
TableView.ReloadData();
}
}
Collection<T> does not implement INotifyPropertyChanged. You can verify that in the docs here. You need to change your LaborTransactions property to a collection type that implements INotifyPropertyChanged like ObservableCollection<T> and MvxObservableCollection<T>. You can see that ObservableCollection<T> implements INotifyPropertyChanged here
Change your LaborTransactions as such:
private ObservableCollection<LaborTransaction> _laborTransactions;
public ObservableCollection<LaborTransaction> LaborTransactions
{
get { return _laborTransactions; }
set {
return _laborTransactions;
RaisePropertyChanged(() => LaborTransactions);
}
}
Related
I am using MvvmCross in my Xamarin.Android application. I want to make my own custom MvxRecyclerAdapter so that I can have multiple buttons in each row of the MvxRecyclerView. Here is my custom MvxRecyclerView:
public class TwoPieceMvxRecyclerView : MvxRecyclerView
{
private bool _initialized;
public TwoPieceMvxRecyclerView(Context context, IAttributeSet attr) : base(context, attr)
{
}
public override Android.Support.V7.Widget.RecyclerView.Adapter GetAdapter()
{
if(!_initialized)
{
SetAdapter(new TwoPieceMvxRecyclerAdapter());
_initialized = true;
}
return base.GetAdapter();
}
}
And here is my custom MvxRecyclerAdapter:
public class TwoPieceMvxRecyclerAdapter : MvxRecyclerAdapter, IOnClickListener
{
private ICommand _itemClickPiece1;
private ICommand _itemClickPiece2;
private View _clickablePiece1;
private View _clickablePiece2;
public TwoPieceMvxRecyclerAdapter()
{
}
public ICommand ItemClickPiece1
{
get { return _itemClickPiece1; }
set
{
if (ReferenceEquals(_itemClickPiece1, value))
{
return;
}
_itemClickPiece1 = value;
}
}
public ICommand ItemClickPiece2
{
get { return _itemClickPiece2; }
set
{
if (ReferenceEquals(_itemClickPiece2, value))
{
return;
}
_itemClickPiece2 = value;
}
}
protected override Android.Views.View InflateViewForHolder(Android.Views.ViewGroup parent, int viewType, MvvmCross.Binding.Droid.BindingContext.IMvxAndroidBindingContext bindingContext)
{
var view = base.InflateViewForHolder(parent, viewType, bindingContext);
_clickablePiece1 = view.FindViewById<View>(Resource.Id.clickable_piece1);
_clickablePiece2 = view.FindViewById<View>(Resource.Id.clickable_piece2);
_clickablePiece1.SetOnClickListener(this);
_clickablePiece2.SetOnClickListener(this);
return view;
}
public void OnClick(View v)
{
if (v == _clickablePiece1)
{
ItemClickPiece1.Execute(null);
}
else if (v == _clickablePiece2)
{
ItemClickPiece2.Execute(null);
}
}
}
When I run the application I get this error:
Could not activate JNI Handle 0xbfd00978 (key_handle 0x6e44919) of
Java type
'md5bd77c484e80df14e69d8c5ab04394fe0/TwoPieceMvxRecyclerView' as
managed type
'AzzimovMobile.Droid.Components.TwoPieceMvxRecycler.TwoPieceMvxRecyclerView'.
System.InvalidOperationException: If you wan't to use single
item-template RecyclerView Adapter you can't change
it'sIMvxTemplateSelector to anything other than
MvxDefaultTemplateSelector
You are missing a constructor on your RecyclerView:
public TwoPieceMvxRecyclerView(IntPtr javaReference, JniHandleOwnership transfer): base(javaReference, transfer)
{
}
Also be aware you don't need to use a custom RecyclerView to change its Adapter. You can just grab the RecyclerView instance on your .cs view and set the adapter from there. Something like this should work:
public class MyView: MvxFragment<MyViewModel>
{
//...
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = base.OnCreateView(inflater, container, savedInstanceState);
// ...
var recycler = view.FindViewById<MvxRecyclerView>(Resource.Id.recycler);
recycler.Adapter = new TwoPieceMvxRecyclerAdapter(((IMvxAndroidBindingContext)BindingContext);
// you can even set a TemplateSelector here!
recycler.ItemTemplateSelector = new MyTemplateSelector();
// ...
return view;
}
}
I've got problem with data-binding in mvvmcross after doing the navigation in the model by calling the showviewmodel-method. On the android side it works.
So the Problem is, that the navigation itself is working but I don't get any data from the model.
Navigation in the model:
ShowViewModel<TeamEventDetailsViewModel>(new { eventID = item.ID });
ViewModel which containts the Data:
public class TeamEventDetailsViewModel
: EventDetailsViewModel
{
public TeamEventModel CurrentEvent
{
get { return MyCurrentEvent as TeamEventModel; }
set
{
MyCurrentEvent = value;
RaisePropertyChanged(() => CurrentEvent);
TickerModel.Comments = value.Comments;
RaisePropertyChanged(() => TickerModel);
LineupModel.Team1Players = value.Team1Players;
LineupModel.Team2Players = value.Team2Players;
RaisePropertyChanged(() => LineupModel);
}
}
private EventDetailsLineupViewModel _lineupModel = new EventDetailsLineupViewModel();
public EventDetailsLineupViewModel LineupModel
{
get { return _lineupModel; }
set { _lineupModel = value; RaisePropertyChanged(() => LineupModel); }
}
public TeamEventDetailsViewModel()
{
EventToken = MvxMessenger.Subscribe<EventUpdateMessage>(OnEventUpdateMessage);
}
private void OnEventUpdateMessage(EventUpdateMessage eventUpdate)
{
if (MyCurrentEvent != null && eventUpdate.Event.ID == MyCurrentEvent.ID)
{
var updatedEvent = (TeamEventModel)eventUpdate.Event;
var myEvent = CurrentEvent;
if(updatedEvent.Score!=null)
myEvent.Score = updatedEvent.Score;
if (updatedEvent.Team1Players != null)
myEvent.Team1Players = updatedEvent.Team1Players;
if (updatedEvent.Team2Players != null)
myEvent.Team2Players = updatedEvent.Team2Players;
CurrentEvent = myEvent;
}
}
protected override void Update(EventModel eventdetails)
{
CurrentEvent = (TeamEventModel) eventdetails;
}
private string _teststring = "success";
public string Teststring
{
get { return _teststring; }
set
{
_teststring = value;
RaisePropertyChanged(()=>_teststring);
}
}
}
As you can see at the bottom I implemented a teststring to prove functionality.
Binding in the View:
public class TeamEventDetailsView : MvxViewController
{
public UILabel TestLabel = new UILabel();
public TeamEventDetailsViewModel TeamEventDetailsViewModel
{
get { return (TeamEventDetailsViewModel)base.ViewModel; }
set { base.ViewModel = value; }
}
public override void ViewDidLoad()
{
View.AddSubview(TestLabel);
this.CreateBinding(TestLabel).To<TeamEventDetailsViewModel>(vm => vm.Teststring).Apply();
TestLabel.BackgroundColor = UIColor.Orange;
}
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
TestLabel.Frame=new RectangleF(0,20,View.Frame.Width,80);
}
}
So I repeat, the navigation itself works but the data from model doesn't get shown on the view.
If I create the ViewModel manually in the View then the binding works also, but in my Situation I can't do that because the Data is pulled depending on the generated Data from the ViewModel which calls the navigation-proceed.
Manual ViewModel:
TeamEventDetailsViewModel = new TeamEventDetailsViewModel();
TeamEventDetailsViewModel.Init(9816);
As I can tell I did exactly the same as Stuard does in his Tutorial:
https://www.youtube.com/watch?v=cbdPDZmuHk8
Does anyone has an advice for me?
Thanks.
MvvmCross does the ViewModel create in base.ViewDidLoad() - if you add that call to your ViewDidLoad override then everything should work ok
I've got a special problem with binding data to an itemsource of an mvxtablieviewsource.
I'm trying to generate a list of favorites, which are generated in the core by clicking on a different tablview.
Normally I get the databinding working like this: (Just basic structure)
controller:
var source = new MySource(TableView);
this.AddBindings(new Dictionary<object, string>
{
{source, "ItemsSource Favs"}
});
Source:
private List<FavModel> _favs;
public override IEnumerable ItemsSource
{
get { return _favs; }
set
{
_favs = (List<FavModel>)value;
ReloadTableData();
}
}
protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
var cell = new AdFavCell();
cell.TextLabel.Text = ((FavModel)item).Display;
return cell;
}
Normally it works really great but no in this case where i generate the data by reacting on users touch, I've got this strange failure;
When I set a breakpoint in the setter of the ItemsSource, and wait for a while then it works correctly.
When I run without a breakpoint the tableview keeps empty.
I also figured out that if I insert a manually pause in the setter then it works too:
Setter with pause:
public override IEnumerable ItemsSource
{
get { return _mydata; }
set
{
_favs = (List<FavModel>)value;
ReloadTableData();
Task.Delay(1000).Wait();
}
}
I also tried to do a delaybinding, but it didn't work.
Have anyone an idea where the problem is?
Edit:
Here some additional Information:
How the Data is generated:
I've got a tableview with content and depending on a longclick on a cell, I create a popumenu where you can add your favorites.
Detecting the longclick:
protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
MvxTableViewCell cell = null;
if (item is SoccerEventListModel)
{
cell = tableView.DequeueReusableCell(this.CellIdentifier) as SoccerEvent;
if (cell == null)
{
cell = new SoccerEvent((SoccerEventListModel)item);
cell.AddGestureRecognizer(new UILongPressGestureRecognizer((e) =>
{
if (e.State == UIGestureRecognizerState.Began)
{
var command = ItemLongClickCommand;
if (command != null)
command.Execute(item);
}
}));
return cell;
}
}
}
Binding the Longclick to the core:
EventListViewModel.EventFavViewCallbackEvent += EventListViewModel_EventFavViewCallbackEvent;
void EventListViewModel_EventFavViewCallbackEvent(EventModel e)
{
var StoreFav = new EventFavoritesView { ViewModel = new EventFavoritesViewModel { ID = e.ID } };
View.Add(StoreFav.View);
}
Depending on the ID of the cell, it creates the list of the favorites by sending a request to our server.
Update:
private long _id;
public long ID
{
get { return _id; }
set { _id = value; RaisePropertyChanged(() => ID); Update(); }
}
When the data is received a RaisePropertyChanged() should make the view to reload its content.
private List<FavModel> _favs;
public List<FavModel> Favs
{
get { return _favs; }
set { _favs = value; RaisePropertyChanged(() => Favs); }
}
ViewModel:
public class EventFavoritesViewModel : MvxViewModel
{
private readonly EventFavoritesService _eventFavoriteService;
private readonly UserFavoritesService _userFavoriteService;
private long _id;
public long ID
{
get { return _id; }
set { _id = value; RaisePropertyChanged(() => ID); Update(); }
}
private string _title;
public string Title
{
get { return _title; }
set { _title = value; RaisePropertyChanged(() => Title); }
}
private List<FavModel> _favs;
public List<FavModel> Favs
{
get { return _favs; }
set { _favs = value; RaisePropertyChanged(() => Favs); }
}
private MvxCommand<FavModel> _itemSelectedCommand;
public System.Windows.Input.ICommand ItemSelectedCommand
{
get
{
_itemSelectedCommand = _itemSelectedCommand ?? new MvxCommand<FavModel>(ToggleFav);
return _itemSelectedCommand;
}
}
public void Init(long eventID)
{
MvxTrace.Trace("We get the details", Logger.Errorlevel.Debug);
ID = eventID;
}
public EventFavoritesViewModel()
{
_eventFavoriteService = new EventFavoritesService(UpdateEventFav);
_userFavoriteService = new UserFavoritesService(UpdateUserFav);
}
private void UpdateUserFav(Fav[] favlist)
{
MvxMessenger.Publish(new UserFavUpdateMessage(this, favlist));
}
private void Update()
{
Favs = _eventFavoriteService.GetFavforEvent(ID).MapToFavs();
}
private void UpdateEventFav(Fav[] favlist)
{
Favs = favlist.MapToFavs();
}
private void ToggleFav(FavModel item)
{
MvxTrace.Trace("Got Item: " + item.Display);
item.NewSubscription = !item.NewSubscription;
}
private IMvxMessenger MvxMessenger
{
get
{
return Mvx.Resolve<IMvxMessenger>();
}
}
public void SaveFavs()
{
foreach (var fav in Favs)
{
if (fav.AlreadySubscribed != fav.NewSubscription)
{
if (fav.NewSubscription)
_userFavoriteService.PutToUserFavorites(fav.MapToFav());
else
_userFavoriteService.DeleteFromUserFavorites(fav.MapToFav());
}
}
}
}
I hope this is enough information, otherwise just tell me.:-)
Thanks for any help.
My table view does not update whenever its source property is changed. The code is as follows:
ViewController:
public override void ViewDidLoad()
{
base.ViewDidLoad();
View.BackgroundColor = UIColor.White;
var table = new UITableView(new RectangleF(0, 80, Device.Width, Device.Height - 80));
Add(table);
var source = new MvxStandardTableViewSource(table, "TitleText SessionInfo");
table.Source = source;
var set = this.CreateBindingSet<JoinSessionViewController, JoinSessionViewModel>();
set.Bind(source).To(vm => vm.AvailableServers);
set.Apply();
table.ReloadData();
}
ViewModel:
private readonly INetworkSessionClient _client;
public JoinSessionViewModel(INetworkSessionClient client)
{
_client = client;
_client.ServerFound += ClientOnServerFound;
_client.BeginSearchingForServers();
}
private void ClientOnServerFound(object sender, ServerFoundEventArgs serverFoundEventArgs)
{
if (AvailableServers.Any(s => s.Identifier == serverFoundEventArgs.ServerInfo.Identifier))
return;
AvailableServers.Add(serverFoundEventArgs.ServerInfo);
RaisePropertyChanged(() => AvailableServers);
}
private List<ServerInfo> _availableServers;
public List<ServerInfo> AvailableServers
{
get { return _availableServers; }
set { _availableServers = value; RaisePropertyChanged(() => AvailableServers); }
}
This was a tricky one. The culprit is this line in MvxTableViewSource:
https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/Views/MvxTableViewSource.cs#L56
public virtual IEnumerable ItemsSource
{
get { return _itemsSource; }
set
{
if (_itemsSource == value) // **** This one ****
return;
if (_subscription != null)
{
_subscription.Dispose();
_subscription = null;
}
_itemsSource = value;
var collectionChanged = _itemsSource as INotifyCollectionChanged;
if (collectionChanged != null)
{
_subscription = collectionChanged.WeakSubscribe(CollectionChangedOnCollectionChanged);
}
ReloadTableData();
}
}
Since I was only adding to the list and not setting it to a new list, the setter was early returning and not firing ReloadTableData(). The fix was to use an ObservableCollection (or anything else that implements INotifyCollectionChanged) instead of a List.
private void ClientOnServerFound(object sender, ServerFoundEventArgs serverFoundEventArgs)
{
if (AvailableServers.Any(s => s.Identifier == serverFoundEventArgs.ServerInfo.Identifier))
return;
// CollectionChanged event is not automatically martialed to UI thread
InvokeOnMainThread(() => AvailableServers.Add(serverFoundEventArgs.ServerInfo));
}
private ObservableCollection<ServerInfo> _availableServers;
public ObservableCollection<ServerInfo> AvailableServers
{
get { return _availableServers; }
set { _availableServers = value; RaisePropertyChanged(() => AvailableServers); }
}
Note that because of my particular case, the ServerFound event is being invoked on a separate thread. That means the INotifyCollectionChanged.CollectionChanged event will also be invoked on a separate thread, so the action of adding the server to the list has to be martialed onto the main thread with InvokeOnMainThread() so that the UI will properly update.
It would be nice if CollectionChanged events would automatically be handled on the UI thread, similar to how RaisePropertyChanged() works.
How can I bind Command to recieve taps on sections of my Table? I am using MvxViewController with custom TableSource, but it seems that I cant add bindings to my VM when creating UIView for sections.
Here is my ViewModel:
public class TestViewModel : MvxViewModel
{
private ObservableCollection<string> _sections;
public ObservableCollection<string> Sections
{
get { return _sections; }
set { _sections = value; RaisePropertyChanged(() => Sections); }
}
private MvxCommand _sectionTappedCommand;
public ICommand SectionTappedCommand
{
get
{
_sectionTappedCommand = _sectionTappedCommand ?? new MvxCommand(DoSectionTappedCommand);
return _sectionTappedCommand;
}
}
private void DoSectionTappedCommand()
{
//I want this command somehow to be called when user taps section header
Debug.WriteLine("Section tapped!");
}
}
My view:
[Register("TestView")]
public class OrderView : MvxViewController
{
public override void ViewDidLoad()
{
View = new UIView() { BackgroundColor = UIColor.White };
base.ViewDidLoad();
var table = new UITableView(new RectangleF(0, 20, 320, 660));
Add(table);
var source = new TestTableSource(table);
table.Source = source;
var set = this.CreateBindingSet<OrderView, OrderViewModel>();
// I think here I need to write something like:
// set.Bind(source).For(s => s.Section.TapAction).To(vm => vm.SectionTappedCommand);**
set.Bind(source).For(s => s.ItemsSource).To(vm => vm.Sections).OneWay();
set.Apply();
}
}
Table source:
public class TestTableSource : MvxBaseTableViewSource
{
// all needed overrides implemented
private IList<OrderGuest> _sections;
public IList<OrderGuest> ItemsSource
{
get
{
return _sections;
}
set
{
_sections = value;
ReloadTableData();
}
}
public override UIView GetViewForHeader(UITableView tableView, int section)
{
// Do I need to add bindings here?
var view = new OrderGuestSectionHeader(OrderGuestSectionHeader(tableView, section), () => {
Debug.WriteLine("selected " + section.ToString());
});
return view;
}
public override int NumberOfSections(UITableView tableView)
{
if (_sections == null)
return 0;
return _sections.Count;
}
public override string[] SectionIndexTitles(UITableView tableView)
{
if (_sections == null)
return null;
return _sections.Select(x => x.Name).ToArray();
}
}
Subclassed UIView for section header:
public sealed class OrderGuestSectionHeader : UIView
{
private UIButton SectionButton;
public Action TapAction;
public OrderGuestSectionHeader(string header, Action tapped)
{
Frame = new RectangleF(0, 0, 320, 20);
BackgroundColor = UIColor.Blue;
SectionButton = new UIButton(this.Frame);
SectionButton.TouchUpInside += SectionButton_TouchUpInside;
SectionButton.Title(header);
TapAction = tapped;
Add(SectionButton);
}
private void SectionButton_TouchUpInside(object sender, EventArgs e)
{
TapAction();
}
}
There are a few ways you could achieve this effect.
For just your current requirements, the easiest way to achieve it would be to add just a single command to the TestTableSource and to have that command passed on to your section header inside the Action handler.
public class TestTableSource : MvxBaseTableViewSource
{
public ICommand FooCommand { get; set; }
// existing code
public override UIView GetViewForHeader(UITableView tableView, int section)
{
var view = new OrderGuestSectionHeader(OrderGuestSectionHeader(tableView, section), () => {
Debug.WriteLine("selected " + section.ToString());
if (FooCommand != null) FooCommand.Execute(null);
});
return view;
}
}
This Command can then be bound to the ViewModel by adding the OrderView binding:
set.Bind(source)
.For(s => s.FooCommand)
.To(vm => vm.SectionTappedCommand)
.OneWay();
If you wanted to go further - if you wanted to do a more complicated binding - then you could actually set up a full binding DataContext for the Header View. The easiest way to do this would be to inherit from MvxView. I won't go into the full detail of this here - instead for an introduction to MvxView, see the N+1 video - http://slodge.blogspot.co.uk/2013/06/n32-truth-about-viewmodels-starring.html with sample source code in https://github.com/slodge/NPlus1DaysOfMvvmCross/tree/master/N-32-ViewModels