1

There is a label in the page Account which when tapped on will create a new ContentPage with a list of premise addresses. Tapping on any of the addresses should pop the ContentPage and send back a value to the Account page to set certain fields within the Account page. I tried to use Messaging center but it doesn't seem to be able to get the value. What am I missing?

This is the code that creates the ContentPage with the premise addresses:

 private void ddlPremisesAddNavigation()
    {
        PremiseListPage = CreatePAContentPage();
        var tgrddlPremiseAddress = new TapGestureRecognizer();
        NavigationPage.SetHasNavigationBar(PremiseListPage, false);

        tgrddlPremiseAddress.Tapped += (s, e) =>
        {
            Navigation.PushAsync(PremiseListPage);
        };
        //  ddlPremiseAddresses.GestureRecognizers.Add(tgrddlPremiseAddress);
        lblpremiseAddress.GestureRecognizers.Add(tgrddlPremiseAddress);
    }
    private ContentPage CreatePAContentPage()
    {
        #region Containers
        ContentPage content = new ContentPage();
        StackLayout pageContent = new StackLayout();
        ScrollView addressesView = new ScrollView()
        {
            // BackgroundColor = Color.White,
            Padding = new Thickness(20, 10, 20, 10)
        };
        StackLayout addressContainer = new StackLayout();
        #endregion
        #region Header
        RowDefinitionCollection RowDefinitions = new RowDefinitionCollection();
        ColumnDefinitionCollection ColumnDefinitions = new ColumnDefinitionCollection();
        RowDefinitions.Add(new RowDefinition { Height = new GridLength(50, GridUnitType.Absolute) });

        Grid header = new Grid()
        {
            RowDefinitions = RowDefinitions,
        };

        BoxView bg = new BoxView() { HeightRequest = 50, WidthRequest = 250, BackgroundColor = Color.White };
        header.Children.Add(bg, 0, 0);
        Grid.SetColumnSpan(bg, 5);

        Label title = new Label()
        {
            Text = "Premise Address",
            FontSize = 15,
            FontAttributes = FontAttributes.Bold,
            VerticalOptions = LayoutOptions.CenterAndExpand,
            HorizontalOptions = LayoutOptions.CenterAndExpand
        };
        header.Children.Add(title, 1, 0);
        Grid.SetColumnSpan(title, 3);
        Button back = new Button() { Image = "backArrow", BackgroundColor = Color.White };//
        header.Children.Add(back, 0, 0);
        Grid.SetColumnSpan(back, 1);
        back.Clicked += back_Clicked;
        #endregion
        #region Address Frames
        List<Frame> addrFrames = new List<Frame>();

        if (premiseAddresses.Count <= 0)
        {
            foreach (PremisesModel premise in Premises)
            {
                premiseAddresses.Add(premise.PremiseId, premise.PremiseAddress);
            }
        }


        foreach (KeyValuePair<int,string> item in premiseAddresses)
        {
            addrFrames.Add(CreatePAFrame(item));
        }

        #endregion
        #region Add Content to Containers

        foreach (Frame item in addrFrames)
        {
            addressContainer.Children.Add(item);
        }
        //  < Button x: Name = "btnReqAmendment" Text = "Request amendment" Style = "{StaticResource buttonStyle}" Clicked = "btnReqAmendment_Clicked" />
        Button addNew = new Button()
        {
            Text = "ADD NEW PREMISE ADDRESS",
            Style = Application.Current.Resources["buttonStyle"] as Style,
            HorizontalOptions = LayoutOptions.CenterAndExpand,
            Margin = new Thickness(0, 20, 2, 15)
            //FontSize = 12,
            //WidthRequest = 220,
            //HeightRequest = 40
        };
        addNew.Clicked += btnAddNewPremise_Clicked;
        addressContainer.Children.Add(addNew);

        addressesView.Content = addressContainer;
        pageContent.Children.Add(header);
        pageContent.Children.Add(addressesView);
        content.Content = pageContent;

        #endregion

        return content;
    }
    private Frame CreatePAFrame(KeyValuePair<int, string> premiseAddress)
    {
        Frame frame = new Frame() { Padding = new Thickness(5, 5, 3, 5), HeightRequest = 60 };
        StackLayout content = new StackLayout() { Padding = 0 };
        content.Orientation = StackOrientation.Horizontal;

        Label pAddress = new Label();
        pAddress.Text = premiseAddress.Value;
        pAddress.Style = Application.Current.Resources["LabelStart"] as Style;
        pAddress.HeightRequest = 50;
        pAddress.HorizontalOptions = LayoutOptions.StartAndExpand;

        Image img = new Image()
        {
            Source = "rightArrow",
            HorizontalOptions = LayoutOptions.End,
            VerticalOptions = LayoutOptions.CenterAndExpand
        };
        content.Children.Add(pAddress);
        content.Children.Add(img);

        frame.Content = content;

        var selectAddress = new TapGestureRecognizer();
        selectAddress.Tapped += (s, e) =>
        {
            MessagingCenter.Send(this, "premiseId", premiseAddress.Key);
            Navigation.PopAsync();
        };
        frame.GestureRecognizers.Add(selectAddress);
        return frame;
    }

And this is how it subscribes to Messaging center:

public Account()
    {
        MessagingCenter.Subscribe<ContentPage,int>(this, "premiseId", (sender,arg) =>
        {
            DisplayAlert("Premise Changed", "test", "OK");
            selectedPremise = arg;
            DisplaySelectedPremiseValues();
        });
        InitializeComponent();
}
Scar
  • 725
  • 2
  • 7
  • 28
  • Have you checked value is passing on Send() method of Messaging Center? Also add your Subscribe() method in OnAppearing() method of Account Page and try one more time to passing value. – Srusti Thakkar Jun 05 '18 at 04:47
  • Might be happening due to you are using Generic Value i.e. ContentPage in Subscribe() method. Try to use specific value instead of that. – Srusti Thakkar Jun 05 '18 at 04:55

1 Answers1

0

One option could be using a global variable (e.g in App.cs) and setting this variable whenever a list item is tapped:

public static Address TappedAddress;

And before showing the listview reset that variable.

VahidShir
  • 2,066
  • 2
  • 17
  • 27