调用变量从DataTemplate内部的代码隐藏与Xamarin形式

问题描述:

它会从代码隐藏firstGridsecondGrid对象恢复。 但是这些都在DataTemplate中,因为我可以从代码隐藏中回想起它们?调用变量从DataTemplate内部的代码隐藏与Xamarin形式

谢谢。

这里是我的代码:

<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。它与c#和WPF略有不同。

感谢

+0

你想用电网做什么?也许我们可以找到一个解决方法 – Daniel

+0

我必须给她回电话以根据旋转木马的索引隐藏它。 –

+0

添加属性isVisible到您的viewmodel并相应地设置 – Daniel

当你使用一个DataTemplate,绑定关注的对象,你有约束力,而不是您可能使用的视图模型。

但是不要害怕,因为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 
     } 

如果我这样做不回答你的问题,不要犹豫,以增加你的问题,使其更清楚。

+0

是的,在这里他讲的是后面的代码,而不是ViewModel,但是你展示的方法应该可以工作...... :) –

+0

当你点击...不要为我 –