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

本文介绍了一个名为 MProgress 的自定义进度控件的实现方法,该控件支持文本显示及前景背景颜色设置等功能,并提供了使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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的 否则不能在配置文件中调用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值