NavigationWindow win = new NavigationWindow();//未設(shè)置大小//win.Content = new Page1();//宿主大小大于Page尺寸//win.Content = new Page1(300,300,500,500);//宿主大小小于Page尺寸win.Content = new Page1(500, 500, 300, 300);win.Show();
<Page x:Class="WpfApplication4.Page4"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"Title="Page4"><Grid><FlowDocumentReader><FlowDocument><Paragraph x:Name="para" FontSize="24" Background="AliceBlue"><Figure Width="100" Height="100" HorizontalAnchor="ColumnRight" HorizontalOffset="-10" VerticalAnchor="ParagraphTop" VerticalOffset="-30"><BlockUIContainer><Image Source="bee.png"/></BlockUIContainer></Figure>路由事件(Routed Event)</Paragraph><Section FontFamily="華文仿宋"><Paragraph>黃蓉凝目看去,只見(jiàn)那兩只玉蜂雙翅上也都有字,那六個(gè)字也是一模一樣,右翅是“情谷底”,左翅是“我在絕”。黃蓉大奇,暗想:“造物雖奇,也決造不出這樣一批蜜蜂來(lái)之理。其中必有緣故?!?……</Paragraph><Paragraph>黃蓉不答,只是輕輕念著:“情谷底,我在絕。情谷底,我在絕。”她念了幾遍,隨即省悟:“啊!那是‘我在絕情谷底’。是誰(shuí)在絕情谷底啊?難道是襄兒?”心中怦怦亂跳……</Paragraph><Paragraph TextAlignment="Right">——《神雕俠侶:第三十八回 生死茫?!?lt;/Paragraph></Section><Section LineHeight="25" FontSize="15"><Paragraph >這一段講的是小龍女深陷絕情谷地,用花樹(shù)上的細(xì)刺,在玉蜂翅上刺下‘我在絕情谷底’六字,盼望玉蜂飛上之后,能為人發(fā)現(xiàn)。結(jié)果蜂翅上的細(xì)字被周伯通發(fā)現(xiàn),而給黃蓉隱約猜到了其中含義。本節(jié)內(nèi)容包括:</Paragraph><List ><ListItem><Paragraph><Hyperlink NavigateUri="Page4.xaml#first"><!--<Hyperlink Click="Hyperlink_Click">-->從玉蜂說(shuō)起,回顧.Net事件模型</Hyperlink></Paragraph></ListItem><ListItem><Paragraph><Hyperlink NavigateUri="Page4.xaml#second">什么是路由事件?</Hyperlink></Paragraph></ListItem><ListItem><Paragraph>CLR事件足夠完美,為什么還需要路由事件?</Paragraph></ListItem><ListItem><Paragraph>言歸正傳,話路由事件</Paragraph></ListItem><ListItem><Paragraph>路由事件的實(shí)例</Paragraph></ListItem></List></Section><Paragraph x:Name="first" FontSize="20" Background="AliceBlue">1. 從玉蜂說(shuō)起,回顧.Net事件模型</Paragraph><Paragraph>木木熟悉神雕俠侶的故事,于是他根據(jù)“玉蜂傳信”這樣一個(gè)故事,信手畫(huà)下這樣一幅有趣的圖。</Paragraph><BlockUIContainer><Image Source="routedevent.jpg"/></BlockUIContainer><Paragraph>其實(shí)這一幅“玉蜂傳信圖”暗合.Net的事件模型。小龍女是事件的發(fā)布者,她發(fā)布了事件“我在絕情谷底”;老頑童和黃蓉是事件的訂閱者,不過(guò)老頑童并沒(méi)有處理該事件,而黃蓉處理了事件,隱約能猜出其中含義;至于可憐的小楊過(guò),則根本沒(méi)有訂閱事件,只是苦苦念叨“龍兒,龍兒,你在哪兒……”;而玉蜂正是傳遞信息的事件。事件,事件的發(fā)布者和事件的訂閱者構(gòu)成了.Net事件模型的三個(gè)角色。在.Net當(dāng)中,一個(gè)事件是用關(guān)鍵字event來(lái)表示的。如下代碼所示:</Paragraph><Paragraph xml:space="preserve" Background="#88888888">public delegate void WhiteBee(string param); //聲明了玉蜂的委托// 小龍女類(lèi)class XiaoLongnv{public event WhiteBee WhiteBeeEvent; //玉蜂事件public void OnFlyBee(){Console.WriteLine("小龍女在谷底日復(fù)一日地放著玉蜂,希望楊過(guò)有一天能看到.....");WhiteBeeEvent(msg);}private string msg = "我在絕情谷底";
}// 老頑童類(lèi)class LaoWantong{public void ProcessBeeLetter(string msg){Console.WriteLine("老頑童:小蜜蜂、小蜜蜂,別跑");}}// 黃蓉類(lèi)class Huangrong{public void ProcessBeeLetter(string msg){Console.WriteLine("黃蓉:\"{0}\",莫非......",msg);}
}// 楊過(guò)類(lèi)class YangGuo{public void ProcessBeeLetter(string msg){Console.WriteLine("楊過(guò):\"{0}\",我一定會(huì)找她!", msg);}public void Sign(){Console.WriteLine("楊過(guò)嘆息:龍兒,你在哪兒....");}
}static void Main(string[] args){// 第一步 人物介紹XiaoLongnv longnv = new XiaoLongnv(); //小龍女LaoWantong wantong = new LaoWantong(); //老頑童Huangrong rong = new Huangrong(); //黃蓉YangGuo guo = new YangGuo(); //楊過(guò)// 第二步 訂閱事件,唯獨(dú)沒(méi)有訂閱楊過(guò)的ProcessBeeLetter;longnv.WhiteBeeEvent += wantong.ProcessBeeLetter;longnv.WhiteBeeEvent += rong.ProcessBeeLetter;// longnv.WhiteBeeEvent += guo.ProcessBeeLetter; //楊過(guò)是沒(méi)有訂閱小龍女的玉蜂事件// 第三步 小龍女玉蜂傳信longnv.OnFlyBee();// 第四步 楊過(guò)嘆息guo.Sign();}
</Paragraph><Paragraph x:Name="second" FontSize="20" Background="AliceBlue">2. 什么是路由事件?</Paragraph><Paragraph>什么是路由事件呢?木木很快查看了一下MSDN,MSDN從功能和實(shí)現(xiàn)兩種視角給出了路由事件的定義。</Paragraph><Paragraph>Functional definition: A routed event is a type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event.</Paragraph><Paragraph>Implementation definition: A routed event is a CLR event that is backed by an instance of the RoutedEvent class and is processed by the Windows Presentation Foundation (WPF) event system.</Paragraph><Paragraph>雖然木木現(xiàn)在英語(yǔ)功底已經(jīng)進(jìn)步了很多,但是這兩個(gè)定義還是讓他看得一頭霧水??磥?lái)必須得找個(gè)例子有點(diǎn)感性的認(rèn)識(shí)(以大家都非常熟悉的Button的Click事件為例,該事件是個(gè)路由事件,可以通過(guò)Reflector查看ButtonBase的源碼)。</Paragraph></FlowDocument></FlowDocumentReader></Grid>
</Page>