wp wp8:自定义控件 自定义progressbar

阅读更多
MProgress.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;

namespace MyControl.M
{
    public partial class MProgress : UserControl
    {


        public Color ForeColor
        {
            get { return (Color)GetValue(MProgressForeColorProperty); }
            set { SetValue(MProgressForeColorProperty, value); }
        }
        public Color BackColor
        {
            get { return (Color)GetValue(MProgressBackColorProperty); }
            set { SetValue(MProgressBackColorProperty, value); }
        }

        public static readonly DependencyProperty MProgressForeColorProperty =
            DependencyProperty.Register("ForeColor", typeof(Color), typeof(MProgress),
            new PropertyMetadata(Colors.Black, OnColorChanged));

        public static readonly DependencyProperty MProgressBackColorProperty =
            DependencyProperty.Register("BackColor", typeof(Color), typeof(MProgress),
            new PropertyMetadata(Colors.White, OnColorChanged));

       

        private static void OnColorChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            MProgress progress = obj as MProgress;
            if (e.Property == MProgressForeColorProperty)
            {

                progress.textBlock.Foreground = new SolidColorBrush((Color)e.NewValue);
            }
            if (e.Property == MProgressBackColorProperty)
            {

                progress.LayoutRoot.Background = new SolidColorBrush((Color)e.NewValue);
            }
        }

        private DispatcherTimer timer = null;

        public MProgress()
        {
            InitializeComponent();
            timer = new DispatcherTimer();
        }

        public void setText(String text)
        {
            textBlock.Text = text;
        }

        public void startLoading()
        {
            //progressBar.Visibility = Visibility.Collapsed;
            progressBar.Visibility = System.Windows.Visibility.Visible;
            LayoutRoot.Visibility = System.Windows.Visibility.Visible;
        }

        public void stopLoading()
        {
            //progressBar.Visibility = Visibility.Collapsed;
            progressBar.Visibility = System.Windows.Visibility.Visible;
            LayoutRoot.Visibility = System.Windows.Visibility.Collapsed;
        }

        public void startLoadingWithText(String text)
        {
            textBlock.Text = text;
            progressBar.Visibility = System.Windows.Visibility.Visible;
            LayoutRoot.Visibility = System.Windows.Visibility.Visible;
           
        }

        public void stopLoadingWithText(String text)
        {
            timer.Stop();

            textBlock.Text = text;
            progressBar.Visibility = System.Windows.Visibility.Collapsed;

            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += (s, ev) =>
            {
                LayoutRoot.Visibility = System.Windows.Visibility.Collapsed;
                timer.Stop();
            };
            timer.Start();

        }

    }
}

========================================================================================

MProgress.xaml

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    x:Name="this">

            VerticalAlignment="Stretch"
        Background="#ee000000"
        Visibility="Collapsed">
       
                    HorizontalAlignment="Stretch"
            VerticalAlignment="Center">

                            x:Name="progressBar"
                HorizontalAlignment="Stretch"
                         Height="50"
                         VerticalAlignment="Center"
                         IsIndeterminate="true"
                         Visibility="Collapsed" />
                            Foreground="White"
                       Text=""
                       Margin="5,0,0,5"
                       TextWrapping="Wrap"
                       FontSize="22"
                       Height="35"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center"/>

       

   

   

========================================================================================

使用方式:

    x:Class="MyControl.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sam="clr-namespace:MyControl.M"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True"
     
    >


   
        
        
           
           
        

           
           
           

你可能感兴趣的:(wp)