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

Xamarin 如何从我在cs代码中创建的xaml访问对象?,xamarin,xamarin.android,xamarin.forms,Xamarin,Xamarin.android,Xamarin.forms,我试图访问我用cs创建的一些对象(带标签、listview和按钮的旋转页面),但没有成功 这里引用这一点,表示您可以使用classId或样式ID,但它没有明确说明如何获得相同的样式: 在这里你可以看到旋转木马页面 这是我的cs代码: private readonly List<AreaModel> _are = new List<AreaModel>(); public AreasModal() { Lista();

我试图访问我用cs创建的一些对象(带标签、listview和按钮的旋转页面),但没有成功

这里引用这一点,表示您可以使用
classId
样式ID
,但它没有明确说明如何获得相同的样式:

在这里你可以看到旋转木马页面

这是我的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

                        }
                    }
                });
            }

        }
    }
private readonly List_are=new List();
公共区域
{
Lista();
}
受保护的覆盖无效OnCurrentPageChanged()
{
base.OnCurrentPageChanged();
var index=Children.IndexOf(当前页面);
}
私有异步void Lista()
{ 
var listareas=wait App.NaturalezaManager.GetAreas();
foreach(列表区域中的var区域){
var区域=新区域模型
{
IdArea=面积。IdArea,
NombreArea=面积。NombreArea
};
_是。添加(区域);
按钮boton=新按钮
{ 
Text=“Listo”,
垂直选项=LayoutOptions.CenterAndExpand,
水平选项=LayoutOptions.CenterAndExpand
};
添加(新内容页)
{
内容=新的堆栈布局
{
孩子们=
{
新堆栈布局
{
方向=堆叠方向。水平,
孩子们=
{
新堆栈布局
{
方向=堆叠方向。垂直,
孩子们=
{
新标签
{
Text=Area.NombreArea,
FontSize=Device.GetNamedSize(NamedSize.Large,typeof(Label)),
垂直选项=LayoutOptions.CenterAndExpand,
水平选项=LayoutOptions.CenterAndExpand
}
}
},
新交换机
{
水平选项=LayoutOptions.EndAndExpand
}
}
},
新列表视图
{
ItemsSource=列表区域,
ItemTemplate=新数据模板(()=>
{
标签名称标签=新标签
{
水平选项=LayoutOptions.FillAndExpand
};
namelab.SetBinding(Label.TextProperty,“NombreArea”);
开关切换器=新开关
{
水平选项=LayoutOptions.EndAndExpand
};
返回新的ViewCell
{
视图=新堆栈布局
{
方向=堆叠方向。水平,
孩子们=
{
新堆栈布局
{
方向=堆叠方向。垂直,
孩子们=
{
名牌
}
},
切换器
}
}
};
})
},
波顿
}
}
});
}
}
}

我必须从“OnCurrentPageChanged”方法访问这些对象,但我不知道如何访问,因为如果是xaml代码,可以使用属性
x:Name
,提前获得,谢谢您的帮助。

您试图访问哪些对象?
ContentPage
对象在
CarouselPage
中还是
列表区域中的对象?您想对对象做什么?您好,我必须访问这两个对象,我应该做的是根据contentpage标签中的区域加载listview,但我必须发送一个参数来查找列表中的项目,但是我不知道如何为我的Label分配id属性或名称,只需将对象的声明移动到更高的级别,就像
\u are
变量一样。然后您将能够访问该范围内的任何位置。