Wpf WebBrowser navigate throws COM exception - wpf

This is the code I was using, when I use webbrowser navigate to a page, it throws a COM exception. Is there any help here? Thanks.

Based on your comments in our conversation, your WebBrowser control is not loaded at the time you're trying to navigate. Therefore, you need to create a Loaded event handler and place your navigation code into that.
webBrowser.Loaded += WebBrowser_Loaded; // or in XAML
void WebBrowser_Loaded(object sender, RoutedEventArgs e)
{
// your navigation code here or set a flag
}
If you don't wish to go through the event route, you must, at the very least, check if the control IsLoaded prior to attempting navigation (in your particular scenario).
If the issue persists, create a new instance of the WebBrowser control and navigate through that.
webBrowser = new WebBrowser();

Related

'DeferRefresh' is not allowed during an AddNew or EditItem transaction

I have a tab control in the GUI and there is WPF 4.0 datagrid in one of the tabs. When I click on a cell in the grid and edit something and then switch tabs, I get a Defer Refresh error:
DeferRefresh' is not allowed during an AddNew or EditItem transaction.
So I call datagrid.CancelEdit(DataGridEditingUnit.Row) when tab is switched to cancel any pending edit and the Defer refresh issue is gone.
But what I really want to do is CommitEdit() so that the user doesn't have to reenter the data again.
And datagrid.CommitEdit(DataGridEditingUnit.Row, true) doesn't work for me.
I get the below error on CommitEnd():
Cannot perform this operation while dispatcher processing is
suspended.
PS: I have tried datagrid.CommitEdit() and datagrid.CommitEdit(DataGridEditingUnit.Column, true) and it didnt work.
I solved this by adding this handler for the DataGrid's Unloaded event:
void DataGrid_Unloaded(object sender, RoutedEventArgs e)
{
var grid = (DataGrid)sender;
grid.CommitEdit(DataGridEditingUnit.Row, true);
}
I've run in to this before. WPF only keeps the current tab's view in memory; when you switch tabs, WPF unloads the current view and loads the view of the selected tab. However, DataGrid throws this exception if is currently executing a AddNew or EditItem transaction and WPF tries to unload it.
The solution for me was to keep all the tab views in memory, but only set the current tab's view to visible. This link shows a method of doing this:
WPF TabControl - Preventing Unload on Tab Change?
This change will also make your tabs load more smoothly when you switch between them because the view doesn't have to be regenerated. In my case, the extra memory usage was a reasonable trade-off.
In Xaml :
Loaded="OnUserControlLoaded"
Unloaded="OnUserControlUnloaded"
In Code Behind Inside OnUserControlLoaded and OnUserControlUnloaded Methods:
dataGrid.CommitEdit()
dataGrid.CancelEdit()
I just solved a similar problem by "commiting" changes to the DataTable that is my source of datas.
So if you have a DataTable in the source you can try the following code :
DataTableSource.AcceptChanges();
I've fixed that issue by adding this piece of code:
private void tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (tabControl.SelectedIndex == 1)
{
WPFdataGrid.CancelEdit(DataGridEditingUnit.Row);
}
}
I think it's a problem of UI threads.

UI freezed instead of showing BusyIndicator in Silverlight

This is a side-related question to this other question:
BackgroundWorker in Silverlight ViewModel
I have a TabControl where I load many TabItems when the user selects menu options. I load this Tabs by binding the TabControl ItemsSource to an ObservableCollection. When I add a new TabItem to this Collection, it is shown perfectly.
The problem is I've realized that since user press a button until the tab is created (ViewModel and View creation takes a couple of seconds), the screen is freezed.
I've tried to set "IsBusy" before calling the "loadTab" but it doesn't shows up... I've tried almost everything with async calls but the UI thread is in use and it throws an exception when I create the new tab control.
Is there any trick I'm loosing??? Any ideas??? Thanks in advance.
have you seen this post?
http://www.dotnetspark.com/kb/3524-doesnt-your-girlfriend-deserves-more-time.aspx
It helps when you avoid heavy stuff in the load event and make Visible=true after you finish to load all your resources, so in that sense you avoid the user feeling tempted to click something that is not ready yet.
Not sure if it helps, but how about this idea?
public void DoStuff(Object values)
{
//your values object could be anything,
//they might even be some objects from your form
//as long as you dont modify them in the other thread
imgLoading.Visible=true;
var client = new Proxy();
client.OnWorkCompletedAsync +=client_OnCompleted() ;
client.Work(values);
}
void client_OnCompletedAsync(object sender, EventArgs e)
{
imgLoading.Visible=false;
//now you can update the UI with other stuff
}

Creating a mailto: hyperlink in Silverlight Out Of Browser app

Is there a way to create a mailto: hyperlink in a Silverlight 4 OOB app? Thanks!
Edit: based on some of the discussion, an acceptable answer would be a different way than using the HyperlinkButton, or a way to use the HyperlinkButton without having the extra popup in IE.
Ideally it would have been nice of you to post some code, as I have no idea whether the email address is known/determined at design-time or run-time, but nonetheless:
In XAML:
<HyperlinkButton x:Name="mailButton" NavigateUri="mailto:somedude#example.com" TargetName="_blank"></HyperlinkButton>
In C#:
HyperlinkButton hbtn = new HyperlinkButton();
hbtn.Name = "mailButton";
hbtn.TargetName = "_blank";
hbtn.NavigateUri = new Uri("mailto:somedude#example.com");
parent.Controls.Add(hbtn);
In a situation in which you don't know the email address at design time, it's relatively straightforward to assign the value of the NavigateUri property within a method.
I posted a solution to this issue on CodeProject
http://www.codeproject.com/Answers/383879/Silverlight-mailto-HyperlinkButton-always-opens-an#answer2
Essentially, instead of using the default behaviour add a click event and then call javascript location.href. This stops the extra browser window opening.
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);
}

How to get Control values from a web application that has been opened thru WPF WebBrowser Control(C#)

I have a problem.
I am calling an external application(web) through a WPF WebBrowser Control. The
application is opening properly. Now in the web application, there are certain hyperelinks.
If a user click on that hyperlink, I should be able to trap it's value.
How to achieve the same using this WEB BROWSER.. I mean, in which event , what code I need to write to get it work
Using C#3.0, WPF and Dotnet framework 3.5
Thanks
Try this.
private void _webBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (string.IsNullOrEmpty(e.Uri.AbsolutePath))
{
e.Cancel = true;
string actionName = e.Uri.LocalPath;
MessageBox.Show(actionName);
}
}
Regards,
Nanda

RTF with Links in a RichTextBox WPF

I am able to load an rtf document in a RichTextBox, but the links that the document contains to some websites are not working.
Anyone have any idea why? Some solution to make the links work?
Best regards,
Paulo Azevedo
WPF by default doesn't understand where you want the links to be displayed, so what's happening is that the Hyperlink class is firing an event, RequestNavigate, and expecting you, the application designer, to cause the actual navigation to occur.
I assume you just want to launch the system configured web browser, so here's all you need to do:
Hook the Hyperlink::RequestNavigate routed event
Call Process.Start with the URL you receive to have the OS launch the browser.
That might look a little something like this:
public class MyWindow : Window
{
public MyWindow()
{
this.InitializeComponent();
this.myRichTextBox.AddHandler(Hyperlink.RequestNavigate, MyWidow.HandleRequestNavigate);
}
private static void HandleRequestNavigate(object sender, RequestNavigateEventArgs args)
{
Process.Start(args.Uri.ToString());
}
}

Resources