代码之家  ›  专栏  ›  技术社区  ›  John

xamarin表单-将文本绑定到xaml中的标签-我可以剪切文本的结尾吗?

  •  0
  • John  · 技术社区  · 4 年前

    晚上好,

    有一个标签。。。

      <Label Grid.Column="1"
                               HorizontalOptions="Center"
                               VerticalOptions="Center"
                               Text="{Binding Email}"/>
    

    所以,是的,这很好…但是它显示完整的电子邮件。。。

    JohnnyRambo@JR.com, HugsForFree@HF.com

    问:我可以从xaml页面删除域名吗?输出。。。

    约翰尼兰博, MovesLikeJagger, 拥抱自由

    谢谢你的回复

    0 回复  |  直到 4 年前
        1
  •  1
  •   Cherry Bu - MSFT    4 年前

    根据Jason的意见,您可以使用 Xamarin.Forms Binding Value Converters 删除域名。我提供了一个示例,您可以查看:

    public class persominfo
    {
        public string Name { get; set; }
        public string Email { get; set; }
    }
    
    public partial class Page34 : ContentPage
    {
        public ObservableCollection<persominfo> persons { get; set; }
        public Page34()
        {
            InitializeComponent();
    
            persons = new ObservableCollection<persominfo>()
            {
                new persominfo(){Name="JR",Email="JohnnyRambo@JR.com"},
                new persominfo(){Name="MJ",Email="MovesLikeJagger@MJ.com"},
                new persominfo(){Name="HF",Email="HugsForFree@HF.com"}
            };
            this.BindingContext = this;
        }
    }
    

    然后我使用ListView显示这些信息,使用数据绑定将任何Eamil属性转换为所需的字符串。

    <ContentPage
    x:Class="FormsSample.simplecontrol.Page34"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:converter="clr-namespace:FormsSample.converter">
    <ContentPage.Resources>
        <converter:EmailConverter x:Key="converter1" />
    </ContentPage.Resources>
    <ContentPage.Content>
        <StackLayout>
    
            <ListView ItemsSource="{Binding persons}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Horizontal">
                                <Label Text="{Binding Name}" />
                                <Label
                                    HorizontalOptions="CenterAndExpand"
                                    Text="{Binding Email, Converter={StaticResource converter1}}"
                                    VerticalOptions="CenterAndExpand" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <Label
                HorizontalOptions="CenterAndExpand"
                Text="{Binding Email}"
                VerticalOptions="CenterAndExpand" />
        </StackLayout>
    </ContentPage.Content>
    

    这是EmailConverter,值转换器类可以具有属性和泛型参数

     public class EmailConverter : IValueConverter
    {      
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string  emaildisplay = (string)value;
            string[] words = emaildisplay.Split('@');
            if(words[0]!=null)
            {
                return words[0];
            }
            return null;
           
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    enter image description here

        2
  •  1
  •   ToolmakerSteve    4 年前

    扩展@Jason的评论:

    public string PartialEmail {
        get {
            s = ... write code here to get substring you want ...
            return s;
        }
    }
    

    Text="{Binding PartialEmail}"    
    

    换言之,由您自行编码具有要显示的值的属性。