`

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

    博客分类:
  • wp
wp 
阅读更多
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的 否则不能在配置文件中调用

分享到:
评论

相关推荐

    WP8.1自定义Progressbar

    在本教程中,我们将探讨如何在WP8.1中实现一个仿Android风格的自定义Progressbar。这个Progressbar不仅在外观上接近Android,还可能包含一些Android特有的交互特性。 首先,让我们理解什么是Progressbar。...

    wp7下等待进度条

    在Windows Phone 7 (WP7)开发中,创建一个等待进度条可以提供用户友好的界面...通过合理地使用ProgressIndicator控件,结合异步编程和自定义样式,开发者能够为用户提供一个明确的反馈机制,增强应用的交互性和专业性。

    windows phone (wp7)页面加载进度提示

    1. 创建进度条控件:在XAML布局文件中,我们可以添加一个ProgressBar控件,用于显示加载进度。可以通过设置其Visibility属性来控制是否显示,通过Value属性设置当前进度。 2. 启动后台任务:在后台线程执行耗时操作...

    WP7带文本信息进度条

    1. **ProgressBar控件**:这是Windows Phone 7中用于表示进度的内置控件。通过调整其`Value`属性,可以改变进度条的填充程度。同时,可以通过设置`IsIndeterminate`属性来决定进度条是否显示不确定的动画(如圆形...

    android 中win10 使用uwp控件实现进度条Marquez效果

    如何做上图的效果,实际需要的是两个控件,一个是显示文字 的 TextBlock 一个是进度条。 那么如何让 文字和左边的距离变化?使用 TranslateTransform 看起来 Marquez 的界面就是:  &lt;ProgressBar x:Name=Mcdon ...

    Windows-Phone-7-UI

    - **ProgressBar**:用于显示任务进度。 - **PageTitle**:用于设置页面标题。 - **PanoramaApplication**:用于创建全景应用,展示多个视图。 - **PivotControl**:用于创建可以旋转的界面。 #### Windows Phone ...

Global site tag (gtag.js) - Google Analytics