在 WPF 应用程序中托管网页并与之交互 [英] Hosting and interacting with a webpage inside a WPF App

查看:31
本文介绍了在 WPF 应用程序中托管网页并与之交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为 LOB 应用程序创建一个基于 Web 的仪表板工具.本质上,用户需要能够登录到一个网站,这将允许他们查看各种数据的统计数据,以及查看与它们相关的任何通知.我们的主要应用程序是使用 WPF 为桌面构建的.我们还需要提供一个相同的仪表板,它将托管在我们的 WPF 应用程序中.主要区别在于,在 WPF 版本中,它们将具有用于打开应用程序的其他部分以更改数据或执行任何必要操作的按钮.

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.

主要问题是管理层只想编写 1 个可以在 Web 和 WPF shell 中使用的版本.他们不想写入不同版本的 UI.因此,解决方案是为 Web 编写仪表板,但将其托管在我们的桌面应用程序中供本地用户使用,并托管在浏览器中供远程用户使用.

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.

所以这是我的问题:

  1. 如何在 WPF 应用程序中托管网页
  2. 如何根据我是在 WPF 应用程序中还是在 IE 资源管理器之类的应用程序中隐藏/删除按钮?
  3. 如果在浏览器中点击链接或按钮,WPF 应用如何对这些点击做出反应并在应用内打开相关屏幕?
  4. 什么是更好的方法?

我意识到这个想法可能很糟糕,所以不要因此而责备我.我只是想用正确的方法为管理层提供服务.欢迎提出所有建议.

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.

谢谢!

推荐答案

您可以使用 .NET 3.5 SP1 中添加的 WebBrowser 控件在 WPF 应用程序中托管网页:

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>

并且在代码隐藏中,您必须将 Uri 设置为您的页面和一个将从 java 脚本调用的对象(应该是可见的):

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);
        }
    }
}

最后在您的页面中,您必须使用 window.external 调用代码,如下所示:

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>

这篇关于在 WPF 应用程序中托管网页并与之交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆