【问题标题】:How can I get the text value of a label that is a child of a stacklayout which is a child of a Frame inside a listview item如何获取作为堆栈布局子级的标签的文本值,堆栈布局是列表视图项内框架的子级
【发布时间】:2017-02-18 17:22:55
【问题描述】:

我有一个 Xamarin.Forms PCL VS2015 解决方案,并花了三天时间寻找我的问题的解决方案。我尝试了很多方法,包括使用 Listview onitemselected,它适用于 Win10,但不适用于 Android 或 iOS。试过这个。FindByName(emailValue);并使用发件人框架,但没有运气。我需要提供作为标签文本绑定的电子邮件地址。

<Label x:Name="emailValue"
       Text="{Binding Email}"/>

标签是列表视图项的子项的子项。

x:Name 元素在后面的代码中不可用。我理解这是因为标签位于列表视图中,编译器无法区分列表视图项。

我已经使用智能感知在 Tap 的发件人(框架)上进行了深入研究,我可以看到我需要的电子邮件文本,但我无法找到提取它的方法。

    <StackLayout Orientation="Vertical"
           VerticalOptions="FillAndExpand">
<ListView x:Name="listViewContacts"
            ItemSelected="OnListViewItemSelected"
            IsPullToRefreshEnabled="true" Refreshing="OnRefresh"
            SeparatorColor="#FFCF00"
            VerticalOptions="FillAndExpand"
            HasUnevenRows="True">
  <!-- Need HasUnevenRows = true to enable correct 
  display in Android-->
  <ListView.ItemTemplate>
    <DataTemplate >
      <ViewCell>
        <ContentView Padding="5">
          <StackLayout>
            <Grid RowSpacing="10">
              <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
              </Grid.RowDefinitions>
              <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="Auto"/>
              </Grid.ColumnDefinitions>
    <Frame OutlineColor="Black"
                     BackgroundColor="Transparent"
                     Padding="0"
                     Grid.Row="4">
                <Frame.GestureRecognizers>
                  <TapGestureRecognizer Tapped="OnPhoneFrameTapped"/>
                </Frame.GestureRecognizers>
                <StackLayout Orientation="Horizontal"
                              Padding="10,5,0,5">
                  <Label Text="Phone:"/>
                  <Label x:Name="phoneValue"
                         Text="{Binding Mobile}"/>
                </StackLayout>
              </Frame> ...

【问题讨论】:

    标签: c# listview xamarin.forms xamarin.forms.listview


    【解决方案1】:

    发布问题后,我整理了如何使用向下钻取智能感知获取所需的文本,以降低到所需的级别。

    第一层是框架。第二层是Stacklayout,需要的Label是第三层。

    然后我使用 Plugin.Messanger 发送电子邮件。

    请注意,这也适用于使用电话拨号器并提取要拨打的电话号码。

    这是我的解决方案:注意 listChildren[1] 是我需要的子标签的索引。

    void OnEmailFrameTapped(object sender, EventArgs args)
        {
            Frame f = (Frame)sender;
            string emailstring = string.Empty;
    
            var fcontent = f.Content;
    
            var myStacklayout = fcontent.GetType();
    
            if (myStacklayout == typeof (StackLayout))
            {
               StackLayout fStacklayout = (StackLayout)fcontent;
    
                var listChildren = fStacklayout.Children;
    
                var reqLabel = listChildren[1];
    
                var theLabel = reqLabel.GetType();
    
                if (theLabel == typeof(Label))
                {
                   Label emailLabel = (Label)reqLabel;
    
                    emailstring = emailLabel.Text;
                }
            }
    
            var emailMessenger = CrossMessaging.Current.EmailMessenger;
            if (emailMessenger.CanSendEmail)
            {
                // Send simple e-mail to single receiver without attachments, bcc, cc etc.
                emailMessenger.SendEmail(emailstring,
                    "Test Sender",
                    "Hello from message");
            }
        }
    

    【讨论】:

      【解决方案2】:

      使用您找到的信息,我还能够将 TapGesture 缩小到我需要的特定标签,从而简化了我需要的部分。

      这里是 xaml

      <Label   
         Text="{Binding MapUri}" 
         TextColor="#ab1e2c" 
         FontAttributes="Bold"
         HorizontalTextAlignment="End">
           <Label.GestureRecognizers>
               <TapGestureRecognizer
                   Tapped="OnLabelTapped"
                   NumberOfTapsRequired="1" />
          </Label.GestureRecognizers>
      </Label>
      

      这是.cs

      public async void OnLabelTapped(object sender, EventArgs args)
      {
          string mapUri = string.Empty;
          Label l = (Label)sender;
          mapUri = l.mapUri; 
      
          var result = await Application.Current.MainPage.DisplayAlert("Alert", "Launch Google Maps" , "OK", "Cancel");
          if (result)
          {
              Device.OpenUri(new Uri(mapUri));
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-05
        • 2016-06-23
        • 2020-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多