UWP开发入门(十一)——Attached Property的简单应用
UWP中的Attached Property即附加屬性,在實際開發(fā)中是很常見的,比如Grid.Row:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><Button Grid.Row="1"></Button></Grid>? Grid.Row這個屬性并不是Button對象本身的實例方法,而是定義在Grid類型上的static property,實際使用時卻又附在其他控件的XAML里。
我們今天不討論如何使用UWP中已經(jīng)定義好的Attached Property(使用起來很簡單,沒啥好講的),也不會介紹附件屬性的定義,以及與.NET屬性的區(qū)別(MSDN文檔非常詳細),更不會花大力氣去分析附加屬性背后的原理(這個有大牛珠玉在前,不敢造次)。
本篇僅以極簡單的例子來展示Attached Propery的應用場景,拋磚引玉以期共同學習提高。
設想場景如下,需要根據(jù)文本內(nèi)容,將電話號碼和郵箱地址以下劃線藍色字體和普通文本區(qū)分開來:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"><TextBlock local:RichTextBlockEx.RichText="種子我發(fā)你這個郵箱:SampleEmail@outlook.com,有事打我電話18610241024。"></TextBlock></Grid>
顯示文本UWP中一般是通過TextBlock來實現(xiàn),一段文字中如果需要不同顏色,以及增加下劃線就需要在TextBlock的Inlines屬性中添加不同的Inline的子類對象(比如Run,Underline等)。
而修改TextBlock的Inlines集合需要直接操作TextBlock對象,UWP的程序又通常是MVVM結構,在ViewModel中不應添加對View層級的引用,這就要求不能在ViewModel中訪問TextBlock。
那么修改TextBlock的邏輯就只能放在View中,問題是最終返回的TextBlock需要根據(jù)運行時具體綁定的文本內(nèi)容才能確定,要求同時能訪問TextBlock以及綁定的DataContext。似乎也不適合寫在Page.xaml.cs中。
Attached Propery則非常完美的解決了我們的問題,首先附加屬性支持binding,這就將View中的TextBlock和ViewModel中的文本內(nèi)容結合在一起;其次附加屬性是View層級的概念,通過它來修改TextBlock不會破壞MVVM的結構。
接下來我們動手定義一個Attached Property:
public static string GetRichText(DependencyObject obj){return (string)obj.GetValue(RichTextProperty);}public static void SetRichText(DependencyObject obj, string value){obj.SetValue(RichTextProperty, value);}// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...public static readonly DependencyProperty RichTextProperty =DependencyProperty.RegisterAttached("RichText", typeof(string), typeof(RichTextBlockEx), new PropertyMetadata(null, RichTextChanged));這個附加屬性叫做RichTextProperty,我們給他做了GetRichText和SetRichText兩個方法的封裝,以便我們能像經(jīng)典的.NET屬性一樣,通過“RichText”來訪問。具體的細節(jié)請參考MSDN的文檔,這里我們著重看一下RegisterAttached方法中傳遞了一個回調(diào)方法RichTextChanged。
private static void RichTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){TextBlock textBlock = d as TextBlock;if (textBlock == null)return;string inputString = e.NewValue as string;if (string.IsNullOrEmpty(inputString) || string.IsNullOrWhiteSpace(inputString)){textBlock.Text = string.Empty;return;}textBlock.Inlines.Clear();CreateTextBlock(textBlock, inputString);}該方法是一個靜態(tài)方法,在biding的對象發(fā)生改變時觸發(fā),并將biding的對象本身作為第一個參數(shù)傳遞進來。該方法的代碼先判斷了綁定的新值是否為空,不為空則調(diào)用CreateTextBlock方法,根據(jù)inputString來創(chuàng)建帶有藍色下劃線的TextBlock。
private static void CreateTextBlock(TextBlock textBlock, string inputString){var resultList = new List<MatchResult>();resultList.AddRange(GetMatchResultList(inputString));int offset = 0;var matchDicOrder = resultList.OrderBy(_ => _.Match.Index);foreach (var item in matchDicOrder){var match = item.Match;if (match.Index >= offset){if (match.Index > 0 && match.Index != offset){var text = inputString.Substring(offset, match.Index - offset);AddText(textBlock, text);}var content = inputString.Substring(match.Index, match.Length);AddUnderline(textBlock, content);offset = match.Index + match.Length;}}if (offset < inputString.Length){var text = inputString.Substring(offset);AddText(textBlock, text);}}通過正則表達式匹配phone和email,將匹配的內(nèi)容通過AddUnderline方法添加到原TextBlock中,不匹配的內(nèi)容則直接AddText。
private static void AddText(TextBlock textBlock, string text){Run run = new Run();run.Text = text;textBlock.Inlines.Add(run);}private static void AddUnderline(TextBlock textBlock, string content){Run run = new Run();run.Text = content;run.Foreground = new SolidColorBrush(Colors.Blue);Underline underline = new Underline();underline.Inlines.Add(run);textBlock.Inlines.Add(underline);}實際的開發(fā)中,這里可能存在更復雜的邏輯,比如即時通訊程序中存在@的概念,下劃線的內(nèi)容需要在點擊后做相應處理等(打電話,發(fā)郵件)。本篇中的代碼為了簡單清晰做了最簡化的處理,各位在自己的APP里可以進一步發(fā)揮想象補充,只要別讓手機或電腦爆炸就行……
本篇有心回歸開發(fā)入門的初心,介紹的內(nèi)容較為簡單,寫得不好還請包涵。
照例放出GayHub地址:
https://github.com/manupstairs/UWPSamples
?
轉載于:https://www.cnblogs.com/manupstairs/p/5380139.html
總結
以上是生活随笔為你收集整理的UWP开发入门(十一)——Attached Property的简单应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 北京计算机应用基础考试时间,07年北京市
- 下一篇: 根据拼音首字母查询人名【转】