Share via

How to use the same Element instance on different page ?

Xamarin NewBie 121 Reputation points
2021-04-07T08:48:14.227+00:00

I want to create an Android App with Xamarin.Forms.
I create an Object on the Mainpage. This object is a Collection of many other Objects.
My goal is to push this Collection-Object to another Page, and on this Page i want to add an Object to the Collection-Object. After this i want that the newly added Object is in the Collection on the MainPage.

I am Navigating between Pages with Push and Pop.
The YT Tutorials only work in one direction, so that i can see the object on the second page, but when i add an Object to the Collection, it isn't visible on the second Page.

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

Anonymous
2021-04-07T09:32:39.273+00:00

Hello,​

Welcome to our Microsoft Q&A platform!

How to use the same Element instance on different page ?

You can use static value for Collection-Object. For example, I have static ObservableCollection in the mainPage.

   public partial class MainPage : ContentPage  
       {  
     
          public static ObservableCollection<Employee> employees = new ObservableCollection<Employee>();  
           public MainPage()  
           {  
                
               InitializeComponent();  
     
               employees.Add(new Employee() { DisplayName = "test1" });  
               employees.Add(new Employee() { DisplayName = "test2" });  
               employees.Add(new Employee() { DisplayName = "test3" });  
               employees.Add(new Employee() { DisplayName = "test4" });  
               employees.Add(new Employee() { DisplayName = "test5" });  
     
               mylist.ItemsSource = employees;  
           }  
     
           private void Button_Clicked(object sender, EventArgs e)  
           {  
               Navigation.PushAsync(new Page1());  
           }  
       }  
     
       public class Employee  
       {  
           public string DisplayName { get; set; }  
       }  

MainPage's layout.

   <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
                x:Class="App81.MainPage">  
     
       <StackLayout>  
     
           <Button Text="navi" Clicked="Button_Clicked"></Button>  
           <ListView x:Name="mylist">  
               <ListView.ItemTemplate>  
                   <DataTemplate>  
                       <TextCell Text="{Binding DisplayName}" />  
                   </DataTemplate>  
               </ListView.ItemTemplate>  
           </ListView>  
       </StackLayout>  
     
   </ContentPage>  

When I navigiate to the page1, I use this static ObservableCollection in mainpage for page1‘s ItemsSource of listview, I add value to the ObservableCollection in the Button Click event.

   public partial class Page1 : ContentPage  
       {  
           public Page1()  
           {  
               InitializeComponent();  
     
               mylist.ItemsSource = MainPage.employees;  
           }  
     
           private void Button_Clicked(object sender, EventArgs e)  
           {  
               MainPage.employees.Add(new Employee() { DisplayName="ttttt" });  
           }  
       }  

Here is page1’s layout.

   <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
                x:Class="App81.Page1">  
       <ContentPage.Content>  
           <StackLayout>  
               <Button Text="add" Clicked="Button_Clicked"></Button>  
               <ListView x:Name="mylist">  
                   <ListView.ItemTemplate>  
                       <DataTemplate>  
                           <TextCell Text="{Binding DisplayName}" />  
                       </DataTemplate>  
                   </ListView.ItemTemplate>  
               </ListView>  
           </StackLayout>  
       </ContentPage.Content>  
   </ContentPage>  

Here is running screenshot.

Best Regards,

Leon Lu


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.