【问题标题】:call variable from codebehind inside DataTemplate with Xamarin Forms使用 Xamarin Forms 从 DataTemplate 中的代码隐藏调用变量
【发布时间】:2017-03-13 11:53:38
【问题描述】:

它将从 firstGridsecondGrid 对象的代码隐藏中恢复。 但是这些都在 DataTemplate 中,我可以从代码隐藏中回忆它们吗?

谢谢。

这是我的代码:

<control:CarouselView x:Name="carouselView">
                            <control:CarouselView.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="46*"></RowDefinition>
                                            <RowDefinition Height="0.2*"></RowDefinition>
                                            <RowDefinition Height="53.8*"></RowDefinition>
                                        </Grid.RowDefinitions>
                                        <!-- griglia del carousel, tutta la struttura inizia qui-->
                                        <Grid Grid.Row="0">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="10*"></ColumnDefinition>
                                                <ColumnDefinition Width="35*"></ColumnDefinition>
                                                <ColumnDefinition Width="45*"></ColumnDefinition>
                                                <ColumnDefinition Width="10*"></ColumnDefinition>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="*"></RowDefinition>
                                            </Grid.RowDefinitions>
                                            <Button Image="backBlueArrow.png" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="clickmePrev" Grid.Column="0"></Button>
                                            <!--<Image Source="backBlueArrow.png" x:Name="backClickArrow" Aspect="AspectFit" Grid.Column="0"></Image>-->
                                            <StackLayout Margin="0,0,10,0" VerticalOptions="Center" HorizontalOptions="End" Grid.Column="1">
                                                <Image HeightRequest="70" Source="{Binding Immagine}" Aspect="AspectFit"></Image>
                                            </StackLayout>
                                            <!-- griglia superiore, riprende i dati tra le barre bianche-->
                                            <Grid Grid.Row="0" Grid.Column="2">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="40*"></RowDefinition>
                                                    <RowDefinition Height="60*"></RowDefinition>
                                                </Grid.RowDefinitions>
                                                <Label Text="{Binding Descrizione}" Margin="{Binding Margine}" TextColor="#fff" FontSize="Small" VerticalTextAlignment="End" HorizontalTextAlignment="Start" Grid.Row="0"/>
                                                <Label Text="{Binding Valore}" FontSize="Large" TextColor="#fff" VerticalTextAlignment="Start" HorizontalTextAlignment="Start" Grid.Row="1"/>
                                                <Image Margin="-25,-5,0,0" Source="{Binding ImmagineBatteriaEmoticon}" HeightRequest="50" x:Name="emoticonBattery" Grid.Row="1" VerticalOptions="Start" HorizontalOptions="Start"></Image>
                                            </Grid>
                                            <Button Image="nextBlueArrow.png" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="clickmeNext" Grid.Column="3"></Button>
                                            <!--<Image Source="nextBlueArrow.png" x:Name="nextClickArrow" Aspect="AspectFit" Grid.Column="3"></Image>-->
                                        </Grid>
                                        <!-- griglia fare la riga bianca -->
                                        <Grid BackgroundColor="White" Grid.Row="1">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*"></ColumnDefinition>
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="*"></RowDefinition>
                                            </Grid.RowDefinitions>
                                        </Grid>
                                        <!-- griglia inferiore, ritrare i dati dei testi -->
                                        <ScrollView Grid.Row="2">
                                            <Grid Padding="5" Grid.Row="2" x:Name="firstGrid">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="65*"></ColumnDefinition>
                                                    <ColumnDefinition Width="35*"></ColumnDefinition>
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                    <RowDefinition Height="*"></RowDefinition>
                                                </Grid.RowDefinitions>
                                                <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="0" Text="{Binding Testo1}"></Label>
                                                <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="0" Text="{Binding Valore1}"></Label>
                                                <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="1" Text="{Binding Testo2}"></Label>
                                                <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="1" Text="{Binding Valore2}"></Label>
                                                <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="2" Text="{Binding Testo3}"></Label>
                                                <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="2" Text="{Binding Valore3}"></Label>
                                                <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="3" Text="{Binding Testo4}"></Label>
                                                <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="3" Text="{Binding Valore4}"></Label>
                                                <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="4" Text="{Binding Testo5}"></Label>
                                                <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="4" Text="{Binding Valore5}"></Label>
                                                <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="5" Text="{Binding Testo6}"></Label>
                                                <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="5" Text="{Binding Valore6}"></Label>
                                                <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="6" Text="{Binding Testo7}"></Label>
                                                <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="6" Text="{Binding Valore7}"></Label>
                                                <Label HorizontalTextAlignment="Start" TextColor="#5B6667" Grid.Column="0" Grid.Row="7" Text="{Binding Testo8}"></Label>
                                                <Label HorizontalTextAlignment="End" TextColor="#5B6667" Grid.Column="1" Grid.Row="7" Text="{Binding Valore8}"></Label>
                                            </Grid>
                                            <RelativeLayout Grid.Row="2" x:Name="secondGrid" BackgroundColor="Fuchsia" HeightRequest="50" WidthRequest="100">
                                                <Label Text="ciao"></Label>
                                            </RelativeLayout>
                                        </ScrollView>
                                    </Grid>
                                    <!--<Label Text="{Binding Testo}" />-->
                                </DataTemplate>
                            </control:CarouselView.ItemTemplate>
                        </control:CarouselView>

警告:在复制之前,代码适用于 Xamarin 表单 pcl。它与带有 WPF 的 c# 略有不同。

谢谢

【问题讨论】:

  • 你想用网格做什么?也许我们可以找到解决方法
  • 我得给她回电话,根据轮播的索引把它藏起来。
  • 添加一个属性 isVisible 到你的视图模型并相应地设置它
  • 我不使用 ViewModel。我需要从 Name 调用 objetc,但这不可能。我必须找到另一个解决方案

标签: c# xaml xamarin xamarin.forms portable-class-library


【解决方案1】:

当您使用 DataTemplate 时,绑定关系到您正在绑定的对象,而不是您可能使用的 ViewModel。

但不要害怕,因为 Xamarin 为我们提供了将处理附加到此类对象的方法!

假设你想点击你的图片:

<Button Image="nextBlueArrow.png" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="clickmeNext" Grid.Column="3"></Button>

可能变成

<Image Source="nextBlueArrow.png">
   <Image.GestureRecognizers>
     <TapGestureRecognizer
       Tapped="clickmeNext"
       CommandParameter="{Binding .}" />
     </Image.GestureRecognizers>
</Image>

在您的代码中使用 clickmeNext 方法,如下所示:

public async void clickmeNext(object sender, EventArgs e) 
        {
            Image imageSender = (Image)sender;
            YourObject yourObject= (YourObject )imageSender.BindingContext;

            //some actions on your instance of YourObject, 
            //which is the instance linked to the image
        }

如果我没有回答您的问题,请随时添加到您的问题中以使其更清楚。

【讨论】:

  • 是的,他在这里谈论的是代码背后而不是 ViewModel,但您展示的方法应该可以工作...... :)
  • 当你点击这个事件开始......不要为我
猜你喜欢
  • 2019-11-07
  • 2020-12-30
  • 2011-08-03
  • 2021-03-08
  • 2017-06-29
  • 2017-08-05
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多