如何解决xamarin 表单 - 将文本绑定到 xaml 中的标签 - 我可以剪掉文本的结尾吗?
晚安,
<Label Grid.Column="1"
HorizontalOptions="Center"
VerticalOptions="Center"
Text="{Binding Email}"/>
JohnnyRambo@JR.com, MovesLikeJagger@MJ.com,HugsForFree@HF.com
约翰尼·兰博, 动作像贾格尔, 拥抱免费
感谢您的回复
解决方法
扩展@Jason 的评论:
public string PartialEmail {
get {
s = ... write code here to get substring you want ...
return s;
}
}
然后在 XAML 中:
Text="{Binding PartialEmail}"
换句话说,由您来编写具有您想要显示的值的属性。
一般来说,最好让 UI (XAML) 只关注 UI;任何值的计算或操作都应该在代码 (cs) 中。
,根据 Jason 的意见,您可以使用 Xamarin.Forms Binding Value Converters 删除域名。我做了一个样本,你可以看看:
首先,创建一个显示 Eamil 或其他信息的模型类。
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,CultureInfo culture)
{
throw new NotImplementedException();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。