WPF custom controls and styles (11)-Wait/free/loading state-control implementation

Source: Internet
Author: User

A Objective

Disclaimer: WPF custom controls and styles are a series of articles, before and after some related, but mostly in accordance with the order from simple to complex gradually released, etc., if you do not understand where you can refer to the previous articles in this series, at the end of the article is accompanied by some links.

This article mainly has three kinds of realization way:

    • Simple busy state control busybox;
    • Win8/win10 effect busy state control progressring;
    • Popup Asynchronous Wait box Waitingbox;

Two Simple busy state control BusyBox

Control whether the control is enabled through the property "IsActive", in the background C # code:

//<summary>//Busybox.xaml interactive logic///</summary> public partial class Busybox:usercontrol {public static readonly DependencyProperty Isactiveproperty = Dependencyproperty.register ("IsActive", typeof (Bo        OL), typeof (BusyBox), new PropertyMetadata (false)); <summary>///whether to enable//</summary> public bool IsActive {get {Retu RN (BOOL) GetValue (Isactiveproperty);        } set {SetValue (Isactiveproperty, value);} } static BusyBox () {Defaultstylekeyproperty.overridemetadata (typeof (BusyBox), new Frameworkprope        Rtymetadata (typeof (BusyBox))); }} uses a font icon that implements the control of the animated display in the trigger, the style code: &NBSP;&NBSP; 
<style targettype= "{x:type Local:busybox}" > <setter property= "Foreground" value= "{StaticResource Textforeg Round} "></Setter> <setter property=" Width "value=" "></Setter> <setter property=" He                Ight "value=" ></Setter> <setter property= "Template" > <Setter.Value> <controltemplate targettype= "{x:type Local:busybox}" > <grid verticalalignment= "Center" Hori zontalalignment= "center" > <viewbox stretch= "Uniform" verticalalignment= "center" Horizontala Lignment= "Center" > <textblock text= "& #xe65f;" X:name= "FIcon" fontsize= "+ style=" {St                                Aticresource FIcon} "rendertransformorigin=" 0.5,0.5 "foreground=" {TemplateBinding Foreground} "> <TextBlock.RenderTransform> <rotatetransform x: Name= "Transficon" angle="0"/> </TextBlock.RenderTransform> </TextBlock> </Viewbox> </Grid> <controltemplate.triggers&gt                        ; <!--activation Status--<trigger property= "IsActive" value= "true" > <s Etter property= "Visibility" value= "Visible" targetname= "FIcon"/> <trigger.enteractions&gt                                ; <beginstoryboard > <storyboard > &lt ;D oubleanimation repeatbehavior= "Forever" storyboard.targetname= "Transficon" Storyboa Rd.                                Targetproperty= "Angle" to= "duration=" 0:0:2.5 "/> </Storyboard> </BeginStoryboard> </trigger.Enteractions> <Trigger.ExitActions> <beginstoryboard > <storyboard > <doubleanimation Re Peatbehavior= "Forever" storyboard.targetname= "Transficon" storyboard.targetproperty= " Angle "to=" 0 "duration=" 0 "/> </Storyboard> <                        /beginstoryboard> </Trigger.ExitActions> </Trigger>                            <!--inactive state--<trigger property= "IsActive" value= "false" > <setter property= "Visibility" value= "collapsed" targetname= "FIcon"/> </trig ger> </ControlTemplate.Triggers> </ControlTemplate> </sette R.value> </Setter> </Style> 
Examples of Use:
<checkbox verticalalignment= "Center" x:name= "CbActive2" ischecked= "True" margin= "5" >isactive</checkbox >            <core:busybox width= "height=" foreground= "white" background= "Red"  margin= "5"  isactive= " {Binding IsChecked, elementname=cbactive2} "/>            <core:busybox width=" "height=" "foreground=" Background= "Red"  margin= "5"  isactive= "{Binding IsChecked, elementname=cbactive2}"/>

Four Pop-up async wait Box Waitingbox

The use of a modal form, asynchronous execution of incoming operations, the implementation is relatively simple, did not do exception handling. Another drawback is that the cancellation operation is not supported. Background C # code:

<summary>///Simple wait box///</summary> public partial class Waitingbox:window {public St        Ring Text {get {return this.txtMessage.Text;} set {this.txtMessage.Text = value;}}        Private Action _callback;            Public Waitingbox (Action callback) {InitializeComponent ();            This._callback = Callback; This.        Loaded + = waitingbox_loaded; } void Waitingbox_loaded (object sender, RoutedEventArgs e) {This._callback.begininvoke (this.        Oncomplate, NULL); } private void Oncomplate (IAsyncResult ar) {this. Dispatcher.invoke (new Action () = {this.            Close ();        }));        }///<summary>///Display the Wait box, owner specifies the host view element, callback is the method body that needs to be executed (exception handling is required). The current box is the modal form///</summary> public static void Show (FrameworkElement owner, Action callback, string         Mes = "There is a kind of happiness, called Waiting ...") {   Waitingbox win = new Waitingbox (callback);            Window Pwin = Window.getwindow (owner); Win.            Owner = Pwin; Win.            Text = mes; var loc = owner.            Pointtoscreen (new Point ()); Win. left = loc. X + (owner. Actualwidth-win.            Width)/2; Win. Top = loc. Y + (owner. Actualheight-win.            Height)/2; Win.        ShowDialog (); }    }

Style code:
<window x:class= "System.Windows.WaitingBox" x:name= "WB" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/ Presentation "xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "allowstransparency=" True "windowstyle= "None" windowstartuplocation= "Manual" showintaskbar= "False" background= "Transparent" title= "Waitingbox" Heig        ht= "width=" "260" > <Grid> <!--background= "{Binding PATH=BACKGROUND,ELEMENTNAME=WB}"-- <border background= "{StaticResource waitingboxbackground}" opacity= "0.89" cornerradius= "1" effect= "{ StaticResource Windowdropshadow} "></Border> <stackpanel verticalalignment=" Center "orientation=" Horizo Ntal "horizontalalignment=" Center "margin=" 5 "> <textblock text=" & #xe65f; "X:name=" FIcon "fontsize=" 50 "style=" {StaticResource FIcon} "rendertransformorigin=" 0.5,0.5 "margin=" 3 "> <textblock.rendertransf Orm> <rotatetransform X:name= "Transficon" angle= "0"/> </TextBlock.RenderTransform> </TextBlock> <textblock x:name= "Txtmessage" margin= 2,10,15,10 "width=" verticalalignment= "Center" textwrapping= "Wrap" >Loading...</TextBlock> </StackPanel> </Grid> <Window.Triggers> <eventtr                    Igger routedevent= "window.loaded" > <beginstoryboard > <storyboard >                                      <doubleanimation repeatbehavior= "Forever" storyboard.targetname= "Transficon" Storyboard.targetproperty= "Angle" to= "duration=" 0:0:2.5 "/> </Storyboard> </beg" Instoryboard> </EventTrigger> </Window.Triggers></Window>
Simple to use, example:
  Waitingbox.show (this, () =            {                System.Threading.Thread.Sleep);            }, "The loading of the desperate, please later ...");            var res = messageboxx.question ("Already done?") ");

Referenced in

Http://www.cnblogs.com/anding/p/5006279.html

WPF custom controls and styles (11)-Wait/free/loading state-control implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.