Issue
Evening All,
Have a label...
<Label Grid.Column="1"
HorizontalOptions="Center"
VerticalOptions="Center"
Text="{Binding Email}"/>
So yeah, this works fine...However it displays the full email...
[email protected], [email protected], [email protected]
Q being can I remove the domain name from the xaml page? to output...
JohnnyRambo, MovesLikeJagger, HugsForFree
Thanks for any replies
Solution
As Jason's opinion, you can use Xamarin.Forms Binding Value Converters to remove the domain name. I do one sample that you can take a look:
Firstly, create one model class that display Eamil or other info.
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="[email protected]"},
new persominfo(){Name="MJ",Email="[email protected]"},
new persominfo(){Name="HF",Email="[email protected]"}
};
this.BindingContext = this;
}
}
Then I use ListView to display these info, using data binding to convert any Eamil property into a string that you want.
<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>
This is EmailConverter ,Value converter classes can have properties and generic parameters
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();
}
}
Answered By - Cherry Bu - MSFT
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.