【问题标题】:Get label from a stacklayout with a Tap Gesture Recognizer使用 Tap Gesture Recognizer 从堆栈布局中获取标签
【发布时间】:2017-05-24 10:05:39
【问题描述】:

我想知道通过点击手势从堆栈布局中获取不同标签的最佳方法是什么。我的 Stacklayout 是网格的一部分。

似乎是个好主意,但无助于获取例如我的品牌名称的文本值。

这是我的堆栈布局的内容:

Image PictureProduct = new Image { Aspect = Aspect.AspectFit };
PictureProduct.Source = FileImageSource.FromUri(...);
Label BrandName = new Label {Text = "HelloWorld"};
Label Description = new Label { Text = "Hello darkness my old friends..."};
Label Price = new Label { Text = "14,99€"};

StackLayout TheStack = new StackLayout { HorizontalOptions = LayoutOptions.Fill, VerticalOptions = LayoutOptions.Fill, HeightRequest = 425 };

TheStack.Children.Add(PictureProduct);
TheStack.Children.Add(BrandName);
TheStack.Children.Add(Description);
TheStack.Children.Add(Price);

var MyTapGesture = new TapGestureRecognizer();

MyTapGesture.Tapped += (sender, e) =>
{

    Debug.Write(/*BrandName of product*/);

};
TheStack.GestureRecognizers.Add(MyTapGesture);

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    我找到了一个目前正在工作的解决方案。

                StackLayout TheStack = new StackLayout { HorizontalOptions = LayoutOptions.Fill, VerticalOptions = LayoutOptions.Fill, HeightRequest = 425, ClassId = ListProduct[CurrentProd].Sku.ToString(), ClassId = "Something" };
    
                TheStack.Children.Add(PictureProduct);
                TheStack.Children.Add(BrandName);
                TheStack.Children.Add(Description);
                TheStack.Children.Add(Price);
    
                var MyTapGesture = new TapGestureRecognizer();
                MyTapGesture.Tapped += (sender, e) =>
                {
                    StackLayout TappedStackId = sender as StackLayout;
                    Debug.Write("TappedStackId = " + TappedStackId.ClassId);
                };
                TheStack.GestureRecognizers.Add(MyTapGesture);
    

    【讨论】:

      【解决方案2】:

      您不应该将 TGR 附加到 StackLayout,而是附加到每个标签。那么,“发件人”应该告诉你哪个是“发件人”...

      你可以试试

      PictureProduct.GestureRecognizers.Add(MyTapGesture);
      
      MyTapGesture.Tapped += (sender, e) =>
      {
      
          if(sender is PictureProduct){}
      
      };
      

      否则,为每个标签创建一个 TGR

      PictureProduct.GestureRecognizers.Add(PictureProductTGR);
      

      【讨论】:

      • 似乎是个好主意,但无助于获取例如我的品牌名称的文本值。这是我的堆栈布局的内容: Image PictureProduct = new Image { Aspect = Aspect.AspectFit }; PictureProduct.Source = FileImageSource.FromUri(...);标签 BrandName = 新标签 {Text = "HelloWorld"};标签描述=新标签{文本=“你好黑暗我的老朋友......”};标签价格 = 新标签 { Text = "14,99€"};
      • 对于标签类似于“var text = ((Label)sender).Text;
      猜你喜欢
      • 2016-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      相关资源
      最近更新 更多