question

Grime avatar image
0 Votes"
Grime asked Grime commented

I want to pass data in my app from page 3 to both page 2 and page 1

My app's opening page, MainPage (1), uses the OperatorButton to open OperatorsPage (3), from where you can select an Operator from a ListView in the SelectOperatorPage (4).
MainPage (1) can also open MenuPage (2) through a hamburger button where that page can also access OperatorsPage (3).
My query is how do I get the selected model from (4) back to be displayed on (1) using Navigation.PopModalAsync?

Code follows: MainPage.xaml

 <?xml version="1.0" encoding="utf-8" ?>
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              BackgroundColor="White"
              Title="Hands Free Notes"
              x:Class="HandsFreeNotes.MainPage">
    
     <Grid BackgroundColor="White">
         <Grid.RowDefinitions>
             <RowDefinition Height="40"/>
             <RowDefinition Height="40"/>
             <RowDefinition Height="80"/>
             <RowDefinition Height="*"/>
             <RowDefinition Height="50"/>
         </Grid.RowDefinitions>
    
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="50"/>
             <ColumnDefinition Width="*"/>
             <ColumnDefinition Width="50"/>
         </Grid.ColumnDefinitions>
    
         <Button
             x:Name="AboutButton"
             Text="About"
             TextColor="Black"
             FontSize="Medium"
             FontAttributes="Bold"
             Grid.Row="0"
             Grid.Column="0"
             Grid.ColumnSpan="2"
             Margin="20, 0, 0, 0"
             VerticalOptions="Center"
             HorizontalOptions="Start"
             Clicked="AboutButton_Clicked"/>
    
         <Button
             x:Name="MenuButton"
             ImageSource="hbb32"
             Grid.Row="0"
             Grid.Column="1"
             Grid.ColumnSpan="2"
             Margin="0, 0, 20, 0"
             VerticalOptions="Center"
             HorizontalOptions="End"
             BackgroundColor="Transparent"
             Clicked="MenuButton_Clicked"/>
    
         <Image 
             Source="hfn256"
             Margin="0, 0, 0, 0"
             Grid.Row="1" 
             Grid.Column="0"
             Grid.ColumnSpan="3"
             HorizontalOptions="Center"
             VerticalOptions="Center"
             BackgroundColor="Transparent"/>
    
         <Label
             Text="Home"
             TextColor="Black"
             Grid.Row="2"
             Grid.Column="1"
             FontSize="24"
             FontAttributes="Bold"
             HorizontalOptions="Center"
             VerticalOptions="Center"/>
    
         <ScrollView Orientation="Vertical"
             Grid.Row="3" Grid.Column="0"
             Grid.ColumnSpan="3">
             <Grid BackgroundColor="White" >
                 <Grid.RowDefinitions>
                     <RowDefinition Height="30"/>
                     <RowDefinition Height="60"/>
                     <RowDefinition Height="60"/>
                     <RowDefinition Height="80"/>
                     <RowDefinition Height="60"/>
                     <RowDefinition Height="80"/>
                     <RowDefinition Height="80"/>
                     <RowDefinition Height="60"/>
                     <RowDefinition Height="60"/>
                     <RowDefinition Height="60"/>
                     <RowDefinition Height="60"/>
                     <RowDefinition Height="60"/>
                     <RowDefinition Height="60"/>
                     <RowDefinition Height="*"/>
                 </Grid.RowDefinitions>
    
                 <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="150"/>
                     <ColumnDefinition Width="*"/>
                     <ColumnDefinition Width="*"/>
                     <ColumnDefinition Width="10"/>
                 </Grid.ColumnDefinitions>
    
                 <Label
                     x:Name="OperatorLabel"
                     Text="Operator:"
                     TextColor="Black"
                     FontAttributes="Bold"
                     Grid.Row="0"
                     Grid.Column="0"
                     BackgroundColor="Transparent"
                     Margin="10,0,0,0"
                     HorizontalOptions="End"
                     VerticalOptions="Center"/>
    
                 <Button
                     x:Name="OperatorButton"
                     Text="Click to SET Operator"
                     Grid.Row="0"
                     Grid.Column="1"
                     Grid.ColumnSpan="2"
                     TextColor="Gray"
                     FontAttributes="Italic"
                     BackgroundColor="White"
                     Margin="10,0,0,0"
                     Clicked="OperatorButton_Clicked"
                     VerticalOptions="Center"/>
    
                 <Label
                     x:Name="RecipientLabel"
                     Text="Recipient:"
                     TextColor="Black"
                     FontAttributes="Bold"
                     Grid.Row="1"
                     Grid.Column="0"
                     BackgroundColor="Transparent"
                     Margin="10,20,0,0"
                     HorizontalOptions="End"
                     VerticalOptions="Center"/>
    
                 <Button
                     x:Name="RecipientButton"
                     Text="Click to SET Recipient"
                     Grid.Row="1"
                     Grid.Column="1"
                     Grid.ColumnSpan="2"
                     TextColor="Gray"
                     FontAttributes="Italic"
                     Margin="10,20,0,0"
                     BackgroundColor="White"
                     VerticalOptions="Center"/>
 <!--
                 <Entry
                     x:Name="RecipientEntry"
                     Grid.Row="1"
                     Grid.Column="1"
                     Grid.ColumnSpan="2"
                     TextColor="DarkBlue"
                     Placeholder="Click to set"
                     PlaceholderColor="Gray"
                     FontAttributes="Italic"
                     FontSize="18"
                     HorizontalOptions="Center"
                     VerticalOptions="Center"/> -->
    
                 <Label
                     x:Name="SubjectLabel"
                     Text="Subject:"
                     TextColor="Black"
                     FontAttributes="Bold"
                     Grid.Row="2"
                     Grid.Column="0"
                     BackgroundColor="Transparent"
                     Margin="10,20,0,0"
                     HorizontalOptions="End"
                     VerticalOptions="Center"/>
    
                 <Button
                     x:Name="SubjectButton"
                     Text="Click to SET Subject"
                     Grid.Row="2"
                     Grid.Column="1"
                     Grid.ColumnSpan="2"
                     TextColor="Gray"
                     FontAttributes="Italic"
                     Margin="10,20,0,0"
                     BackgroundColor="White"
                     VerticalOptions="Center"/>
    
                 <Label
                     x:Name="InstanceStartLabel"
                     Text="Instance&#x0a;      Start:"
                     TextColor="Black"
                     FontAttributes="Bold"
                     Grid.Row="3"
                     Grid.Column="0"
                     BackgroundColor="Transparent"
                     Margin="10,20,0,0"
                     HorizontalOptions="End"
                     VerticalOptions="Center"/>
    
                 <Button
                     x:Name="InstanceStartButton"
                     Text="Click to START Instance"
                     Grid.Row="3"
                     Grid.Column="1"
                     Grid.ColumnSpan="2"
                     TextColor="Gray"
                     FontAttributes="Italic"
                     Margin="10,20,0,0"
                     BackgroundColor="White"
                     VerticalOptions="Center"/>
    
                 <Label
                     x:Name="AudioLabel"
                     Text="Audio:"
                     TextColor="Black"
                     FontAttributes="Bold"
                     Grid.Row="4"
                     Grid.Column="0"
                     BackgroundColor="Transparent"
                     Margin="10,20,0,0"
                     HorizontalOptions="End"
                     VerticalOptions="Center"/>
    
                 <Button
                     x:Name="AudioButton"
                     Text="Click to RECORD Audio Notes"
                     Grid.Row="4"
                     Grid.Column="1"
                     Grid.ColumnSpan="2"
                     TextColor="Gray"
                     FontAttributes="Italic"
                     Margin="10,20,0,0"
                     BackgroundColor="White"
                     VerticalOptions="Center"/>
    
                 <Label
                     x:Name="MediaLabel"
                     Text="Photos&#x0a;&amp; Video:"
                     TextColor="Black"
                     FontAttributes="Bold"
                     Grid.Row="5"
                     Grid.Column="0"
                     BackgroundColor="Transparent"
                     Margin="10,20,0,0"
                     HorizontalOptions="End"
                     VerticalOptions="Center"/>
    
                 <Button
                     x:Name="MediaButton"
                     Text="Click to SELECT Media"
                     Grid.Row="5"
                     Grid.Column="1"
                     Grid.ColumnSpan="2"
                     TextColor="Gray"
                     FontAttributes="Italic"
                     Margin="10,20,0,0"
                     BackgroundColor="White"
                     VerticalOptions="Center"/>
    
                 <Label
                     x:Name="InstanceStopLabel"
                     Text="Instance&#x0a;       Stop:"
                     TextColor="Black"
                     FontAttributes="Bold"
                     Grid.Row="6"
                     Grid.Column="0"
                     BackgroundColor="Transparent"
                     Margin="10,20,0,0"
                     HorizontalOptions="End"
                     VerticalOptions="Center"/>
    
                 <Button
                     x:Name="InstanceStopButton"
                     Text="Click to STOP Instance"
                     Grid.Row="6"
                     Grid.Column="1"
                     Grid.ColumnSpan="2"
                     TextColor="Gray"
                     FontAttributes="Italic"
                     Margin="10,20,0,0"
                     BackgroundColor="White"
                     VerticalOptions="Center"/>
    
 <!--                <Label
                     x:Name="LatitudeLabel"
                     Text="Latitude:"
                     TextColor="Black"
                     FontAttributes="Bold"
                     Grid.Row="8"
                     Grid.Column="0"
                     BackgroundColor="Transparent"
                     Margin="10,0,0,0"
                     HorizontalOptions="End"
                     VerticalOptions="Center"/>
    
                 <Button
                     x:Name="LatitudeButton"
                     Text="Waiting..."
                     Grid.Row="8"
                     Grid.Column="1"
                     Grid.ColumnSpan="2"
                     TextColor="Gray"
                     FontAttributes="Italic"
                     Margin="10,0,0,0"
                     BackgroundColor="White"
                     VerticalOptions="Center"/>
    
                 <Label
                     x:Name="LongitudeLabel"
                     Text="Longitude:"
                     TextColor="Black"
                     FontAttributes="Bold"
                     Grid.Row="9"
                     Grid.Column="0"
                     BackgroundColor="Transparent"
                     Margin="10,0,0,0"
                     HorizontalOptions="End"
                     VerticalOptions="Center"/>
    
                 <Button
                     x:Name="LongitudeButton"
                     Text="Waiting..."
                     Grid.Row="9"
                     Grid.Column="1"
                     Grid.ColumnSpan="2"
                     TextColor="Gray"
                     FontAttributes="Italic"
                     Margin="10,0,0,0"
                     BackgroundColor="White"
                     VerticalOptions="Center"/> -->
    
                 <Button
                     x:Name="SendButton"
                     Text="Send Instance Now"
                     FontSize="Large"
                     FontAttributes="Bold"
                     Grid.Row="8"
                     Grid.Column="0"
                     Grid.ColumnSpan="3"
                     TextColor="Black"
                     Margin="10,0,0,0"
                     BackgroundColor="White"
                     VerticalOptions="Center"/>
    
             </Grid>
    
         </ScrollView>
    
     </Grid>
 </ContentPage>

MainPage.xaml.cs

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using Xamarin.Forms;
 using SQLite;
 using HandsFreeNotes.Model;
 using HandsFreeNotes.View;
 using HandsFreeNotes.ViewModel;
    
 namespace HandsFreeNotes
 {
     public partial class MainPage : ContentPage
     {
         OperatorViewModel ovm;
    
         public MainPage()
         {
             // add a bit of padding to cater to the "notch" on the iPhone.
             if (Device.RuntimePlatform == Device.iOS)
             {
                 Padding = new Thickness(0, 40, 0, 0);
             }
    
             InitializeComponent();
    
             ovm = new OperatorViewModel();
    
             BindingContext = ovm;
         }
    
         //protected override async void OnAppearing()
         //{
         //    base.OnAppearing();
         //    await RefreshOperatorListView();
         //}
    
         //public async Task<bool> RefreshOperatorListView()
         //{
         //    OperatorListView.ItemSource = null;
         //    await ovm.GetOperators()
         //    OperatorListView.ItemSource = ovm.OperatorList;
         //    return true;
         //}
    
         private async void OperatorButton_Clicked(object sender, EventArgs e)
         {
             await Navigation.PushModalAsync(new OperatorsPage());
         }
    
         private async void AboutButton_Clicked(object sender, EventArgs e)
         {
             await Navigation.PushModalAsync(new AboutPage());
         }
    
         private async void MenuButton_Clicked(object sender, EventArgs e)
         {
             await Navigation.PushModalAsync(new MenuPage());
         }
     }
 }

MenuPage.xaml

 <?xml version="1.0" encoding="utf-8" ?>
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="HandsFreeNotes.View.MenuPage">
     <ContentPage.Content>
         <Grid BackgroundColor="White">
             <Grid.RowDefinitions>
                 <RowDefinition Height="40"/>
                 <RowDefinition Height="40"/>
                 <RowDefinition Height="60"/>
                 <RowDefinition Height="*"/>
                 <RowDefinition Height="50"/>
             </Grid.RowDefinitions>
    
             <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="50"/>
                 <ColumnDefinition Width="*"/>
                 <ColumnDefinition Width="50"/>
             </Grid.ColumnDefinitions>
    
             <Button
                 x:Name="BackButton"
                 Text="Back"
                 Grid.Row="0"
                 Grid.Column="0"
                 Grid.ColumnSpan="3"
                 TextColor="Black"
                 FontAttributes="Bold"
                 HeightRequest="40"
                 VerticalOptions="Center"
                 HorizontalOptions="End"
                 Margin="0,0,20,0"
                 Clicked="BackButton_Clicked"/>
    
             <Image 
                 Source="hfn256"
                 Margin="2"
                 Grid.Row="1" 
                 Grid.Column="0"
                 Grid.ColumnSpan="3"
                 HorizontalOptions="Center"
                 BackgroundColor="Transparent"/>
    
             <Label 
                 Text="Menu"
                 FontSize="Large"
                 TextColor="Black"
                 FontAttributes="Bold"
                 Margin =" 0, 0, 0, 0"
                 Grid.Row="2" 
                 Grid.Column="0"
                 Grid.ColumnSpan="3"
                 HorizontalOptions="Center"
                 VerticalOptions="Center"
                 BackgroundColor="Transparent"/>
    
             <ScrollView 
                 Orientation="Vertical"
                 Grid.Row="3" 
                 Grid.Column="0"
                 Grid.ColumnSpan="3">
                 <Grid BackgroundColor="White" >
                     <Grid.RowDefinitions>
                         <RowDefinition Height="50"/>
                         <RowDefinition Height="50"/>
                         <RowDefinition Height="50"/>
                         <RowDefinition Height="50"/>
                         <RowDefinition Height="50"/>
                         <RowDefinition Height="50"/>
                     </Grid.RowDefinitions>
    
                     <Grid.ColumnDefinitions>
                         <ColumnDefinition Width="50"/>
                         <ColumnDefinition Width="*"/>
                         <ColumnDefinition Width="50"/>
                     </Grid.ColumnDefinitions>
    
                     <Button
                         x:Name="ManageOperatorsButton"
                         Text="Manage Operators"
                         FontSize="Medium"
                         Grid.Row="0"
                         Grid.Column="0"
                         Grid.ColumnSpan="3"
                         TextColor="Black"
                         FontAttributes="Bold"
                         BackgroundColor="White"
                         Margin="0,20,0,0"
                         HorizontalOptions="Center"
                         VerticalOptions="Start"
                         Clicked="ManageOperatorsButton_Clicked"/>
    
                     <Button
                         x:Name="ManageRecipientsButton"
                         Text="Manage Recipients"
                         FontSize="Medium"
                         Grid.Row="1"
                         Grid.Column="0"
                         Grid.ColumnSpan="3"
                         TextColor="Black"
                         FontAttributes="Bold"
                         BackgroundColor="White"
                         Margin="0,20,0,0"
                         HorizontalOptions="Center"
                         VerticalOptions="Start"/>
    
                     <Button
                         x:Name="ManageSubjectsButton"
                         Text="Manage Subjects"
                         FontSize="Medium"
                         Grid.Row="2"
                         Grid.Column="0"
                         Grid.ColumnSpan="3"
                         TextColor="Black"
                         FontAttributes="Bold"
                         BackgroundColor="White"
                         Margin="0,20,0,0"
                         HorizontalOptions="Center"
                         VerticalOptions="Start"/>
    
                     <Button
                         x:Name="ManageInstancesButton"
                         Text="Manage Instances"
                         FontSize="Medium"
                         Grid.Row="3"
                         Grid.Column="0"
                         Grid.ColumnSpan="3"
                         TextColor="Black"
                         FontAttributes="Bold"
                         BackgroundColor="White"
                         Margin="0,20,0,0"
                         HorizontalOptions="Center"
                         VerticalOptions="Start"/>
    
                     <Button
                         x:Name="ManageAudioRecordingsButton"
                         Text="Manage Audio Recordings"
                         FontSize="Medium"
                         Grid.Row="4"
                         Grid.Column="0"
                         Grid.ColumnSpan="3"
                         TextColor="Black"
                         FontAttributes="Bold"
                         BackgroundColor="White"
                         Margin="0,20,0,0"
                         HorizontalOptions="Center"
                         VerticalOptions="Start"/>
    
                 </Grid>
    
             </ScrollView>
    
         </Grid>    </ContentPage.Content>
 </ContentPage>

MenuPage.xaml.cs

 using HandsFreeNotes.ViewModel;
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
    
 using Xamarin.Forms;
 using Xamarin.Forms.Xaml;
    
 namespace HandsFreeNotes.View
 {
     [XamlCompilation(XamlCompilationOptions.Compile)]
     public partial class MenuPage : ContentPage
     {
    
         OperatorViewModel ovm;
    
         public MenuPage()
         {
             // add a bit of padding to cater to the "notch" on the iPhone.
             if (Device.RuntimePlatform == Device.iOS)
             {
                 Padding = new Thickness(0, 40, 0, 0);
             }
    
             InitializeComponent();
    
             ovm = new OperatorViewModel();
             BindingContext = ovm;
         }
    
         private async void BackButton_Clicked(object sender, EventArgs e)
         {
             await Navigation.PopModalAsync();
         }
    
         private async void ManageOperatorsButton_Clicked(object sender, EventArgs e)
         {
             await Navigation.PushModalAsync(new OperatorsPage());
         }
     }
 }

OperatorsPage.xaml

 <?xml version="1.0" encoding="utf-8" ?>
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="HandsFreeNotes.View.OperatorsPage">
     <ContentPage.Content>
         <Grid BackgroundColor="White">
             <Grid.RowDefinitions>
                 <RowDefinition Height="40"/>
                 <RowDefinition Height="40"/>
                 <RowDefinition Height="40"/>
                 <RowDefinition Height="70"/>
                 <RowDefinition Height="*"/>
                 <RowDefinition Height="50"/>
             </Grid.RowDefinitions>
    
             <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="50"/>
                 <ColumnDefinition Width="*"/>
                 <ColumnDefinition Width="50"/>
             </Grid.ColumnDefinitions>
    
             <Button
                 x:Name="BackButton"
                 Text="Back"
                 Grid.Row="0"
                 Grid.Column="0"
                 Grid.ColumnSpan="3"
                 TextColor="Black"
                 FontAttributes="Bold"
                 HeightRequest="40"
                 VerticalOptions="Center"
                 HorizontalOptions="End"
                 Margin="0,0,20,0"
                 Clicked="BackButton_Clicked"/>
    
             <Image 
                 Source="hfn256"
                 Margin="2"
                 Grid.Row="1" 
                 Grid.Column="0"
                 Grid.ColumnSpan="3"
                 HorizontalOptions="Center"
                 BackgroundColor="Transparent"/>
    
             <Label 
                 Text="Operator"
                 FontSize="Large"
                 TextColor="Black"
                 FontAttributes="Bold"
                 Margin =" 0, 0, 0, 0"
                 Grid.Row="2" 
                 Grid.Column="0"
                 Grid.ColumnSpan="3"
                 HorizontalOptions="Center"
                 VerticalOptions="End"
                 BackgroundColor="Transparent"/>
    
             <StackLayout
                 Grid.Row="3" 
                 Grid.Column="0"
                 Grid.ColumnSpan="3"
                 Orientation="Horizontal">
    
                 <Label
                     Text="Selected&#x0a;Operator: "
                     TextColor="Black"
                     FontSize="Medium"
                     FontAttributes="Bold"
                     HorizontalOptions="End"
                     VerticalOptions="Center"
                     BackgroundColor="Transparent"
                     Margin="40, 0, 0, 0"
                     WidthRequest="250"/>
    
                 <Entry 
                     x:Name="SelectedOperatorEntry"
                     Text="{Binding OperatorName}"
                     TextColor="Green"
                     IsReadOnly="True"
                     Placeholder="No Currently Selected Operator"
                     PlaceholderColor="LightGray"
                     FontSize="Medium"
                     FontAttributes="Italic"
                     Margin ="0, 0, 0, 0"
                     HorizontalOptions="Center"
                     VerticalOptions="Center"
                     BackgroundColor="Transparent"
                     WidthRequest="280"/>
    
             </StackLayout>
    
             <ScrollView 
                 Orientation="Vertical"
                 Grid.Row="4" 
                 Grid.Column="0"
                 Grid.ColumnSpan="3">
                 <Grid BackgroundColor="White" >
                     <Grid.RowDefinitions>
                         <RowDefinition Height="50"/>
                         <RowDefinition Height="50"/>
                         <RowDefinition Height="50"/>
                         <RowDefinition Height="100"/>
                         <RowDefinition Height="20"/>
                     </Grid.RowDefinitions>
    
                     <Grid.ColumnDefinitions>
                         <ColumnDefinition Width="50"/>
                         <ColumnDefinition Width="*"/>
                         <ColumnDefinition Width="50"/>
                     </Grid.ColumnDefinitions>
    
                     <Button
                         x:Name="SelectOperatorButton"
                         Text="Select Operator"
                         FontSize="Medium"
                         Grid.Row="0"
                         Grid.Column="1"
                         TextColor="Black"
                         FontAttributes="Bold"
                         BackgroundColor="White"
                         Margin="0,0,0,0"
                         HorizontalOptions="Center"
                         VerticalOptions="Start"
                         Clicked="SelectOperatorButton_Clicked"/>
    
                     <Button
                         x:Name="UpdateOperatorButton"
                         Text="Update Selected Operator"
                         FontSize="Medium"
                         Grid.Row="1"
                         Grid.Column="1"
                         TextColor="Black"
                         FontAttributes="Bold"
                         BackgroundColor="White"
                         Margin="0,0,0,0"
                         HorizontalOptions="Center"
                         VerticalOptions="Start"
                         Clicked="UpdateOperatorButton_Clicked"/>
    
                     <Button
                         x:Name="DeleteOperatorButton"
                         Text="Delete Selected Operator"
                         FontSize="Medium"
                         Grid.Row="2"
                         Grid.Column="1"
                         TextColor="Black"
                         FontAttributes="Bold"
                         BackgroundColor="White"
                         Margin="0,0,0,0"
                         HorizontalOptions="Center"
                         VerticalOptions="Start"
                         Clicked="DeleteOperatorButton_Clicked"/>
    
                     <Button
                         x:Name="AddOperatorButton"
                         Text="Add New Operator"
                         FontSize="Medium"
                         Grid.Row="3"
                         Grid.Column="1"
                         HorizontalOptions="Center"
                         VerticalOptions="End"
                         TextColor="Black"
                         FontAttributes="Bold"
                         BackgroundColor="White"
                         Margin="0,0,0,0"
                         Clicked="AddOperatorButton_Clicked"/>
    
                 </Grid>
    
             </ScrollView>
    
         </Grid>
     </ContentPage.Content>
 </ContentPage>

OperatorsPage.xaml.cs

using HandsFreeNotes.ViewModel;
using HandsFreeNotes.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace HandsFreeNotes.View
{
[XamlCompilation(XamlCompilationOptions.Compile)]

 public partial class OperatorsPage : ContentPage
 {
     OperatorViewModel ovm;
     OperatorModel opm;

     // public OperatorsPage(OperatorViewModel _ovm)
     // The line above changed to the line below at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021
     public OperatorsPage()
     {
         // add a bit of padding to cater to the "notch" on the iPhone.
         if (Device.RuntimePlatform == Device.iOS) { Padding = new Thickness(0, 40, 0, 0); }

         InitializeComponent();

         // ovm = _ovm;
         // opm = ovm.SelectedOperator;
         // The 2 lines above changed to the line below at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021
         opm = new OperatorModel();

         BindingContext = opm;
     }

     private async void BackButton_Clicked(object sender, EventArgs e)
     {
         await Navigation.PopModalAsync();
         // The line above originally commented out at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021
     }

     private async void AddOperatorButton_Clicked(object sender, EventArgs e)
     {
         await Navigation.PushModalAsync(new NewOperatorPage());
         // The line above originally commented out at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021

     }

     private async void SelectOperatorButton_Clicked(object sender, EventArgs e)
     {
         // The lines below added at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021
         SelectOperatorPage page = new SelectOperatorPage();
         page.ReturnValue += delegate (object s, OperatorModel operatorModel)
         {
             BackCall(s, operatorModel);
         };

         await Navigation.PushModalAsync(page);
     }
     // The line below added at the behest of LeonLu-MSFT (Microsoft Q&A) 06/05/2021
     OperatorModel SelectModel;


     // The lines below added at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021
     private void BackCall(object s, OperatorModel model)
     {
         SelectModel = model;
         opm.OperatorName = model.OperatorName;
     }

     // The lines below in UpdateOperatorButton_Clicke added at the behest of LeonLu-MSFT (Microsoft Q&A) 11/05/2021
     private async void UpdateOperatorButton_Clicked(object sender, EventArgs e)
     {

         if (SelectModel != null)
         {
             var operatorInfoPage = new OperatorInfoPage(SelectModel);
             operatorInfoPage.ReturnValue += delegate (object s, OperatorModel operatorModel)
             {
                 BackCall(s, operatorModel);
             };
             await Navigation.PushModalAsync(operatorInfoPage);
         }
         else
         {
             await DisplayAlert("Info", "Please select an Operator", "OK");
         }
     }

     // The lines included below in the DeleteOperatorButton_Clicked added at the behest of LeonLu-MSFT (Microsoft Q&A) 06/05/2021
     private async void DeleteOperatorButton_Clicked(object sender, EventArgs e)
     {
         if (SelectModel != null)
         {
             bool answer = await DisplayAlert("Warning!", "Do you really want to delete Operator " + SelectModel.OperatorName, "Yes", "No");

             if (answer == true)
             {
                 OperatorViewModel.OperatorList.Remove(SelectModel);
                 SelectModel = null;
                 SelectedOperatorEntry.Text = "";
             }
         }
         else
         {
             await DisplayAlert("Info", "Please select an Operator to delete", "OK");
         }
     }
 }

}

OperatorModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using SQLite;

namespace HandsFreeNotes.Model
{
// public class OperatorModel
// The line above changed to the line below at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021
public class OperatorModel: INotifyPropertyChanged
{
public int OperatorID { get; set; }
// public string OperatorName { get; set; }
// The 1 line above changed to the 6 lines below at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021
string _operatorName;
public string OperatorName
{
set { SetProperty(ref _operatorName, value); }
get { return _operatorName; }
}

     public string OperatorPhone { get; set; }
     public string OperatorEmail { get; set; }
     public string OperatorAvatar { get; set; }

     // The 4 lines below added at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021
     public OperatorModel()
     {

     }

     public OperatorModel(int operatorID, string operatorName, string operatorPhone, string operatorEmail, string operatorAvatar)
     {
         OperatorID = operatorID;
         OperatorName = operatorName;
         OperatorPhone = operatorPhone;
         OperatorEmail = operatorEmail;
         OperatorAvatar = operatorAvatar;
     }
     // All the lines below added at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021
     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;
 }


}

OperatorViewModel.cs

 using HandsFreeNotes.Model;
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.ComponentModel;
 using System.Text;
 using System.Threading.Tasks;
 using Xamarin.Forms;
    
 namespace HandsFreeNotes.ViewModel
 {
     public class OperatorViewModel : INotifyPropertyChanged
     {
         private string _HFNName;
    
         public string HFNName
         {
             get
             {
                 return _HFNName;
             }
                
             set
             {
                 _HFNName = value;
                 RaisePropertyChanged("HFNName");
             }
         }
    
         // public List<OperatorModel> OperatorList;
         // The line above replaced by the 5 lines below at the behest of LeonLu-MSFT (Microsoft Q&A) 06/05/2021
         public OperatorViewModel()
         {
             GetOperators();
         }
         public static ObservableCollection<OperatorModel> OperatorList;
    
    
         public OperatorModel SelectedOperator;
    
         public async Task<bool> GetOperators()
         {
             if (OperatorList == null)
             {
                 // OperatorList = new List<OperatorModel>();
                 // The line above replaced by the line below at the behest of LeonLu-MSFT (Microsoft Q&A) 06/05/2021
                 OperatorList = new ObservableCollection<OperatorModel>();
    
                 OperatorList.Add(new OperatorModel(1, "Grime", "+61419659866", "apps@grime.net", "gs300.png"));
                 OperatorList.Add(new OperatorModel(2, "Brookey", "+61419659866", "brookey@grime.net", "gmb.jpg"));
                 OperatorList.Add(new OperatorModel(3, "CC", "+61422192456", "shaunachick@gmail.com", "cc.jpg"));
                 OperatorList.Add(new OperatorModel(4, "Monte", "+61412595878", "monte@aussieweb.com.au", "monte2a.jpg"));
                 OperatorList.Add(new OperatorModel(5, "Jeff", "+61409998197", "jeff@content2convert.com.au", "jeff.jpg"));
             }
    
             return true;
         }
    
         public event PropertyChangedEventHandler PropertyChanged;
    
         protected void RaisePropertyChanged(string prop)
         {
             if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
         }
    
         public bool UpdateOperator(OperatorModel opm)
         {
             SelectedOperator.OperatorName = opm.OperatorName;
             SelectedOperator.OperatorPhone = opm.OperatorPhone;
             SelectedOperator.OperatorEmail = opm.OperatorEmail;
             SelectedOperator.OperatorAvatar = opm.OperatorAvatar;
    
             return true;
         }
     }
 }







dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered Grime commented

Hello,​

Welcome to our Microsoft Q&A platform!

how do I get the selected model from (4) back to be displayed on (1) using Navigation.PopModalAsync?

Using Navigation.PopModalAsync to transfer data, it is impossible.

But you can use MessageCenter to achieve it.

You can subscribe it in the MainPage's background code.

MessagingCenter.Subscribe<App, string>(App.Current, "OneMessage", (snd, arg) =>
            {
                Device.BeginInvokeOnMainThread(() => {
                    //Navigation.PushAsync(new NavigationPage(new DetailsInfo(arg)));

                    myLabel.Text = arg;
                });
            });


You can send message in the SelectOperatorPage (4).

MessagingCenter.Send<App, string>(App.Current as App, "OneMessage", "test");

=========================Update=========================

If you do not want to achieve it with MessagingCenter, you can use CallBack event to achieve it.

Open your OperatorsPage.xaml.cs. Add public EventHandler<OperatorModel> ReturnValue; and achieve it in BackButton_Clicked method, Here is all of code about OperatorsPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App94
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class OperatorsPage : ContentPage
    {
        OperatorViewModel ovm;
        OperatorModel opm;
        public EventHandler<OperatorModel> ReturnValue;
        // public OperatorsPage(OperatorViewModel _ovm)
        // The line above changed to the line below at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021
        public OperatorsPage(OperatorModel SelectModel)
        {
            // add a bit of padding to cater to the "notch" on the iPhone.
            if (Device.RuntimePlatform == Device.iOS)
            {
                Padding = new Thickness(0, 40, 0, 0);
            }

            InitializeComponent();


            // ovm = _ovm;
            // opm = ovm.SelectedOperator;
            // The 2 lines above changed to the line below at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021

            if (SelectModel!=null)
            {
                opm = SelectModel;
            }
            else
            {
                opm = new OperatorModel();
            }
          

            BindingContext = opm;

        }

        private async void BackButton_Clicked(object sender, EventArgs e)
        {
            if (SelectModel!=null)
            {
              //  ovm.SelectedOperator = SelectModel;
                EventHandler<OperatorModel> handler = ReturnValue;
                if (handler != null)
                {
                    handler(this, opm);
                }

                
            }
          
           


            await Navigation.PopModalAsync();



            // The line above originally commented out at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021
        }

        private async void AddOperatorButton_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new NewOperatorPage());
            // The line above originally commented out at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021

        }

        private async void SelectOperatorButton_Clicked(object sender, EventArgs e)
        {
            // The lines below added at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021
            SelectOperatorPage page = new SelectOperatorPage();
            page.ReturnValue += delegate (object s, OperatorModel operatorModel)
            {
                BackCall(s, operatorModel);
            };

            await Navigation.PushModalAsync(page);
        }
        // The lines below added at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021

        OperatorModel SelectModel;
        private void BackCall(object s, OperatorModel model)
        {
            SelectModel = model;
            opm.OperatorName = model.OperatorName;
        }

       

        private async void DeleteOperatorButton_Clicked(object sender, EventArgs e)
        {
            if (SelectModel != null)
            {

                OperatorViewModel.OperatorList.Remove(SelectModel);
                SelectModel = null;
                SelectedOperatorEntry.Text = "";
            }
            else
            {
              await  DisplayAlert("info","please select item","OK");
            }
        }

        private async void UpdateOperatorButton_Clicked(object sender, EventArgs e)
        {
            //var operatorInfoPage = new OperatorInfoPage();
            //operatorInfoPage.BindingContext = ovm;
            //await Navigation.PushModalAsync(operatorInfoPage);
            //  if (opm != null)
            //  {

           

            //  }
            if (SelectModel != null)
            {

                  var operatorInfoPage = new OperatorInfoPage(SelectModel);
                operatorInfoPage.ReturnValue += delegate (object s, OperatorModel operatorModel)
                {
                    BackCall(s, operatorModel);
                };
                await Navigation.PushModalAsync(operatorInfoPage);
            }
            else
            {
                await DisplayAlert("info", "please select item", "OK");
            }

        }
    }
}


Then, you can get the call back value in the BackCall method of MainPage.xaml.cs, here is all of code about MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace App94
{
    public partial class MainPage : ContentPage
    {
        OperatorViewModel ovm;
        OperatorModel opm;
        public MainPage()
        {
            // add a bit of padding to cater to the "notch" on the iPhone.
            if (Device.RuntimePlatform == Device.iOS)
            {
                Padding = new Thickness(0, 40, 0, 0);
            }

            InitializeComponent();

            ovm = new OperatorViewModel();
            opm = new OperatorModel();
            BindingContext = ovm;
        }

        //protected override async void OnAppearing()
        //{
        //    base.OnAppearing();
        //    await RefreshOperatorListView();
        //}

        //public async Task<bool> RefreshOperatorListView()
        //{
        //    OperatorListView.ItemSource = null;
        //    await ovm.GetOperators()
        //    OperatorListView.ItemSource = ovm.OperatorList;
        //    return true;
        //}

        private async void OperatorButton_Clicked(object sender, EventArgs e)
        {
         var page=   new OperatorsPage(SelectModel);
            page.ReturnValue += delegate (object s, OperatorModel operatorModel)
            {
                BackCall(s, operatorModel);
            };
            await Navigation.PushModalAsync(page);
        }
        OperatorModel SelectModel;
        private void BackCall(object s, OperatorModel model)
        {
            SelectModel = model;
            opm.OperatorName = model.OperatorName;

            OperatorButton.Text = opm.OperatorName;
        }
        private async void AboutButton_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new AboutPage());
        }

        private async void MenuButton_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new MenuPage());
        }
    }

}


If you other navigate pages want to achieve it as well, you can use callback event to achieve it as well.

And I change the constructor for OperatorsPage, But your ManageOperatorsButton_Clicked event in MenuPage need to open OperatorsPage as well, so I new a OperatorModel to send it.

private async void ManageOperatorsButton_Clicked(object sender, EventArgs e)
        {
            OperatorModel SelectModel = new OperatorModel();
            await Navigation.PushModalAsync(new OperatorsPage(SelectModel));
        }


=================Update2==================

If you want to achieve the MenuPage function, that is same with above funtion, use callback event to achieve it as well, Here is all of code MainPage.xaml.cs

public partial class MainPage : ContentPage
    {
        OperatorViewModel ovm;
        OperatorModel opm;
        public MainPage()
        {
            // add a bit of padding to cater to the "notch" on the iPhone.
            if (Device.RuntimePlatform == Device.iOS)
            {
                Padding = new Thickness(0, 40, 0, 0);
            }

            InitializeComponent();

            ovm = new OperatorViewModel();
            opm = new OperatorModel();
            BindingContext = ovm;
        }

        //protected override async void OnAppearing()
        //{
        //    base.OnAppearing();
        //    await RefreshOperatorListView();
        //}

        //public async Task<bool> RefreshOperatorListView()
        //{
        //    OperatorListView.ItemSource = null;
        //    await ovm.GetOperators()
        //    OperatorListView.ItemSource = ovm.OperatorList;
        //    return true;
        //}

        private async void OperatorButton_Clicked(object sender, EventArgs e)
        {
         var page=   new OperatorsPage(SelectModel);
            page.ReturnValue += delegate (object s, OperatorModel operatorModel)
            {
                BackCall(s, operatorModel);
            };
            await Navigation.PushModalAsync(page);
        }
        OperatorModel SelectModel;
        private void BackCall(object s, OperatorModel model)
        {
            SelectModel = model;
            opm.OperatorName = model.OperatorName;

            OperatorButton.Text = opm.OperatorName;
        }
        private async void AboutButton_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new AboutPage());
        }

        private async void MenuButton_Clicked(object sender, EventArgs e)
        {

            var page = new MenuPage(SelectModel);
            page.ReturnValue += delegate (object s, OperatorModel operatorModel)
            {
                BackCall(s, operatorModel);
            };

            await Navigation.PushModalAsync(page);
        }
    }

}


Here is MenuPage.xaml.cs

public partial class MenuPage : ContentPage
    {
        OperatorViewModel ovm;
        OperatorModel SelectModel;
        public MenuPage(OperatorModel SelectModel)
        {
            // add a bit of padding to cater to the "notch" on the iPhone.
            if (Device.RuntimePlatform == Device.iOS)
            {
                Padding = new Thickness(0, 40, 0, 0);
            }

            InitializeComponent();
            this.SelectModel = SelectModel;
            ovm = new OperatorViewModel();
            BindingContext = ovm;
        }

        
        private void BackCall(object s, OperatorModel model)
        {
            SelectModel = model;
           // SelectModel.OperatorName = model.OperatorName;

           // OperatorButton.Text = opm.OperatorName;
        }
        public EventHandler<OperatorModel> ReturnValue;
        private async void BackButton_Clicked(object sender, EventArgs e)
        {
            if (SelectModel != null)
            {
                //  ovm.SelectedOperator = SelectModel;
                EventHandler<OperatorModel> handler = ReturnValue;
                if (handler != null)
                {
                    handler(this, SelectModel);
                }


            }




            await Navigation.PopModalAsync();



            // The line above originally commented out at the behest of JessieZhang-2116 (Microsoft Q&A) 01/05/2021
        }

        //private async void BackButton_Clicked(object sender, EventArgs e)
        //{
        //    await Navigation.PopModalAsync();
        //}

        private async void ManageOperatorsButton_Clicked(object sender, EventArgs e)
        {
            if (this.SelectModel == null)
            {
                SelectModel = new OperatorModel();
            }

            var page = new OperatorsPage(SelectModel);
            page.ReturnValue += delegate (object s, OperatorModel operatorModel)
            {
                BackCall(s, operatorModel);
            };

            await Navigation.PushModalAsync(page);
        }
    }
}


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.





· 13
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LeonLu-MSFT Thanks for your response.

At the moment, the OperatorModel selected in SelectOperatorPage (4) can be displayed in OperatorsPage (3).
I need to also display it in MainPage (1).

The Operator Model consists of an int and 4 strings:

         public OperatorModel(int operatorID, string operatorName, string operatorPhone, string operatorEmail, string operatorAvatar)
         {
             OperatorID = operatorID;
             OperatorName = operatorName;
             OperatorPhone = operatorPhone;
             OperatorEmail = operatorEmail;
             OperatorAvatar = operatorAvatar;
         }

Is MessagingCenter really the best way of doing this?

0 Votes 0 ·

@LeonLu-MSFT

Ultimately, I want to programmatically change the button Text to the Selected OperatorName, and change the TextColor to Black.

                 <Button
                     x:Name="OperatorButton"
                     Text="Click to SET Operator"
                     Grid.Row="0"
                     Grid.Column="1"
                     Grid.ColumnSpan="2"
                     TextColor="Gray"
                     FontAttributes="Italic"
                     BackgroundColor="White"
                     Margin="10,0,0,0"
                     Clicked="OperatorButton_Clicked"
                     VerticalOptions="Center"/>

I'm really using the button as an Entry with Placeholder text, but I wanted to be able to click on it to make a selection.

0 Votes 0 ·

please see my updated answer.

0 Votes 0 ·
Show more comments