C# 从代码访问DataTemplate的元素

C# 从代码访问DataTemplate的元素,c#,xaml,xamarin.forms,datatemplate,C#,Xaml,Xamarin.forms,Datatemplate,我想找到一种从c#访问按钮图像元素的方法。DataTemplate中包含的所有内容都不能从c#调用 在c#中,我想回忆一下CuorePieno元素 我该怎么做?更好的方法是将数据模板移动到单独的文件中。我假设您共享的文件名为CarouselPage.xaml,类型为ContentPage: <ContentPage.Resource> <DataTemplate x:Key="FrasiViewItemTemplate">

我想找到一种从c#访问
按钮图像
元素的方法。
DataTemplate
中包含的所有内容都不能从c#调用


在c#中,我想回忆一下
CuorePieno
元素
我该怎么做?

更好的方法是将
数据模板
移动到单独的文件中。我假设您共享的文件名为CarouselPage.xaml,类型为
ContentPage

<ContentPage.Resource>
        <DataTemplate x:Key="FrasiViewItemTemplate">
             <yournamespace:FrasiViewItemTemplate>
        </DataTemplate>
</ContentPage.Resource>
        
<CarouselView                   
    x:Name="FrasiView"
    RelativeLayout.WidthConstraint="{ConstraintExpression
    Type=RelativeToParent,
    Property=Width,
    Factor=1}"
    RelativeLayout.YConstraint="{ConstraintExpression
    Type=Constant,
    Constant=45}"
    PeekAreaInsets="18"
    HeightRequest="370"
    ItemTemplate={DynamicResource FrasiViewItemTemplate}>
    <CarouselView.ItemsLayout>
        <LinearItemsLayout Orientation="Horizontal" SnapPointsType="MandatorySingle" SnapPointsAlignment="Center"/>
    </CarouselView.ItemsLayout>
</CarouselView>
您可以访问
DataTemplate
中的元素frasiveItemTemplate.xaml.cs以覆盖它(不要忘记名称空间):

(注意它的基类,它不再是
ContentView


我希望当用户按下ImageButton时,IsVisible属性会改变

我同意Jason的回答,您可以使用绑定来更改ImageButton IsVisible属性,我做了一个示例,您可以看看:

<RelativeLayout>
        <CarouselView
            x:Name="FrasiView"
            HeightRequest="370"
            ItemsSource="{Binding lists}"
            PeekAreaInsets="18"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
                                                                  Property=Width,
                                                                  Factor=1}"
            RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,
                                                              Constant=45}">
            <CarouselView.ItemsLayout>
                <LinearItemsLayout
                    Orientation="Horizontal"
                    SnapPointsAlignment="Center"
                    SnapPointsType="MandatorySingle" />
            </CarouselView.ItemsLayout>
            <CarouselView.ItemTemplate>
                <DataTemplate>

                    <RelativeLayout x:Name="FrontView">
                        <Label Text="{Binding str}" />
                        <ImageButton
                            x:Name="CuoreVuoto"
                            BackgroundColor="Transparent"
                            Clicked="CuoreVuoto_Clicked"
                            IsVisible="{Binding Vuoto}"
                            RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,
                                                                              Constant=275}"
                            RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,
                                                                              Constant=22}"
                            Source="c1.png"
                            WidthRequest="24" />
                        <ImageButton
                            x:Name="CuorePieno"
                            BackgroundColor="Transparent"
                            Clicked="CuorePieno_Clicked"
                            IsVisible="{Binding Pieno}"
                            RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,
                                                                              Constant=275}"
                            RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,
                                                                              Constant=22}"
                            Source="check.png"
                            WidthRequest="24" />
                    </RelativeLayout>

                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>
    </RelativeLayout>

您不能通过代码隐藏按名称访问模板化元素。使用模板的全部目的是利用数据绑定。您具体需要做哪些数据绑定无法实现的事情?我希望当用户按下ImageButton时,IsVisible属性更改您可以通过绑定、命令或事件处理程序来完成。没有理由需要按名称引用元素来执行此操作。在本例中,我可以更改可见性,但是如果我想访问x:name,该怎么办?
<RelativeLayout  x:Class="yournamespace.FrasiViewItemTemplate"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <RelativeLayout x:Name="FrontView">
        <RelativeLayout.GestureRecognizers>
            <TapGestureRecognizer Tapped="FlipToBack_Tapped"/>
        </RelativeLayout.GestureRecognizers>
          <ImageButton
            x:Name="CuoreVuoto"
            RelativeLayout.YConstraint="{ConstraintExpression
            Type=Constant,
            Constant=22}"
            RelativeLayout.XConstraint="{ConstraintExpression
            Type=Constant,
            Constant=275}"
            Source="IconHeart"
            WidthRequest="24"
            Clicked="CuoreVuoto_Clicked"
            BackgroundColor="Transparent"/>
          <ImageButton
          x:Name="CuorePieno"
          RelativeLayout.YConstraint="{ConstraintExpression
          Type=Constant,
          Constant=22}"
          RelativeLayout.XConstraint="{ConstraintExpression
          Type=Constant,
          Constant=275}"
          Source="IconHeart"
          WidthRequest="24"
          Clicked="CuorePieno_Clicked"
          IsVisible="False"
          BackgroundColor="Transparent"/>
         </RelativeLayout>
</RelativeLayout>
    public partial class FrasiViewItemTemplate : RelativeLayout
    {
        public FrasiViewItemTemplate()
        {
            BindingContext = this;
            InitializeComponent();
        }

        private void FlipToBack_Tapped(object sender, System.EventArgs e)
        {

        }

        private void CuoreVuoto_Clicked(object sender, System.EventArgs e)
        {
            CuoreVuoto.IsVisible = false;
            CuorePieno.IsVisible = true;

        }

        private void CuorePieno_Clicked(object sender, System.EventArgs e)
        {
            CuoreVuoto.IsVisible = true;
            CuorePieno.IsVisible = false;
        }

        private void TapGestureRecognizer_Tapped(object sender, System.EventArgs e)
        {

        }
    }
<RelativeLayout>
        <CarouselView
            x:Name="FrasiView"
            HeightRequest="370"
            ItemsSource="{Binding lists}"
            PeekAreaInsets="18"
            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent,
                                                                  Property=Width,
                                                                  Factor=1}"
            RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,
                                                              Constant=45}">
            <CarouselView.ItemsLayout>
                <LinearItemsLayout
                    Orientation="Horizontal"
                    SnapPointsAlignment="Center"
                    SnapPointsType="MandatorySingle" />
            </CarouselView.ItemsLayout>
            <CarouselView.ItemTemplate>
                <DataTemplate>

                    <RelativeLayout x:Name="FrontView">
                        <Label Text="{Binding str}" />
                        <ImageButton
                            x:Name="CuoreVuoto"
                            BackgroundColor="Transparent"
                            Clicked="CuoreVuoto_Clicked"
                            IsVisible="{Binding Vuoto}"
                            RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,
                                                                              Constant=275}"
                            RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,
                                                                              Constant=22}"
                            Source="c1.png"
                            WidthRequest="24" />
                        <ImageButton
                            x:Name="CuorePieno"
                            BackgroundColor="Transparent"
                            Clicked="CuorePieno_Clicked"
                            IsVisible="{Binding Pieno}"
                            RelativeLayout.XConstraint="{ConstraintExpression Type=Constant,
                                                                              Constant=275}"
                            RelativeLayout.YConstraint="{ConstraintExpression Type=Constant,
                                                                              Constant=22}"
                            Source="check.png"
                            WidthRequest="24" />
                    </RelativeLayout>

                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>
    </RelativeLayout>
public partial class Page1 : ContentPage
{
    public ObservableCollection<carouselmodel> lists { get; set; }
    public Page1()
    {
        InitializeComponent();
        lists = new ObservableCollection<carouselmodel>()
        {
            new carouselmodel(){str="test 1",Vuoto=true,Pieno=false},
            new carouselmodel(){str="test 2",Vuoto=true,Pieno=false},
            new carouselmodel(){str="test 3",Vuoto=true,Pieno=false},
            new carouselmodel(){str="test 4",Vuoto=true,Pieno=false},
            new carouselmodel(){str="test 5",Vuoto=true,Pieno=false},
            new carouselmodel(){str="test 6",Vuoto=true,Pieno=false}
        };
        this.BindingContext = this;
    }

    private void CuoreVuoto_Clicked(object sender, EventArgs e)
    {
        var button = sender as ImageButton;
        var model = button.BindingContext as carouselmodel;
        if(model.Vuoto==true)
        {
            model.Vuoto = false;
            model.Pieno = true;
        }
    }

    private void CuorePieno_Clicked(object sender, EventArgs e)
    {
        var button = sender as ImageButton;
        var model = button.BindingContext as carouselmodel;
        if (model.Pieno == true)
        {
            model.Pieno = false;
            model.Vuoto = true;                
        }
    }
}

public class carouselmodel : ViewModelBase
{
    public string str { get; set; }
    private bool _Vuoto;
    public bool Vuoto
    {
        get { return _Vuoto; }
        set
        {
            _Vuoto = value;
            RaisePropertyChanged("Vuoto");
        }
    }

    private bool _Pieno;
    public bool Pieno
    {
        get
        { return _Pieno; }
        set
        {
            _Pieno = value;
            RaisePropertyChanged("Pieno");
        }
    }
}
public class ViewModelBase : INotifyPropertyChanged
{
    
    public event PropertyChangedEventHandler PropertyChanged;

     
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}