Thursday, May 9, 2013

How to display webpage in WPF?

This is quite easy. Just use “WebBrowser” control for the same.

<Grid Grid.Row="2" >
          <WebBrowser Source="http://www.google.com" />
</Grid>


This will show google default page into the window.
To refresh this on button pressed, implement following command.

ICommand _refreshLogsWebPage;

    public ICommand RefreshLogsWebPage
    {
      get
      {
        if (_refreshLogsWebPage == null)
          _refreshLogsWebPage = new RelayCommand(param => this.RefreshLogsWebPage_Execute(param),
            param => this.RefreshLogsWebPage_CanExecute(param));
        return _refreshLogsWebPage;
      }
    }

    bool RefreshLogsWebPage_CanExecute(object param)
    {
      return true;
    }

    void RefreshLogsWebPage_Execute(object param)
    {
      try
      {
        System.Windows.Controls.WebBrowser logsWebPage = param as System.Windows.Controls.WebBrowser;
        if (logsWebPage == null)
          return;

        logsWebPage.Source = new Uri("http://google.com");

      }
      catch (Exception)
      {

        throw;
      }
    }


Here we used RelayCommands class to register our custom commands in MVVM pattern.

RelayCommands will look like :

/// <summary>
    /// To register commands in MMVM pattern
    /// </summary>
    public class RelayCommand : ICommand
    {
        readonly Action<object> _execute;
        readonly Predicate<object> _canExecute;

        /// <summary>
        /// Constructer takes Execute events to register in CommandManager.
        /// </summary>
        /// <param name="execute">Execute method as action.</param>
        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
            try
            {
                if (null == execute)
                {
                    throw new NotImplementedException("Not implemented");
                }

                _execute = execute;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// Constructer takes Execute and CanExcecute events to register in CommandManager.
        /// </summary>
        /// <param name="execute">Execute method as action.</param>
        /// <param name="canExecute">CanExecute method as return bool type.</param>
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            try
            {
                if (null == execute)
                {
                    _execute = null;
                    throw new NotImplementedException("Not implemented");
                }

                _execute = execute;
                _canExecute = canExecute;
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// Can Executed Changed Event
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
            }
            remove
            {
                CommandManager.RequerySuggested -= value;
            }
        }

        /// <summary>
        /// Execute method.
        /// </summary>
        /// <param name="parameter">Method paramenter.</param>
        public void Execute(object parameter)
        {
            _execute(parameter);
        }

        /// <summary>
        /// CanExecute method.
        /// </summary>
        /// <param name="parameter">Method paramenter.</param>
        /// <returns>Return true if can execute.</returns>
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }
    }


Hope this will help you all. J
Thanks, Enjoy!!!

No comments:

Post a Comment