Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

web applications - Hosting and interacting with a webpage inside a WPF App

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...