在UWP或WPF开发当中,通常能需要自定义添加属性。可以使用 DependencyProperty实现。
cs code:
using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace XamlApp.UWP.CustomProperties { public class WebViewPorpeties { public static readonly DependencyProperty SourceStringProperty = DependencyProperty.RegisterAttached("SourceString", typeof(string), typeof(WebViewPorpeties), new PropertyMetadata("", OnSourceStringChanged)); public static string GetSourceString(DependencyObject obj) { return obj.GetValue(SourceStringProperty).ToString(); } public static void SetSourceString(DependencyObject obj, string value) { obj.SetValue(SourceStringProperty, value); } private static void OnSourceStringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { WebView wv = d as WebView; if (wv != null) { //可在此调用方法 wv.NavigateToString(e.NewValue.ToString()); } } } }
Xaml code:
xmlns:properties="using:XamlApp.UWP.CustomProperties" <WebView properties:WebViewProperties.SourceString="{Binding dataString}"/>
Advertisements