wp7 webbrowser 添加依赖属性content

来源:互联网 发布:php编译安装5.6.31 编辑:程序博客网 时间:2024/06/05 16:36

using Microsoft.Phone.Controls;

using System;

using System.Windows;

namespace CommonUI.AttachedProperty

{

   ///<summary>

   /// Atttached property for WebBrowser control, bind a html string to display as web page in web browser

   ///</summary>

   publicclassWebBrowserContentBinding

    {

       ///<summary>

       /// attached property Content

       ///</summary>

       publicstaticreadonlyDependencyProperty ContentProperty = DependencyProperty.RegisterAttached("Content",typeof(string),typeof(WebBrowserContentBinding),newPropertyMetadata(OnContentChanged));

       ///<summary>

       /// Get value of attached property Content

       ///</summary>

       ///<param name="obj">WebBrowser object</param>

       ///<returns>the value of Content property </returns>

       publicstaticstring GetContent(WebBrowser obj)

        {

           return (string)obj.GetValue(ContentProperty);

        }

       ///<summary>

       /// Set the value of attached property Content

       ///</summary>

       ///<param name="obj">WebBrowser object</param>

       ///<param name="content">the value that set to property Content</param>

       publicstaticvoid SetContent(WebBrowser obj, string content)

        {

            obj.SetValue(ContentProperty, content);

        }

       privatestaticvoid OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

           var webBrowser = d asWebBrowser;

           if (webBrowser == null)

            {

               thrownewInvalidOperationException(string.Format("WebBrowserContentBinding attached property only can be used in WebBrowser, you are using in {0}", d.GetType().Name));

            }

            webBrowser.LoadCompleted += WebBrowser_LoadCompleted;

            webBrowser.NavigateToString(e.NewValue.ToString());

        }

       privatestaticvoid WebBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)

        {

           var wb = sender asWebBrowser;

            wb.LoadCompleted -= WebBrowser_LoadCompleted;

            wb.Visibility =Visibility.Visible;

        }

    }

}

0 0
原创粉丝点击