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);
}
}
}
效果图如下