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;

}

}

}

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...