從我的網易部落格移過來的。。
先提供吧。
這個是coding4Fun提供的MessagePrompt彈窗。還提供了ToastPrompt:土司彈出框,類似於土司推播通知。
AboutPrompt:說明彈出框,特點是彈出框會在螢幕中間,其他的彈出框是在螢幕的上方(其他可以通過Margin調整文位置)。
PasswordInputPrompt:密碼輸入彈出框。
InputPrompt:輸入彈出框,用於彈出輸入框輸入其他的資訊。
這些都不討論了。需要的可以到官網文檔:http://coding4fun.codeplex.com/wikipage?title=MessagePrompt&referringTitle=Documentation
或者到:http://coding4fun.codeplex.com/wikipage?title=MessagePrompt&referringTitle=Documentation瞭解。
今天討論用MessagePrompt自訂自己的彈窗。彈窗可以帶輸入框等內容。
先建立一個window phone User Control。命名為BodyUserControl。這個主要是設計彈窗的內容。
添加代碼:
<Border BorderBrush="YellowGreen" BorderThickness="2"> <StackPanel x:Name="LayoutRoot1" Background="{StaticResource PhoneChromeBrush}" Margin="0,0,0,10"> <TextBlock Text="Body declared in UserControl XAML" HorizontalAlignment="Center"/> <Image Source="/RoundBtnDemo;component/images/MB_0010_radio.png" Height="80" Width="80" HorizontalAlignment="Center"/> <TextBox x:Name="msg" /> <Border Background="YellowGreen" Height="100" Width="400"> <TextBlock x:Name="tx" Text="This is the first text in the Body section. Another Body content here" TextWrapping="Wrap" VerticalAlignment="Center"/> </Border> </StackPanel> </Border>
為:
在主介面裡面的需要彈窗的onclick事件或者其他裡面添加代碼:
MessagePrompt msgPrompt = new MessagePrompt(); msgPrompt.Title = "UserControl test"; msgPrompt.Body = new BodyUserControl(); msgPrompt.IsAppBarVisible = false; msgPrompt.Completed += new EventHandler<PopUpEventArgs<string, PopUpResult>>(msgPrompt_Completed); msgPrompt.Show();
最重要的來了:怎麼擷取彈窗上的TextBox 的內容呢?
其實是在那個Completed事件可以擷取:
void msgPrompt_Completed(object sender, PopUpEventArgs<string, PopUpResult> e) { if (e.PopUpResult == PopUpResult.Ok)//先判斷是否是點擊那個勾,也就是確定 { var msgPrompt = sender as MessagePrompt;//把sender轉為MessagePrompt對象 var bodyControl = msgPrompt.Body as BodyUserControl;//取出BodyUserControl對象 //取出BodyUserControl中Name為msg,也就是那個TextBox。 //這裡的Showmsg是一個TextBlock,這樣,就能取到彈窗上的控制項的值 ShowMsg.Text = bodyControl.msg.Text; } }