Xamarin.Forms StackLayout с x: Name, не найденным внутри DataTemplate - PullRequest
0 голосов
/ 20 июня 2020

Файл 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)

Что я делаю не так?

1 Ответ

0 голосов
/ 22 июня 2020

Вы можете получить текущий элемент, добавив имя к:

<CarouselView x:Name="myCarouselView">

А затем получить текущий видимый стек:

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