Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hosting and interacting with a webpage inside a WPF App

Tags:

I need to create a Web-based Dashboard tool for a LOB application. Essentially users need to be able to log in to a web-site which will allow them to view stats for various bits of data, as well as see any notifications that pertain to them. Our primary application is built for the desktop using WPF. We also need to provide an identical dashboard that will be hosted in our WPF app. The main difference being that in the WPF version they will have buttons that will open up other parts of the application to make changes to the data, or perform whatever actions are necessary.

The main issue is that management only wants to write 1 version that can be used both on the web and in the WPF shell. They don't want to write to different version of the UI. So the solution would be to write the dashboard for the web, but host it inside our desktop application for local users, and in the browser for remote users.

So here's my questions:

  1. How do I go about hosting a web page inside my WPF app
  2. How could I hide/remove buttons based on whether I'm inside my WPF app vs. inside something like IE explorer?
  3. If links or buttons are clicked inside the browser, how can the WPF app react to those clicks and open the pertinent screen inside the app?
  4. What's a better approach?

I realize this idea is probably bad so don't flame me for it. I'm simply trying to supply management with the right approach. All suggestions are welcome.

Thanks!

like image 974
Micah Avatar asked Jan 22 '09 18:01

Micah


People also ask

How do I navigate to a page in WPF?

To package content for navigation, WPF provides the Page class. You can navigate from one Page to another declaratively, by using a Hyperlink, or programmatically, by using the NavigationService. WPF uses the journal to remember pages that have been navigated from and to navigate back to them.

Can WPF be targeted to Web browser?

All replies. WPF only runs on windows. You can make a type of wpf application called xbap which runs in a browser.

How do I add a page in WPF?

Step 1: Create an empty WPF using Visual Studio, enter the name for the application and click on OK. Step 2: Create a button using the following code or drag and drop a button from the ToolBox of Visual Studio 2013.

How do I navigate between windows in WPF?

NavigationService is for browser navigation within WPF. What you are trying to do is change to a different window TrainingFrm . To go to a different window, you should do this: private void conditioningBtn_Click(object sender, RoutedEventArgs e) { var newForm = new TrainingFrm(); //create your new form.


1 Answers

You can host a web page in a WPF application using the WebBrowser controls that was added in .NET 3.5 SP1:

<Grid>     <WebBrowser Name="browser" /> </Grid> 

And in the code-behind you have to set the Uri to your page and an object (which should be com-visible) that is to be called from the java script:

public partial class Window1 : Window {     public Window1()     {         InitializeComponent();          string uri = AppDomain.CurrentDomain.BaseDirectory + "TestPage.html";         this.browser.Navigate(new Uri(uri, UriKind.Absolute));         this.browser.ObjectForScripting = new ScriptingHelper();     }      [ComVisible(true)]     public class ScriptingHelper     {         public void ShowMessage(string message)         {             MessageBox.Show(message);         }     } } 

And finally in your page you must call the code using window.external like below:

<head>     <title></title>      <script type="text/javascript">         function OnClick()         {             var message = "Hello!";             window.external.ShowMessage(message);         }     </script> </head> <body>     <a href="#" onclick="OnClick()">Click me</a> </body> 
like image 170
Yordan Pavlov Avatar answered Sep 27 '22 18:09

Yordan Pavlov