Once in WP7, WP8 under the message use is Coding4Fun.Phone.Toolkit inside the Toastprompt class to achieve.
Now let's do this for ourselves like it home: it pops up from the right and disappears after a few seconds.
First identify several requirements:
1. Can eject this message on any interface
2. You can customize the message format and the time the message disappears
(including whether it contains headings, font sizes, permutations ...) )
3. Message hints and disappears are animated
First, get a panel on the current page to render the message above:
ContentPresenter and panel are all inherited from FrameworkElement.
ContentPresenter: Only one element can be accommodated
A control that inherits from a panel can hold multiple child controls, so the message is displayed on this
Panel Popupparentpanel;//message pops up on this panelFrame Rootvisualframe;//the visual root of the current page PublicPanel Popupparentpanel {Get { if(Popupparentpanel = =NULL) {IEnumerable<ContentPresenter> Source = commonhelper.getvisualdescendants ( This. Rootvisualframe). Oftype<contentpresenter> ();//Get all child objects of the ContentPresenter type for(inti =0; I < source. Count<contentpresenter> (); i++) {IEnumerable<Panel> enumerable2 = commonhelper.getvisualdescendants (source. Elementat<contentpresenter> (i)). Oftype<panel> ();//get child objects of all panel types if(Enumerable2. Count<panel> () >0) { This. Popupparentpanel = Enumerable2. First<panel> ();//Select the first panel Break; } } } return This. Popupparentpanel; } }
Second, show Show Method:
BOOLbLocked;//default is False Public voidShow () {if(bLocked) {return; } Initcontrol (); //The code for the message interface is slightly detailed, please see the demo shown below
if(Popupparentpanel! =NULL) { //add message to panel if(Popupparentpanel isStackPanel) {PopUpParentPanel.Children.Insert (0, Toastgrid); } Else{PopUpParentPanel.Children.Add (Toastgrid); } showstoryboard (); //Turn on animationbLocked =true; } }
Get child element related static class:
Public classCommonhelper { Public StaticIenumerable<dependencyobject>Getvisualchildren (DependencyObject element) {if(element = =NULL) { Throw NewArgumentNullException ("element"); } returnGetvisualchildrenandselfiterator (Element). Skip<dependencyobject> (1); } Public StaticIenumerable<dependencyobject>getvisualdescendants (DependencyObject element) {if(element = =NULL) { Throw NewArgumentNullException ("element"); } returnGetvisualdescendantsandselfiterator (Element). Skip<dependencyobject> (1);//Remove the element itself } Private StaticIenumerable<dependencyobject>Getvisualdescendantsandselfiterator (DependencyObject Element) {Queue<DependencyObject> ITERATORVARIABLE0 =NewQueue<dependencyobject>(); Iteratorvariable0.enqueue (Element); while(true) { if(Iteratorvariable0.count <=0) { yield Break; } DependencyObject iteratorVariable1=Iteratorvariable0.dequeue (); yield returnIteratorVariable1; foreach(DependencyObject obj2inchGetvisualchildren (IteratorVariable1)) {Iteratorvariable0.enqueue (OBJ2); } } } Private StaticIenumerable<dependencyobject>Getvisualchildrenandselfiterator (DependencyObject element) {yield returnelement; intChildrencount =Visualtreehelper.getchildrencount (Element); intChildindex =0; while(true) { if(Childindex >=Childrencount) { yield Break; } yield returnVisualtreehelper.getchild (element, childindex); Childindex++; } } }
View Code
Third, display the message animation effect and add message display completion event:
1 Public Eventeventhandler<Object> toastshowcomplated;//Message Display Completion event2 3 Public voidShowstoryboard ()4 {5Storyboard sbshow =NewStoryboard ();6sbshow.completed + = sbshow_completed;//start timing When the animation is complete, and the message disappears after the time7 8 //x-axis side, starting at half the message interface9DoubleAnimation da =NewDoubleAnimation () {from = Toastgrid.width/2, to =0, Duration =Dua}; TenDa. Easingfunction =NewCircleease () {Easingmode =easingmode.easeout}; One Storyboard.settarget (DA, toasttransform); AStoryboard.settargetproperty (DA,"translatetransform.x");//binding target properties and some previous differences - sbShow.Children.Add (DA); - the //set transparency from 0 to 1 -DoubleAnimation da1 =NewDoubleAnimation () {from =0, to =1, Duration =Dua}; - Storyboard.settarget (da1, Toastgrid); -Storyboard.settargetproperty (DA1,"frameworkelement.opacity"); + SbShow.Children.Add (da1); - +Sbshow.begin ();//Start Animation A } at voidSbshow_completed (ObjectSenderObjecte) - { -Isshow =true; - //Timer start -Timer =NewTimer (NewTimerCallback (time_completed),NULL, This. Totalhiddenseconds * +,0); - if( This. Toastshowcomplated! =NULL) in { -Toastshowcomplated ( This, e); to } + } - the Async voidTime_completed (Objecte) * { $ timer. Dispose ();Panax Notoginseng awaitWindows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync ( Coredispatcherpriority.normal, () = - { the Hidestoryboard (); + }); A}
Hide message animations and add message-hiding completion events.
Iv. using the custom message:
Private void Btntoast_click (object sender, RoutedEventArgs e) { new" This is the content of the show! " }; Toast. Show (); }
On the right is the Demo:CustomToastSample.rar of this essay.
"WP8.1" Implementation of a custom message similar to "home of it"