Silverlight mailto:HyperlinkButton始终会打开一个附加的浏览器窗口 [英] Silverlight mailto: HyperlinkButton always opens an additional browser window
问题描述
我有以下问题:
在Silverlight 5应用程序(IE9,VS2010,托管在WebApplication中)中,我创建了一个无辜的HyperlinkButton,让用户发送电子邮件.但是无论我执行此命令如何,总是会打开另一个浏览器窗口/选项卡(邮件客户端也已打开-很好,我希望发生的事情).
超链接按钮的定义如下(没什么特别的):
Hi,
I have following problem:
In an Silverlight 5 Application (IE9, VS2010, Hosted in WebApplication) I created an innocent HyperlinkButton to let the user send an email. But whatever I do this command always opens another browser-window/tab (the mail-client is opened too - thats fine and what I expect to happen).
The Hyperlink-Button is defined like this (nothing special):
<HyperlinkButton x:Name="TestLink"
Content="TestLink"
Click="TestLink_Click">
</HyperlinkButton>
这是Click-Handler的代码
here is the code for the Click-Handler
private void TestLink_Click(object sender, RoutedEventArgs e)
{
System.Windows.Browser.HtmlPage.Window.Navigate(
new Uri("mailto:test@test.com")); // I use a real email-address
}
我还通过评估javascript使用了另一种方法-结果相同(会打开其他浏览器窗口)
I also used another approach by evaluating a javascript - same result (the additional browser window opens)
public void MailTo(string toEmail, string subject, string body)
{
String strCmd = String.Format("window.open('mailto:{0}?subject={1}&body={2}');",
toEmail, subject, body);
HtmlPage.Window.Eval(strCmd );
}
我尝试的其他方法:在同一XAML页上使用HyperlinkButton的TargetName
属性和值""
,"_self"
,"_blank"
和Navigation.Frame
I主机的名称->相同的结果(并且我还尝试通过匹配的Navigate
重载来设置这些值.)
当然,如果我尝试了最明显的方法:在XAML中设置HyperlinkButton的NavigateUri
属性-它甚至没有打开邮件客户端...
我希望你们中的一个知道这个问题,并且我不必为这个简单的事情浪费更多的时间...
Other things I tried: Using HyperlinkButton''s TargetName
property with values ""
, "_self"
, "_blank"
and the Name of a Navigation.Frame
I host on the same XAML page -> same result (and I also tried to set these values with the matching Navigate
overload.)
And of course if I tried the most obvious thing: setting HyperlinkButton''s NavigateUri
property in XAML - it even didn''t open the mail-client...
I hope one of you knows about this problem, and I have not to waste more hours for such a simple thing...
推荐答案
我终于找到了解决方案.解决这个问题.
代替使用Window.Navigate,使用javascript location.href.
I finally found a solution. to this issue.
Instead of using Window.Navigate, use javascript location.href.
private void TestLink_Click(object sender, RoutedEventArgs e)
{
//Only run the click event if in browser because this will not work OOB
if (Application.Current.IsRunningOutOfBrowser)
return;
var cmd = String.Format("location.href='mailto:test@test.com?subject=blah&body=something';",
HtmlPage.Window.Eval(cmd);
}
注意:由于HtmlPage不可用,OOB不能工作,但是mailto的默认行为可以正常工作,因此在构造函数中,如果OOB仅绑定NavigateUri.
在我的项目中,我创建了一个控件来包装所有内容,以便可以将电子邮件,主题和正文绑定在xaml中.
在这里..
Note: This wont work OOB as HtmlPage is not available, however the default behaviour for the mailto works as it should so in your constructor only bind the NavigateUri if OOB.
In my project I created a control to wrap all this up so I could bind the email, subject and body in xaml.
Here it is..
/// <summary>
/// This component allows us to display an email address as a hyperlink that the user can click and it will open a new email window
/// in their default email client. It can handle in browser and out of browser
/// It gets around the issue which we previously were experiencing where a new browser window would open and an email window.
/// </summary>
public class MailToButton : HyperlinkButton
{
/// <summary>
/// Email dependency property
/// </summary>
public static readonly DependencyProperty EmailProperty = DependencyProperty.Register("Email",
typeof(string),
typeof(
MailToButton
), null);
/// <summary>
/// Email Address
/// </summary>
public object Email
{
get { return GetValue(EmailProperty); }
set { SetValue(EmailProperty, Convert.ToString(value)); }
}
/// <summary>
/// Subject dependency property
/// </summary>
public static readonly DependencyProperty SubjectProperty = DependencyProperty.Register("Subject",
typeof(string),
typeof(
MailToButton
), null);
/// <summary>
/// Email Subject
/// </summary>
public object Subject
{
get { return GetValue(SubjectProperty); }
set { SetValue(SubjectProperty, Convert.ToString(value)); }
}
/// <summary>
/// Body dependency property
/// </summary>
public static readonly DependencyProperty BodyProperty = DependencyProperty.Register("Body",
typeof(string),
typeof(
MailToButton
), null);
/// <summary>
/// Hide the NavigateUri property in the base, we will set this manually in code.
/// </summary>
public new Uri NavigateUri { get; set; }
/// <summary>
/// Email Body
/// </summary>
public object Body
{
get { return GetValue(BodyProperty); }
set { SetValue(BodyProperty, Convert.ToString(value)); }
}
/// <summary>
/// Constructor
/// </summary>
public MailToButton()
{
Loaded += MailToButton_Loaded;
Click += MailToButtonClicked;
}
private void MailToButton_Loaded(object sender, RoutedEventArgs e)
{
//Only bind the NavigateUrl when OOB as this method does not work in browser
if (Application.Current.IsRunningOutOfBrowser)
base.NavigateUri = new Uri(BuildMailToString());
}
private void MailToButtonClicked(object sender, RoutedEventArgs e)
{
//Only run the click event if in browser because this will not work OOB
if (Application.Current.IsRunningOutOfBrowser)
return;
if (string.IsNullOrEmpty(Convert.ToString(Email)))
return;
HtmlPage.Window.Eval(BuildMailToString());
}
/// <summary>
/// Constructs a MailTo string using the email, subject and body. It url encodes the email, subject and body
/// </summary>
/// <returns>A mailto string</returns>
private string BuildMailToString()
{
var email = HttpUtility.UrlEncode(Convert.ToString(Email));
var subject = HttpUtility.UrlEncode(Convert.ToString(Subject));
var body = HttpUtility.UrlEncode(Convert.ToString(Body));
var sb = new StringBuilder();
sb.AppendFormat("location.href='mailto:{0}?", email);
if (!string.IsNullOrEmpty(subject))
sb.AppendFormat("subject={0}", subject);
if (!string.IsNullOrEmpty(body))
{
if (!string.IsNullOrEmpty(subject))
sb.Append("&");
sb.AppendFormat("body={0}", body);
}
sb.Append("'");
return sb.ToString();
}
}
,您并没有想到要看起来 ^ ]?并且此处 [
And you didn''t think to look here[^]? And here[^] ...
这篇关于Silverlight mailto:HyperlinkButton始终会打开一个附加的浏览器窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!