Silverlight InvalidOperationException when clicking a link - silverlight

I have a dynamically generated hyperlink which when clicked should open a lotus notes document. I do it using the code below.
HyperlinkButton hlb = new HyperlinkButton();
hlb.SetBinding(HyperlinkButton.ContentProperty, new Binding("Properties[" + col.DisplayField + "]"));
hlb.SetBinding(HyperlinkButton.NavigateUriProperty, new Binding("Properties[" + col.LinkField + "]"));
hlb.Click += new RoutedEventHandler(hlb_Click);
RootGrid.Children.Add(hlb);
this is the code that fires when the link is clicked.
static void hlb_Click(object sender, RoutedEventArgs e)
{
HyperlinkButton hlb = (HyperlinkButton)sender;
var hostingWindow = HtmlPage.Window;
hostingWindow.Navigate(hlb.NavigateUri);
}
the lotus notes document opens correctly but I get a System.InvalidOperationException, the details of which are given below
Description: Failed to navigate to notes://<path to the document>
Stacktrace:
at MS.Internal.NavigationHelper.Navigate(Boolean checkUserInitiatedAction)
at System.Windows.Controls.HyperlinkButton.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
Another interesting thing to note is that it is raised on another thread and hence is not caught when the hostingWindow.Navigate method is fired.
Any ideas ?

Using Silverlight 5, I wrapped the call to open the Lotus Notes doc link within a task and was able to open the link without generating an error.
private void TryOpenDocLink()
{
TaskScheduler ts = TaskScheduler.Default;
Task<bool> task = OpenDocLink();
task.ContinueWith(t =>
{
if (t.Exception != null)
{
this.SetError(t.Exception.Message, enMessageLevel.Error);
}
});
}
private Task<bool> OpenDocLink()
{
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
try
{
var hostWindow = HtmlPage.Window;
hostWindow.Navigate(new Uri(DocLinkPath));
tcs.SetResult(true);
}
catch (Exception)
{
tcs.SetResult(false);
}
return tcs.Task;
}

Try marking the click event as handled:
static void hlb_Click(object sender, RoutedEventArgs e)
{
e.Handled = true;
HyperlinkButton hlb = (HyperlinkButton)sender;
var hostingWindow = HtmlPage.Window;
hostingWindow.Navigate(hlb.NavigateUri);
}
I'm not sure that this will fix the issue. The error is coming from the click event code inside the hyperlink button. You can tell because that code uses the NavigationHelper class while the Window.Navigate method does not.
Is there a reason you're not just letting the hyperlink button do the navigation?

Related

How to hide the Download dialog in WebView2 while downloading page content or printing with "Save as PDF" option?

I know how to subscribe to the CoreWebview2.DownloadStarting event and use handled = true to stop the Download dialog from showing while a download (i.e. an image) is being made, but the problem is the DownloadStarting event never fires if you right click on a web page and choose "Save as" or "Print > Save as PDF", even though the Download dialog will appear as if a regular download was being made. Does anyone know any workaround for this?
My code:
public Form1()
{
InitializeComponent();
}
private async void button1_Click(object sender, EventArgs e)
{
if (webView != null)
webView.Dispose();
webView = new WebView2();
await webView.EnsureCoreWebView2Async();
webView.CoreWebView2.DownloadStarting += CoreWebView2_DownloadStarting;
panel1.Controls.Add(webView);
webView.Dock = DockStyle.Fill;
webView.Source = new Uri("https://www.microsoft.com");
}
private void CoreWebView2_DownloadStarting(object sender, CoreWebView2DownloadStartingEventArgs e)
{
e.Handled = true;
}
I just found a solution:
subscribe to CoreWebView2.IsDefaultDownloadDialogOpenChanged event:
webView.CoreWebView2.IsDefaultDownloadDialogOpenChanged += webView_CoreWebView2_IsDefaultDownloadDialogOpenChanged;
Close the Download dialog if it's open:
private void webView_CoreWebView2_IsDefaultDownloadDialogOpenChanged(object sender, object e)
{
if (webView.CoreWebView2.IsDefaultDownloadDialogOpen) webView.CoreWebView2.CloseDefaultDownloadDialog();
}

Stop further link navigating inside web browser control [WPF]

Hello is there something similar to AllowNavigation like in WinForms?
My search didn't yield any satisfying result.
Basically I'm trying to open a webpage inside new wpf window and stop user from clicking random links on that webpage and navigating further.
Saw something with
void browser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
}
Rest of the code is :
public Popup_webpage(string ime)
{
InitializeComponent();
browser1.LoadCompleted += browser1_LoadCompleted;
browser1.Navigating += browser1_Navigating;
string uri = "www.google.com"
browser1.Navigate(new Uri(uri, UriKind.Absolute));
}
void browser1_LoadCompleted(object sender, NavigationEventArgs e)
{
browser1.Visibility = Visibility.Visible;
}
But it just makes my webpage not display ?
Thanks
Try this:
Once you have loaded your page, then you assign the browser_Navigating event handler.
public Popup_webpage(string ime)
{
InitializeComponent();
browser1.LoadCompleted += browser1_LoadCompleted;
string uri = "www.google.com"
browser1.Navigate(new Uri(uri, UriKind.Absolute));
browser1.Navigating += browser1_Navigating;
}
void browser1_LoadCompleted(object sender, NavigationEventArgs e)
{
browser1.Visibility = Visibility.Visible;
}
void browser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
}

dynamically add webbrowser control in windows chat application:

I am developing windows form chat application and i am writing message in richtextbox but when i click on send button and every message show in webbrowser control. .i want to add webbrowser control dynamically for individual message is send.i am using following code:
namespace WindowsFormsApplication1
{
[ComVisible(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// this.w1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(w1_DocumentCompleted);
}
WebBrowser w1 = new WebBrowser();
private void button1_Click(object sender, EventArgs e)
{
this.Controls.Add(w1);
HtmlElement div = w1.Document.GetElementById("abc");
div.InnerHtml = richTextBox1.Text;
}
private void Form1_Load(object sender, EventArgs e)
{
w1.Navigate(Path.Combine(Environment.CurrentDirectory, "HTMLPageForScripting.htm"));
}
}
}
Here is the case you asked for:
Add a Panel to form to put browser in and set its autoscroll property to true;
In button1 Click event, create browser, set its width and height, and navigate to the document you want.
In button1 Click event subscribe for DocumentCompleted event and add content there. We do add here because we should be sure all contents containing <div id="abc"></div>has been loaded.
Here is code:
private void button1_Click(object sender, EventArgs e)
{
var browser = new WebBrowser();
browser.Height = 100;
browser.Dock = DockStyle.Top;
browser.Navigate(#"D:\test.html");
browser.DocumentCompleted += browser_DocumentCompleted;
this.panel1.Controls.Add(browser);
//Just do it to put it at the end of list and scroll to it
browser.BringToFront();
this.panel1.ScrollControlIntoView(browser);
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var browser = (WebBrowser)sender;
HtmlElement div = browser.Document.GetElementById("abc");
div.InnerHtml = richTextBox1.Text;
}

New tab in WPF - items.add from buttons vs methods

OK, so I have a window called PictureWindow which displays pictures (I've cut out the code not related to making tabs). The TabControl is named "itemsTab". Using a button press, I can make a new tab no problem. But using the same operations inside a called method doesn't work. Using the buttonTab_Click method makes a new tab, the newTab method does not.
The only real difference I can see is due to the sender and RoutedEventArgs objects - how do these effect the operation here? Or is there something else I'm missing?
Thanks in advance.
Edit To make things even stranger, the newTab method does make a new tab, but only if it is called in the PictureWindow constructor method. If I have the following a new tab is made.
public PictureWindow(string current)
{
InitializeComponent();
newTab(current);
}
But if I call the method anywhere else it doesn't work.
public partial class PictureWindow : Window
{
public PictureWindow(string current)
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void buttonClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void buttonTab_Click(object sender, RoutedEventArgs e)
{
TabItem newTab = new TabItem();
newTab.Header = "New Tab!";
itemsTab.Items.Add(newTab);
}
public void newTab(string current)
{
TabItem newTab = new TabItem();
itemsTab.Items.Add(newTab);
}
}

Silverlight Datagrid Refresh

So I have a datagrid in Silverlight that is bound to a WCF that populates a list of class. I basically a pass a parameter to a Linq query. When I do a second query I get double the results, a third triple and so forth. What can I do to make it so when I send a call out to the service that I only get one set of results. I have attached my code in case it helps anyone.
private void button1_Click(object sender, RoutedEventArgs e)
{
dgOrder.ItemsSource = null;
Uri address = new Uri(Application.Current.Host.Source, "../Services/Service1.svc");
//var client = new Services.dataserviceClient("CustomBinding_dataservice", address.AbsoluteUri);
var client = new ServiceReference2.Service1Client("CustomBinding_Service1", address.AbsolutePath);
client.GetOrderCompleted += (s, ea) =>
{
dgOrder.AutoGenerateColumns = false;
//dgOrder.ColumnWidth.Value = 100;
dgOrder.Columns.Add(CreateTextColumn("SKU", "SKU"));
dgOrder.Columns.Add(CreateTextColumn("productname", "Product Name"));
dgOrder.Columns.Add(CreateTextColumn("itemnumber", "Item Number"));
dgOrder.Columns.Add(CreateTextColumn("cost", "Cost"));
dgOrder.Columns.Add(CreateTextColumn("asin", "ASIN"));
dgOrder.Columns.Add(CreateTextColumn("pendingorder", "Rank"));
dgOrder.Columns.Add(CreateTextColumn("rank", "Node"));
//dgOrder.Columns.Add(CreateTextColumn("w4", "AMZN"));
dgOrder.Columns.Add(CreateTextColumn("amazon", "AMZN"));
dgOrder.Columns.Add(CreateTextColumn("ourprice", "OurPrice"));
dgOrder.Columns.Add(CreateTextColumn("bbprice", "BuyBox"));
dgOrder.Columns.Add(CreateTextColumn("afner", "AFN"));
dgOrder.Columns.Add(CreateTextColumn("quantity", "INV"));
dgOrder.Columns.Add(CreateTextColumn("w4", "W4"));
dgOrder.Columns.Add(CreateTextColumn("w3", "W3"));
dgOrder.Columns.Add(CreateTextColumn("w2", "W2"));
dgOrder.Columns.Add(CreateTextColumn("w1", "W1"));
dgOrder.Columns.Add(CreateTextColumn("order", "Order"));
dgOrder.Columns.Add(CreateTextColumn("total", "Total"));
dgOrder.Columns.Add(CreateTextColumn("profit", "Profit"));
dgOrder.Columns.Add(CreateTextColumn("percent", "Percent"));
dgOrder.Columns.Add(CreateHyperlink("asin"));
dgOrder.ItemsSource = ea.Result;
Original = ea.Result;
};
client.GetOrderAsync(txtCompany.Text);
}
The problem is , you are creating a new(duplicate) event handler every time you press the Button. Due to having an extra event for each button pres you do, you get extra sets of data. You need to create your Event.Completed method outside the Button.Cliked event.
To clarify:
public partial class NewPage : Page
{
Uri address = new Uri(Application.Current.Host.Source, "../Services/Service1.svc");
ServiceReference2.Service1Client client = new ServiceReference2.Service1Client("CustomBinding_Service1", address.AbsolutePath);
public NewPage()
{
client.GetOrderCompleted += (s, ea) =>
{
//YOUR CODE
};
}
private void button1_Click(object sender, RoutedEventArgs e)
{
dgOrder.ItemsSource = null;
client.GetOrderAsync(txtCompany.Text);
}
}

Resources