我在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>
我希望“电子邮件:johndoe@uni.gr”可以点击并打开手机上的邮件应用程序。
在我的代码中有很多这种情况,包含更大文本的电子邮件,这就是我使用<TextBlock><Run Text=".."/><LineBreak/>...
格式的原因。
现在我知道我不能在<Run Text=".."/>
内使用超链接按钮所以有任何建议吗?
答案 0 :(得分:10)
如果您想在UI中使用超链接,则HyperlinkButton很好,但如果您希望在一系列文本中嵌入超链接,则应使用 RichTextBox 并使用超链接:< / p>
<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的答案中的处理程序:
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();
}
祝你好运!
答案 1 :(得分:4)
您还可以执行以下操作:
重载MapUri(),因为它完成了应用程序的URI关联,并且从不担心再处理任何点击事件。
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);
}
}
}
答案 2 :(得分:2)
添加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();
}
答案 3 :(得分:2)
更容易:
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"));
}
除了打开您的电子邮件应用之外,您还可以使用启动器执行更多操作:http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj662937(v=vs.105).aspx