wp8自定义控件 进度条

1、自定义控件 继承至ContentControl

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Microsoft.Phone.Shell;
using System.Windows.Controls.Primitives;
using Microsoft.Phone.Controls;
using System.Linq;
using System.Diagnostics;

namespace MyProgress
{
    public class ProgressIndicator : ContentControl
    {       
        //声明自定义控件中包含的控件
        private System.Windows.Shapes.Rectangle backgroundRect;
        private System.Windows.Controls.StackPanel stackPanel;
        private System.Windows.Controls.ProgressBar progressBar;
        private System.Windows.Controls.TextBlock textBlockStatus;


        private double progressBarValue = 0;//进度条的值
        private static string defaultText = "正在加载";
        private bool showLabel;
        private string labelText;

        public ProgressIndicator()
        {
            //引用控件默认的样式
            this.DefaultStyleKey = typeof(ProgressIndicator);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.backgroundRect = this.GetTemplateChild("backgroundRect") as Rectangle;
            stackPanel = this.GetTemplateChild("stackPanel") as StackPanel;
            progressBar = this.GetTemplateChild("progressBar") as ProgressBar;
            textBlockStatus = this.GetTemplateChild("textBlockStatus") as TextBlock;

            backgroundRect.Visibility = System.Windows.Visibility.Visible;
            progressBar.IsIndeterminate = true;
        }

        public bool ShowLabel
        {
            get
            {
                return this.showLabel;
            }
            set
            {
                this.showLabel = value;
            }
        }

        public double ProgressBarValue
        {
            get
            {
                return this.progressBarValue;
            }
            set
            {
                this.progressBarValue = value;
            }
        }

        public string Text
        {
            get
            {
                return labelText;
            }
            set
            {
                this.labelText = value;
                if (this.textBlockStatus != null)
                {
                    this.textBlockStatus.Text = value;
                }
            }
        }



        internal Popup ChildWindowPopup
        {
            get;
            private set;
        }

        private static PhoneApplicationFrame RootVisual
        {
            get
            {
                return Application.Current == null ? null : Application.Current.RootVisual as PhoneApplicationFrame;
            }
        }

        internal PhoneApplicationPage Page
        {
            get { return  RootVisual.GetVisualDescendants().OfType<PhoneApplicationPage>().FirstOrDefault(); }
        }


       

        public bool IsOpen 
        {
            get 
            {
                return ChildWindowPopup != null && ChildWindowPopup.IsOpen;
            } 
        }

        public void Show()
        {
            if (this.ChildWindowPopup == null)
            {
                this.ChildWindowPopup = new Popup();
                ChildWindowPopup.Child = this;
            }
            ChildWindowPopup.IsOpen = true;

            if (Page != null && Page.ApplicationBar != null)
            {
                Page.ApplicationBar.IsVisible = false;
            }
        }

        public void Hide()
        {
            if (Page != null && Page.ApplicationBar != null)
            {
                Page.ApplicationBar.IsVisible = true ;
            }
            ChildWindowPopup.IsOpen = false;
        }

    }
}

在Themesa文件夹新建generic.xaml 定义对应控件的样式

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyProgress;assembly=MyProgress"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
    mc:Ignorable="d"
    >
    <Style TargetType="local:ProgressIndicator">
        <Setter Property="Background" Value="{StaticResource PhoneChromeBrush}"/>
        <Setter Property="Width" Value="480" />
        <Setter Property="Height" Value="800" />
        <Setter Property="Margin" Value="0" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:ProgressIndicator">
                    <Grid x:Name="LayoutRoot" Background="Transparent" >
                        <Rectangle x:Name="backgroundRect" Grid.Row="0" Fill="{StaticResource PhoneBackgroundBrush}"
                                   Opacity="0.5"/>
                        <StackPanel x:Name="stackPanel" Orientation="Vertical" VerticalAlignment="Center">
                            <ProgressBar Opacity="1" Height="4" Margin="-12,10,0,0" 
                                    Maximum="100"
                                    HorizontalAlignment="Center" 
                                    VerticalAlignment="Center"  
                                    Name="progressBar"
                                    Foreground="{StaticResource PhoneForegroundBrush}"
                                    Width="510" />
                            <TextBlock Opacity="1" Height="40" 
                                       HorizontalAlignment="Center" 
                                       VerticalAlignment="Center" 
                                 Name="textBlockStatus" FontSize="30" FontWeight="Bold" 
                                       Style="{StaticResource PhoneTextNormalStyle}"  />
                        </StackPanel>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

最后可以在页面用调用这个控件,会弹出进度条,并且使页面也菜单栏都无法操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MyProgressDemo.Resources;

namespace MyProgressDemo
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();

            // 用于本地化 ApplicationBar 的示例代码
            BuildLocalizedApplicationBar();
            this.BackKeyPress += MainPage_BackKeyPress;
        }
        MyProgress.ProgressIndicator progressIndicator;

        void MainPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (progressIndicator.IsOpen == true)
            {
                e.Cancel = true;
                progressIndicator.IsOpen = false;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            progressIndicator = new MyProgress.ProgressIndicator();
            progressIndicator.Show();
            progressIndicator.Text="l";
        }

        // 用于生成本地化 ApplicationBar 的示例代码
        private void BuildLocalizedApplicationBar()
        {
            // 将页面的 ApplicationBar 设置为 ApplicationBar 的新实例。
            ApplicationBar = new ApplicationBar();

            // 创建新按钮并将文本值设置为 AppResources 中的本地化字符串。
            ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
            appBarButton.Text = AppResources.AppBarButtonText;
            ApplicationBar.Buttons.Add(appBarButton);

            // 使用 AppResources 中的本地化字符串创建新菜单项。
            ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
            ApplicationBar.MenuItems.Add(appBarMenuItem);
        }
    }
}
效果图如下



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值