wp7 webbrowser 添加依赖属性content

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>

    public class WebBrowserContentBinding

    {

        /// <summary>

        /// attached property Content

        /// </summary>

        public static readonly DependencyProperty ContentProperty = DependencyProperty.RegisterAttached("Content", typeof(string), typeof(WebBrowserContentBinding), new PropertyMetadata(OnContentChanged));

        /// <summary>

        /// Get value of attached property Content

        /// </summary>

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

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

        public static string 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>

        public static void SetContent(WebBrowser obj, string content)

        {

            obj.SetValue(ContentProperty, content);

        }

        private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

        {

            var webBrowser = d as WebBrowser;

            if (webBrowser == null)

            {

                throw new InvalidOperationException(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());

        }

        private static void WebBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)

        {

            var wb = sender as WebBrowser;

            wb.LoadCompleted -= WebBrowser_LoadCompleted;

            wb.Visibility = Visibility.Visible;

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值