如何在Xaml文本中有一个电子邮件链接
本文关键字:有一个 电子邮件 链接 文本 Xaml | 更新日期: 2023-09-27 18:01:32
我在Windows phone 8应用程序中有一些大文本,我希望里面有电子邮件链接,类似于mailto功能。下面是部分代码:
<phone:PivotItem Header="μέλη ΔΕΠ">
<ScrollViewer>
<StackPanel>
<TextBlock TextWrapping="Wrap">
<Run Text="John Doe"/>
<LineBreak/>
<Run Text="503 (Building DS 126)"/>
<LineBreak/>
<Run Text="tel.: +30 210-1234567"/>
<LineBreak/>
<Run Text="e-mail: johndoe@uni.gr"/>
</TextBlock>
</StackPanel>
</ScrollViewer>
</phone:PivotItem>
我希望"e-mail: johndoe@uni.gr"可以点击并打开手机上的邮件应用程序。在我的代码中有很多这样的情况,电子邮件的文本要大得多,这就是我使用<TextBlock><Run Text=".."/><LineBreak/>...
格式的原因。
现在我知道我不能在<Run Text=".."/>
中使用超链接按钮,所以有什么建议吗?
HyperlinkButton是很好的,如果你想在你的UI超链接,但如果你想一个超链接嵌入到文本运行,你应该使用RichTextBox与 hyperlink :
<RichTextBox TextWrapping="Wrap">
<Paragraph>
<Run Text="John Doe" />
<LineBreak />
<Run Text="503 (Building DS 126)" />
<LineBreak />
<Run Text="tel.: +30 210-1234567" />
<LineBreak />
<Hyperlink Click="Hyperlink_OnClick">e-mail: johndoe@uni.gr</Hyperlink>
</Paragraph>
</RichTextBox>
然后使用Julien回答中的handler:
private void Hyperlink_OnClick(object sender, RoutedEventArgs e) {
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.Subject = "message subject";
emailComposeTask.Body = "message body";
emailComposeTask.To = "johndoe@uni.gr";
emailComposeTask.Show();
}
祝你好运!
您还可以这样做:
重载MapUri(),因为它是为一个应用程序的URI-Associations所做的,再也不用担心处理任何点击事件了。
XAML:<Hyperlink NavigateUri="tel:0800-555-1234'}">call 0800-555-1234</Hyperlink>
<Hyperlink NavigateUri="mailto:you@your-domain.com'}">e-Mail you@your-domain.com</Hyperlink>
c# App.xaml.cs private void InitializePhoneApplication() {
…
//get ready for URI association
RootFrame.UriMapper = new AssociationUriMapper();
}
c#class AssociateioniMapper : UriMapperBase
{
public override Uri MapUri(Uri uri)
{
tempUri = System.Net.HttpUtility.UrlDecode(uri.ToString());
Uri retUri = uri;
//handle known uri schemes
if (uriUsesKnownScheme(uri))
{
Debug.WriteLine("Known Uri scheme: " + uri.Scheme);
NavigateConsideringKnownScheme(uri);
retUri = null;
}
}
private bool uriUsesKnownScheme(Uri uri)
{
try
{
switch (uri.Scheme)
{
case "http":
case "https":
case "mailto":
case "tel":
return true;
default:
return false;
}
}
catch (Exception)
{
//uri does not have a scheme
return false;
}
}
private void NavigateConsideringKnownScheme(Uri uri)
{
try
{
switch (uri.Scheme)
{
case schemeHttp:
case schemeHttps:
//open URI in IE
WebBrowserTask webBrowserTask = new WebBrowserTask();
webBrowserTask.Uri = uri;
webBrowserTask.Show();
break;
case schemeMailto:
//initiate eMail task
if (! string.IsNullOrWhiteSpace(uri.Query))
handleMailtoUrl_Query(uri.Query);
else
handleMailtoUrl_To(uri.OriginalString.Replace("mailto:", string.Empty));
break;
case schemeTel:
//initiate phonecall task
handleTel(uri.PathAndQuery);
break;
default:
break;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
private void handleTel(string parameter)
{
try
{
string p = parameter as string;
string number = p;
string displayName = string.Empty;
number = Regex.Replace(number, "[^+0-9]", "");
startPhoneCall(number, displayName);
}
catch (Exception e)
{
throw e;
}
}
private void startPhoneCall(string number, string displayName)
{
try
{
PhoneCallTask phoneCallTask = new PhoneCallTask();
phoneCallTask.PhoneNumber = number;
if (!string.IsNullOrWhiteSpace(displayName))
{
phoneCallTask.DisplayName = displayName;
}
phoneCallTask.Show();
}
catch (Exception e)
{
MessageBox.Show(LocalizationHelper.GetString("ContactPhonecallError"));
}
}
private void handleMailtoUrl_Query(string query)
{
//handle subject, to, body and other parameters if required
}
private void handleMailtoUrl_To(string to)
{
try
{
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.To = to;
emailComposeTask.Show();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
}
添加HyperLinkButton:
<HyperlinkButton Name="emailLink" Content="johndoe@uni.gr" Click="EmailLink_Click" />
和在EmailLink_Link中,您可以启动EmailComposeTask (http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh394003(v=vs.105).aspx)
private void EMailLink_Click(object sender, RoutedEventArgs e)
{
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.Subject = "message subject";
emailComposeTask.Body = "message body";
emailComposeTask.To = emailLink.Content;
emailComposeTask.Show();
}
更容易:
XAML:<HyperlinkButton Content="yourname@yourdomain.com" Click="HyperlinkButton_Click"/>
单击事件处理程序:
private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
Windows.System.Launcher.LaunchUriAsync(new Uri("mailto:yourname@yourdomain.com"));
}
除了打开你的电子邮件应用程序,还有很多你可以用Launcher: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj662937(v=vs.105).aspx