CefSharp in a window opened with ShowDialog doen't work - wpf

In WPF, I use CefSharp for WinForm that runs in a WindowsFormsHost in a Window. Everything works like charm when I display the Window using Show(). But if I use ShowDialog(), CefSharp seems not to load. The WindowsFormsHost just display it's background color.
<WindowsFormsHost Name="xamlBrowser" Grid.Row="0" Background="#f5f2e9" />
public WindowEditor() {
InitializeComponent();
if (File.Exists(Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "app"), "editor.html"))) {
ChromiumWebBrowser browser = new ChromiumWebBrowser("localfolder://cefsharp/editor.html", MainWindow.rc1); // with RequestContext for the custom scheme
xamlBrowser.Child = browser;
browser.JavascriptObjectRepository.Register(
"boundAsync",
new BoundEditorObject() { Reference = this },
true
);
browser.IsBrowserInitializedChanged += OnIsBrowserInitializedChanged;
browser.FrameLoadEnd += OnFrameLoadEnd;
}
}
new WindowEditor() { Text = "Init text" }.ShowDialog()
I need to get a value back after closing the Window.
Do you know why this isn't working using ShowDialog() ??
Thanks a lot

Related

How to bubble up keyboard events from winform hosted control

I have an interop scenario where a winform textbox is hosted within a WPF window. I'm using the windows inputbindings in order to relay commands to backend components. This is working pretty well except for commands that use the arrow keys such as Up,Down,Left,Right. When these are ran they seem to get handled by the textbox and not getting bubbled up to the main window. Is there a way to manually bubble up the keyboard command or a setting on the textbox that I could set?
To duplicate create a window with a windowformhost component and add an inputbinding. Other keygestures are handled but not any that use the arrow keys.
<Window>
<WindowsFormsHost x:Name="Tb">
</WindowsFormsHost>
</Window>
public MainWindow()
{
InitializeComponent();
this.InputBindings.Add(new InputBinding(new DelegateCommand(() =>
{
var s = '0';
}), new KeyGesture(Key.Up, ModifierKeys.Control)));
Tb.Child = new TextBox();
}
Thanks

manipulate selected text on wpf webbrowser

I'm creating a epub books reader.
After displaying the book i want to allow users to add some annotation to the book.
To display the book, I'm using a wpf webbrowser control that loads local html files
I want to manipulate selected text on this control by creating a context menu or showing a popup
i've tried to change the control's contextmenu but by searching i found that isn't possible
this is an example of what i want to do with selected text:
IHTMLDocument2 htmlDocument = (IHTMLDocument2)webBrowser1.Document;
IHTMLSelectionObject currentSelection = htmlDocument.selection;
if (currentSelection != null)
{
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
if (range != null)
{
MessageBox.Show(range.text);
}
}
WPF's native browser control will not let you set a custom context menu.
It gets even worse ; while your mouse is over the browser component, or if it has focus, it will not catch events generated by your input either.
A way around this, is to use the windows forms browser control inside a WindowsFormsHost.
To start, add Windows.Forms to your project references.
Then, do something like the following:
XAML:
<Window x:Class="blarb.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<WindowsFormsHost Name="windowsFormsHost" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</Grid>
</Window>
C# code:
public partial class MainWindow : Window
{
private System.Windows.Forms.WebBrowser Browser;
public MainWindow()
{
InitializeComponent();
//initialise the windows.forms browser component
Browser = new System.Windows.Forms.WebBrowser
{
//disable the default context menu
IsWebBrowserContextMenuEnabled = false
};
//make a custom context menu with items
System.Windows.Forms.ContextMenu BrowserContextMenu = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem MenuItem = new System.Windows.Forms.MenuItem {Text = "Take Action"};
MenuItem.Click += MenuItemOnClick;
BrowserContextMenu.MenuItems.Add(MenuItem);
Browser.ContextMenu = BrowserContextMenu;
//put the browser control in the windows forms host
windowsFormsHost.Child = Browser;
//navigate the browser like this:
Browser.Navigate("http://www.google.com");
}
private void MenuItemOnClick(object sender, EventArgs eventArgs)
{
//will be called when you click the context menu item
}
}
This does not yet explain how to do your highlighting though.
You could listen for the event fired by the browser component when it is done loading, and then replace portions of the document it loaded, injecting html code to do the highlighting.
Keep in mind that that might be tricky in some situations (when selecting text across divs, spans or paragraphs for example)
using mshtml;
private mshtml.HTMLDocumentEvents2_Event documentEvents;
in constructor or xaml set your LoadComplete event:
webBrowser.LoadCompleted += webBrowser_LoadCompleted;
then in that method create your new webbrowser document object and view the available properties and create new events as follows:
private void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
documentEvents = (HTMLDocumentEvents2_Event)webBrowserChat.Document; // this will access the events properties as needed
documentEvents.oncontextmenu += webBrowserChat_ContextMenuOpening;
}
private bool webBrowserChat_ContextMenuOpening(IHTMLEventObj pEvtObj)
{
return false; // ContextMenu wont open
// return true; ContextMenu will open
// Here you can create your custom contextmenu or whatever you want
}

How to open a Xaml page in a separate window Other than MainPage.Xaml

All the below functionality should be worked in Out-of-Browser mode.
On start of my project there is login page after login in the MainPage.xaml there is a button,on click of this button a new page should be opened with WebBrowser control in the page there is grid control,as the grid selected item changes it should be reflected to the 1st window.
Please help me
Thanks
I think it's not possible in silverlight 4.
So I updated my project to silverlight 5 and use Window class for out-of-browser app. to achieve my requirement
WebBrower wb = new WebBrowser();
wb.ScriptNotify += new EventHandler<NotifyEventArgs>(b_ScriptNotify);
Window win = new Window();
win.Height = 870;
win.Width = 600;
win.Title = "Site Map";
win.Content = wb;
win.Visibility = Visibility.Visible;
void b_ScriptNotify(object sender, NotifyEventArgs e)
{
MessageBox.Show(Convert.ToInt32(e.Value).ToString());
}

Open an xaml file inside a popup window in silverlight

I need to open an xaml file inside a popup window in silverlight.
In my case i am having two xaml files namely a.axml and b.axml.,
In a.xaml, there is a hyper link button and a popup tag inside it.
I have to open the b.xaml page inside that popup window on clicking the hyperlink of the a.xaml page.
This is my scenario. Pls help me to solve this issue.
thanks,
Neon
You should be able to set the Child property of the popup to be a new instance of your b.xaml class (whatever that is).
Using Telerik controls its getting easier. But you may use second method.
On Blend I changed the template of Telerik's child window. It seems better now
HyperlinkButton link = new HyperlinkButton();
link.Click += (s, a) =>
{
//With telerik
RadWindow w = new RadWindow();
w.Content=new UserControl();
w.Show();
w.ShowDialog();//Swith light and avoid access oter controls
//Without Telerik
Popup p = new Popup();
p.Child = w.Content as UserControl;
p.IsOpen = true;
};
Parent Page XAML:
<HyperlinkButton Content="HyperlinkButton" Height="23" HorizontalAlignment="Left" Margin="150,111,0,0" Name="hyperlinkButton1" VerticalAlignment="Top" Width="100" Click="hyperlinkButton1_Click" />
Code Behind :
public ParentPage()
{
InitializeComponent();
}
private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
{
ChildPage objpage = new ChildPage();
objpage.Show();
}
Here Is Pararent Page have buuton . child page load based on That button Click

Modeless Child WPF Window to a native MFC MDI Application

I have an MFC MDI application and I am trying to add a new dialog to it. I want this dialog to be in WPF (a Window basically rather than a dialog). This window should be modeless and a child to the current MDI View.
Let's say I have CMyView in the MFC application, and in its OnCreate, I try to create the WPF Window. To do so, I made a wrapper class called CMyWindowWrapper (that compiles with /CLR)
int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
m_wrapper.Create(this);
return 0;
}
The window wrapper class has a Create function which actually creates the WPF Window:
void CMyWindowWrapper::Create(CWnd* pParent)
{
MyWindow^ window = gcnew MyWindow();
window->ShowModeless((IntPtr)pParent->GetSafeHwnd());
m_myWindow = window;
}
MyWindow is the WPF Window where I added a function called ShowModeless as follows:
public void ShowModeless(IntPtr parent)
{
WindowInteropHelper helper = new WindowInteropHelper(this);
helper.Owner = parent;
Show();
ShowInTaskbar = false;
}
Now the application behaves as follows: whenever a CMyView is created, a modeless MyWindow is created successfully, and it appears always on top of CMyView even if the focus is on CMyView. However, when CMyView is closed or minimized, MyWindow is not following it. It gets close/minimized only if the whole application gets closed/minimized.
I can attach a sample application showing the problem if needed.
Please advise.
Thank you so much.
An alternative solution would be to make your WPF window a user control. Create a MFC modeless dialog and put the WPF user control in the MFC modeless dialog.

Resources