欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-30 10:02:29
...
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

<UserControl x:Class="MyControl.M.MProgress"
    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">

    <Grid x:Name="LayoutRoot" HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        Background="#ee000000"
        Visibility="Collapsed">
       
        <StackPanel Orientation="Vertical"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Center">

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

        </StackPanel>
    </Grid>
   
</UserControl>
========================================================================================

使用方式:

<phone:PhoneApplicationPage
    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"
     
    >


    <Grid x:Name="LayoutRoot" Background="White">
        
         <Grid.RowDefinitions>
            <RowDefinition Height="200"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
         </Grid.RowDefinitions>
            <sam:MProgress HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="bar" Grid.RowSpan="2"></sam:MProgress>
           
            <Button Grid.Row="0" Content="Button1" Height="72" x:Name="myButton11" Width="160" Click="click" Margin="10"/>
           
    </Grid>

</phone:PhoneApplicationPage>



效果:









注意:

使用时 指定命名空间xmlns:sam="clr-namespace:MyControl.M"

使用时注意调用开头<sam: />

类一定要是public的 否则不能在配置文件中调用

相关标签: wp