0

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="WPapp.Views.Home"
             x:Name="HomeHeader">
    <ContentPage.ToolbarItems> 
        <ToolbarItem Text="Refresh"
                     x:Name="refreshButton"
                     Clicked="refreshButton_Clicked"/>
    </ContentPage.ToolbarItems>
    <ContentPage.Content>
        <ScrollView>
            <StackLayout Margin="10"
                         HorizontalOptions="Center"
                         VerticalOptions="FillAndExpand"
                         x:Name="HomeContainer">
                <Label Text="Featured Posts"
                       FontSize="Title"
                       FontAttributes="Bold"
                       Margin="0, 20, 10, 0"/>
                <CarouselView>
                    <CarouselView.ItemTemplate>
                        <DataTemplate>
                            <StackLayout x:Name="FeaturedContainer"/>
                        </DataTemplate>
                    </CarouselView.ItemTemplate>
                </CarouselView>
            </StackLayout>
        </ScrollView>
    </ContentPage.Content>
</ContentPage>

使用 c# 文件中的这一行,

FeaturedContainer.Children.Clear();

我收到以下错误:

错误 CS0103:当前上下文中不存在名称“FeaturedContainer”(CS0103)

我究竟做错了什么?

4

1 回答 1

0

您可以通过添加名称来获取当前项目:

<CarouselView x:Name="myCarouselView">

然后获取当前可见的stacklayout:

private void Button_Clicked(object sender, EventArgs e)
{
    ObservableCollection<View> views = myCarouselView.VisibleViews;

    StackLayout st = views[0] as StackLayout;
    st.Children.Clear();

    //or
    StackLayout st1 = myCarouselView.CurrentItem as StackLayout;
    st1.Children.Clear();
}

更新

通过 c# 在轮播视图中添加几个堆栈布局

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        CarouselView carouselView = new CarouselView
        {
            ItemsLayout = (LinearItemsLayout)LinearItemsLayout.Horizontal
        };

        carouselView.ItemTemplate = new DataTemplate(typeof(CustomCell));

        carouselView.ItemsSource = new string[]
            {
                "Baboon",
                "Capuchin Monkey",
                "Blue Monkey",
                "Squirrel Monkey",
                "Golden Lion Tamarin",
                "Howler Monkey",
                "Japanese Macaque"
            };

        Content = carouselView;
    }
}

public class CustomCell : ContentView
{
    public CustomCell()
    {
        //instantiate each of our views
        var image = new Image();
        StackLayout cellWrapper = new StackLayout();
        StackLayout horizontalLayout = new StackLayout();
        Label left = new Label();
        Label right = new Label();

        //set bindings
        left.Text = "title";
        right.Text = "title";

        //Set properties for desired design
        cellWrapper.BackgroundColor = Color.FromHex("#eee");
        horizontalLayout.Orientation = StackOrientation.Horizontal;
        right.HorizontalOptions = LayoutOptions.EndAndExpand;
        left.TextColor = Color.FromHex("#f35e20");
        right.TextColor = Color.FromHex("503026");

        //add views to the view hierarchy
        horizontalLayout.Children.Add(image);
        horizontalLayout.Children.Add(left);
        horizontalLayout.Children.Add(right);
        cellWrapper.Children.Add(horizontalLayout);

        this.Content = cellWrapper;
    }
}
于 2020-06-22T02:55:16.897 回答