如何从我在cs代码中创建的xaml中访问对象?

时间:2017-04-16 16:43:43

标签: xamarin xamarin.android xamarin.forms

我试图访问我用cs创建的一些对象(带有标签,列表视图和按钮的轮播页面),但我没有成功。

此处通过说明您可以使用 classId styleId 来引用此内容,但它并未明确说明如何获得相同: https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/getting_started_with_xaml/

在这里你可以看到轮播页面

enter image description here

这是我的cs代码:

    private readonly List<AreaModel> _are = new List<AreaModel>();
    public  AreasModal()
    {
        Lista();
    }

    protected override void OnCurrentPageChanged()
    {
        base.OnCurrentPageChanged();
        var index = Children.IndexOf(CurrentPage);
    }

    private async void Lista() 
    { 
    var listareas = await App.NaturalezaManager.GetAreas();
        foreach (var Area in listareas) {

            var areas = new AreaModel
            {
                IdArea = Area.IdArea,
            NombreArea = Area.NombreArea
            };
            _are.Add(areas);
            Button boton = new Button 
            { 
                Text =  "Listo",                                            
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.CenterAndExpand

            };
            Children.Add(new ContentPage
            {

                Content = new StackLayout
                {

                    Children =
                    {

                        new StackLayout
                        {
                            Orientation = StackOrientation.Horizontal,
                            Children =
                            {
                                new StackLayout
                                {
                                    Orientation = StackOrientation.Vertical,
                                    Children =
                                    {
                                        new Label
                                        {
                                            Text = Area.NombreArea,
                                            FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                                            VerticalOptions = LayoutOptions.CenterAndExpand,
                                            HorizontalOptions = LayoutOptions.CenterAndExpand
                                        }
                                    }
                                },
                                new Switch
                                    {
                                        HorizontalOptions = LayoutOptions.EndAndExpand
                                    }
                            }


                        },
                        new ListView
                        {
                            ItemsSource = listareas,
                            ItemTemplate = new DataTemplate(() =>
                            {

                                Label nameLabel = new Label
                                {
                                    HorizontalOptions = LayoutOptions.FillAndExpand
                                };
                                nameLabel.SetBinding(Label.TextProperty,"NombreArea");
                                Switch switcher = new Switch
                                {
                                    HorizontalOptions = LayoutOptions.EndAndExpand
                                };
                                return new ViewCell
                                {
                                    View = new StackLayout

                                    {
                                        Orientation = StackOrientation.Horizontal,
                                        Children =
                                        {
                                            new StackLayout
                                            {
                                                Orientation = StackOrientation.Vertical,
                                                Children =
                                                {
                                                    nameLabel
                                                }
                                            },
                                            switcher
                                        }


                                    }
                                };
                             })
                        },
                        boton

                        }
                    }
                });
            }

        }
    }

我必须从&#34; OnCurrentPageChanged&#34;访问这些对象。方法,但我不知道怎么做,因为如果它是xaml代码可以使用属性x:Name获得,提前感谢您的帮助。

0 个答案:

没有答案