Forum Cross Platform with Xamarin
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Update proper Viewmodel over view code

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

Answers

  • stXamDevstXamDev Member ✭✭✭

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

    Its called code behind

  • jezhjezh Member, Xamarin Team Xamurai

    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"
                }); ; ;
            }
    
    
  • anteroantero Member ✭✭

    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

  • jezhjezh Member, Xamarin Team Xamurai

    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?

  • anteroantero Member ✭✭

    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

  • jezhjezh Member, Xamarin Team Xamurai

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

Sign In or Register to comment.