0

I have main page on which I have collection view. I want to navigate to next page after I click submit button .I am able to navigate to the next page but I also want total list of items which I have in my collection how can I achieve that?

1 Answers1

0

I don't know the detail of your code, but you can try to pass the total list of items as the parameter of the next page.(suppose it's name is SecondPage)

You can refer to the following code:

MainPage.cs

public partial class MainPage : ContentPage 
{
    MyViewModel myViewModel;
    public MainPage()
      {
            InitializeComponent();

        myViewModel = new MyViewModel();    
        BindingContext = myViewModel;
    }

      private    void mCollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
        string previous = (e.PreviousSelection.FirstOrDefault() as MyModel)?.Name;
        string current = (e.CurrentSelection.FirstOrDefault() as MyModel)?.Name;
    }

    private async void Button_Clicked(object sender, EventArgs e)
    {
        // here we can pass the data we need.
        var secondPage = new SecondPage(myViewModel.Data);

        await Navigation.PushAsync(secondPage);
    }
}

MainPage.xaml.cs

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiCollectionApp.MainPage"
             xmlns:local="clr-namespace:MauiCollectionApp"
             x:Name="myPage"
             >

    <VerticalStackLayout>
        <CollectionView  ItemsSource="{ Binding Data}" x:Name="mCollectionView" 
                    SelectionChanged="mCollectionView_SelectionChanged"
                    SelectionMode="Single"
                    >
            <CollectionView.ItemTemplate>
                <DataTemplate>

                    <HorizontalStackLayout  Margin="3" >
                        <Label Text="{Binding Name}" BackgroundColor="Gray"/>
                        <Label Text="{Binding Car.Make}" Margin="5,0,5,0" />

                        <Button Text="delete"  Margin="10,0,0,0"
                                BackgroundColor="Red"
                                Command="{Binding  Path= BindingContext.RemoveEquipmentCommand,Source={Reference mCollectionView }}"  CommandParameter="{Binding .}"  
                                 />
                    </HorizontalStackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>


        <Button Text="submit" Clicked="Button_Clicked" Margin="10"/>

    </VerticalStackLayout>

</ContentPage>

MyViewModel.cs

public class MyViewModel: INotifyPropertyChanged 
    {
        public  ObservableCollection<MyModel> Data { get; set; }

        public ICommand RemoveEquipmentCommand => new Command<MyModel>(ReMoveItem);

        private void ReMoveItem(MyModel obj)
        {
            System.Diagnostics.Debug.WriteLine(" the selected item's name  is:  " + obj.Name   );

            Data.Remove(obj);
        }

        public MyViewModel() {
            Data = new ObservableCollection<MyModel>();

            Data.Add(new MyModel { Name ="model_1", Car= new Vehicle {Make="Make1" } });
            Data.Add(new MyModel { Name = "model_2", Car = new Vehicle { Make = "Make2" } });
            Data.Add(new MyModel { Name = "model_3", Car = new Vehicle { Make = "Make3" } });
            Data.Add(new MyModel { Name = "model_4", Car = new Vehicle { Make = "Make4" } });
        }

        bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Object.Equals(storage, value))
                return false;

            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

SecondPage.cs

public partial class SecondPage : ContentPage 
{
    public ObservableCollection<MyModel> Items { get; set; }
    public SecondPage(ObservableCollection<MyModel> data )
      {     // we can get the passed data here
            InitializeComponent();

            this.Items = data;
      }
}
Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19
  • Thanks for the answer @Jessie, but I want to pick some files using file picker and then want to populate that files in collection view. Once I click next then I want to navigate to the next page with all the files I have selected. I don't want to set data manually in view model – Preeti Jadhav Oct 28 '22 at 10:34
  • `but I want to pick some files using file picker and then want to populate that files in collection view.` This is another problem. One problem, one thread. For the new problem, you can create a new problem. We are all willing to help you. :) – Jessie Zhang -MSFT Oct 31 '22 at 08:32
  • Hi @PreetiJadhav, could you please mark my reply as answered so that it will help others who have similar problems? Thanks in advance. :) – Jessie Zhang -MSFT Nov 02 '22 at 04:41