如何以xamarin表单将数据发送回父页面?

时间:2018-06-04 08:11:42

标签: c# json xamarin.forms navigation messagingcenter

页面Account中有一个标签,在点按时会创建一个带有前提地址列表的新ContentPage。点击任意地址都应弹出ContentPage并将值发回Account页面以设置Account页面中的某些字段。我试图使用Messaging中心,但它似乎无法获得价值。我错过了什么?

这是使用前提地址创建ContentPage的代码:

 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;
    }

这就是它订阅消息中心的方式:

public Account()
    {
        MessagingCenter.Subscribe<ContentPage,int>(this, "premiseId", (sender,arg) =>
        {
            DisplayAlert("Premise Changed", "test", "OK");
            selectedPremise = arg;
            DisplaySelectedPremiseValues();
        });
        InitializeComponent();
}

1 个答案:

答案 0 :(得分:0)

一个选项可能是使用全局变量(例如在App.cs中)并在点击列表项时设置此变量:

public static Address TappedAddress;

在显示listview之前重置该变量。