locked
Update proper Viewmodel over view code RRS feed

  • Question

  • User394824 posted

    Hallo,

    Need some help on this:

    I need to add data from the view Code page( XAML.cs), because of some Problems in Android, I can't use bindings for the map polylines, but I need to update the ObservableCollection in the viewmodel. I see that the data passes the Viewmodel, Setting a debug Point, also when I call the ObservableCollection again from the Code page, the data is there, but when I call the ObservableCollection than inside the ViewModel, the data-count saying Zero.

    What O'm doing wrong here or missing:

    ViewModel:

        private ObservableCollection<GPXTrack> _gpxTrack;
        public ObservableCollection<GPXTrack> GPXTrack
        {
            get { return _gpxTrack; }
            set { _gpxTrack = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("GPXTrack")); }
        }
    

    Adding the Data from the Code View Page (XAML.cs):

            ((TrackViewModel)this.BindingContext).GPXTrack.Add(new GPXTrack()
                        {
                            Latitude = invariantCultureLatitude,
                            Longitude = invariantCultureLongitude,
                            Elevation = invariantCultureElevation,
                            Distance = distanceTotal
                        });
    

    Thanks,

    Markus

    Thursday, October 8, 2020 1:14 PM

All replies

  • User378073 posted

    I need to add data from the view Code page( XAML.cs), because of some Problems in Android,

    Its called code behind

    Thursday, October 8, 2020 1:16 PM
  • User371688 posted

    Hi @antero , I made a simple demo according to your code, it works properly.

    MyViewModel.cs

    ``` public class MyViewModel : INotifyPropertyChanged {

        ObservableCollection<VeggieViewModel> _gpxTrack;
        public ObservableCollection<VeggieViewModel> GPXTrack
        {
            get { return _gpxTrack; }
            set { _gpxTrack = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("GPXTrack")); }
        }
    
       public  MyViewModel() {
            GPXTrack = new ObservableCollection<VeggieViewModel>();
            GPXTrack.Add(new VeggieViewModel { Name = "Tomato", Type = "Fruit", Image = "tomato.png", Price = "10" });
            GPXTrack.Add(new VeggieViewModel { Name = "Romaine Lettuce", Type = "Vegetable", Image = "lettuce.png", Price = "8" });
            GPXTrack.Add(new VeggieViewModel { Name = "Zucchini", Type = "Vegetable", Image = "zucchini.png", Price = "66" });
    
        }
    
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        void onPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    ```

    The MyViewXaml.xaml.cs

    ``` private void Button_Clicked(object sender, System.EventArgs e) { ((MyViewModel)this.BindingContext).GPXTrack.Add(new VeggieViewModel() { Name = "Squirrel Monkey", Type = " Type3", Price = "20", Image = "icon.png" }); ; ; }

    ```

    Friday, October 9, 2020 8:21 AM
  • User394824 posted

    Hi jezh,

    it works, till I bind a Button Command over Viewmodel and try to run the code in Android and try to add polylines to a xmarin.forms.map

    I think the CustomMap I use and binding the polylines, is Messing up things. It will add the ployline, takes for ever, but than you have other things not workinf correctly, like the Question above

    Thanks,

    Makrus

    Friday, October 9, 2020 8:31 AM
  • User371688 posted

    it works, till I bind a Button Command over Viewmodel and try to run the code in Android and try to add polylines to a xmarin.forms.map Hi @antero , how can we reproduce your question?

    Friday, October 9, 2020 9:53 AM
  • User394824 posted

    Hi jezh,

    I think it is the same iusse like under my other post:

    https://forums.xamarin.com/discussion/185470/i-dont-get-it-andorid-is-just#latest

    Thanks,

    Markus

    Friday, October 9, 2020 9:58 AM
  • User371688 posted

    Then we can talk about this question in another thread: https://forums.xamarin.com/discussion/185470/i-dont-get-it-andorid-is-just#latest

    Monday, October 12, 2020 6:57 AM