Hosting and interacting with a webpage inside a WPF App

前端 未结 2 1823

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

相关标签:
2条回答
  • 2020-12-12 18:11

    o, and you should add saved from url=(0014)about:internet comment above the head tag to make IE not block the javascript

    0 讨论(0)
  • 2020-12-12 18:14

    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>
    
    0 讨论(0)
提交回复
热议问题