Silverlight数据加载时,等待图标显示与隐藏(Loading)
在我們開發SilverLight時,在加載大的數據時,需要很長時間,為了給用戶一個好的印象,我們給一個正在加載的提示,下面我就把簡單的程序貼出來,希望對大家有幫助
waiting.xaml
<UserControl x:Class="Example.Portal.Common.Waiting"
? ? 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"
? ? designWidth="640" designHeight="480">
? ? <Grid x:Name="LayoutRoot" Width="Auto" Height="Auto">
? ?? ???<Grid Background="Black" Opacity="0.2"></Grid>
? ?? ???<Canvas Width="320" Height="50">
? ?? ?? ?? ?<Rectangle RadiusX="8" RadiusY="8" Stroke="{x:Null}" Fill="#19000000" Height="49" Width="316" Canvas.Left="6" Canvas.Top="3"/>
? ?? ?? ?? ?<Rectangle RadiusX="8" RadiusY="8" Stroke="{x:Null}" Fill="#19000000" Height="48" Width="316" Canvas.Left="5" Canvas.Top="3"/>
? ?? ?? ?? ?<Border Height="50" Width="320" Background="#FFFFFFFF" BorderBrush="#FFACACAC" BorderThickness="1,1,1,1" CornerRadius="8,8,8,8">
? ?? ?? ?? ?? ? <Rectangle RadiusX="8" RadiusY="8" Stroke="{x:Null}" Margin="1,1,1,1">
? ?? ?? ?? ?? ?? ???<Rectangle.Fill>
? ?? ?? ?? ?? ?? ?? ?? ?<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
? ?? ?? ?? ?? ?? ?? ?? ?? ? <GradientStop Color="#FFFFFFFF"/>
? ?? ?? ?? ?? ?? ?? ?? ?? ? <GradientStop Color="#FFE9E9E9" Offset="1"/>
? ?? ?? ?? ?? ?? ?? ?? ?</LinearGradientBrush>
? ?? ?? ?? ?? ?? ???</Rectangle.Fill>
? ?? ?? ?? ?? ? </Rectangle>
? ?? ?? ?? ?</Border>
? ?? ???</Canvas>
? ?? ???<Grid Width="320" Height="50" VerticalAlignment="Center">
? ?? ?? ?? ?<Grid.ColumnDefinitions>
? ?? ?? ?? ?? ? <ColumnDefinition Width="5*" />
? ?? ?? ?? ?? ? <ColumnDefinition Width="10*" />
? ?? ?? ?? ?? ? <ColumnDefinition Width="80*" />
? ?? ?? ?? ?? ? <ColumnDefinition Width="5*" />
? ?? ?? ?? ?</Grid.ColumnDefinitions>
? ?? ?? ???<!--waiting.png自己可以找一個圖片-->
? ?? ?? ?? ?<Image Stretch="None" Source="../Resources/Image/waiting.png" Grid.Column="1" Opacity="0.3" />
? ?? ?? ?? ?<Image x:Name="WaitingImage" Stretch="None" Source="../Resources/Image/waiting.png" Grid.Column="1" RenderTransformOrigin="0.5,0.5">
? ?? ?? ?? ?? ? <Image.RenderTransform>
? ?? ?? ?? ?? ?? ???<RotateTransform Angle="0"></RotateTransform>
? ?? ?? ?? ?? ? </Image.RenderTransform>
? ?? ?? ?? ?</Image>
? ?? ?? ?? ?<TextBlock x:Name="titleText" FontFamily="Calibri" FontSize="13" Foreground="#FF000000" HorizontalAlignment="Left" Grid.Column="3"/>
? ?? ?? ?? ?<TextBlock Text="Waiting..." x:Name="waitingText"
? ?? ? FontFamily="Verdana" FontSize="14" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.Column="2"/>
? ?? ???</Grid>
? ? </Grid>
? ? <UserControl.Resources>
? ?? ???<Storyboard x:Name="storyboard">
? ?? ?? ?? ?<DoubleAnimation Storyboard.TargetName="WaitingImage"??Duration="0:0:0.7" To="360" Storyboard.TargetProperty="(UIElement.RenderTransform).RotateTransform.Angle" RepeatBehavior="Forever"></DoubleAnimation>
? ?? ?? ?? ?<DoubleAnimationUsingKeyFrames Storyboard.TargetName="waitingText" Storyboard.TargetProperty="Opacity" RepeatBehavior="Forever">
? ?? ?? ?? ?? ? <SplineDoubleKeyFrame KeyTime="00:00:0.0" Value="0"/>
? ?? ?? ?? ?? ? <SplineDoubleKeyFrame KeyTime="00:00:0.5" Value="1"/>
? ?? ?? ?? ?? ? <SplineDoubleKeyFrame KeyTime="00:00:1.0" Value="0"/>
? ?? ?? ?? ?</DoubleAnimationUsingKeyFrames>
? ?? ???</Storyboard>
? ? </UserControl.Resources>
</UserControl>
waiting.cs
using System.Windows.Controls;
namespace Example.Portal.Common
{
? ? public partial class Waiting : UserControl
? ? {
? ?? ???public Waiting()
? ?? ???{
? ?? ?? ?? ?InitializeComponent();
? ?? ?? ?? ?this.storyboard.Begin();
? ?? ???}
? ?? ???/// <summary>
? ?? ???/// 標題
? ?? ???/// </summary>
? ?? ???public string Title
? ?? ???{
? ?? ?? ?? ?get { return this.titleText.Text; }
? ?? ?? ?? ?set { this.titleText.Text = value; }
? ?? ???}
? ?? ???public void SetLoadingText(string text)
? ?? ???{
? ?? ?? ?? ?this.waitingText.Text = text;
? ?? ???}
? ?? ???public void SetLoadingText()
? ?? ???{
? ?? ?? ?? ?this.waitingText.Text = "正在處理,請稍候......";
? ?? ???}
? ? }
}
下面咱們再定義一個類,對外接口,以后直接調用下這個類就可以了
window.cs
using System;
using System.Windows;
using System.Windows.Controls;
namespace Example.Portal.Common
{
? ? public class Windows
? ? {
? ?? ???private static Waiting waiting;
? ?? ???private static string waitingName = "waiting";
? ?? ???static Windows()
? ?? ???{
? ?? ?? ?? ?if (waiting == null)
? ?? ?? ?? ?{
? ?? ?? ?? ?? ? //取得主要應用程序界面
? ?? ?? ?? ?? ? UserControl rootPage =Application.Current.RootVisual as UserControl;
? ?? ?? ?? ?? ? if (rootPage != null)
? ?? ?? ?? ?? ? {
? ?? ?? ?? ?? ?? ???//取得應用程序界面中waiting節點
? ?? ?? ?? ?? ?? ???waiting = rootPage.FindName(waitingName) as Waiting;
? ?? ?? ?? ?? ? }
? ?? ?? ?? ?}
? ?? ???}
? ?? ???/// <summary>
? ?? ???/// 顯示等待信息
? ?? ???/// </summary>
? ?? ???/// <param name="message"></param>
? ?? ???public static void ShowWaiting(string message)
? ?? ???{
? ?? ?? ?? ?try
? ?? ?? ?? ?{
? ?? ?? ?? ?? ? if (waiting != null)
? ?? ?? ?? ?? ? {
? ?? ?? ?? ?? ?? ???waiting.Dispatcher.BeginInvoke(() =>
? ?? ?? ?? ?? ?? ???{
? ?? ?? ?? ?? ?? ?? ?? ?waiting.Visibility = Visibility.Visible;
? ?? ?? ?? ?? ?? ?? ?? ?waiting.SetLoadingText(message);
? ?? ?? ?? ?? ?? ???});
? ?? ?? ?? ?? ? }
? ?? ?? ?? ?}
? ?? ?? ?? ?catch(Exception e)
? ?? ?? ?? ?{
? ?? ?? ?? ?? ? string str = e.Message.ToString();
? ?? ?? ?? ?}
? ?? ???}
? ?? ???/// <summary>
? ?? ???/// 顯示等待信息
? ?? ???/// </summary>
? ?? ???public static void ShowWaiting()
? ?? ???{
? ?? ?? ?? ?try
? ?? ?? ?? ?{
? ?? ?? ?? ?? ? if (waiting != null)
? ?? ?? ?? ?? ? {
? ?? ?? ?? ?? ?? ???waiting.Dispatcher.BeginInvoke(() =>
? ?? ?? ?? ?? ?? ???{
? ?? ?? ?? ?? ?? ?? ?? ?waiting.Visibility = Visibility.Visible;
? ?? ?? ?? ?? ?? ?? ?? ?waiting.SetLoadingText();
? ?? ?? ?? ?? ?? ???});
? ?? ?? ?? ?? ? }
? ?? ?? ?? ?}
? ?? ?? ?? ?catch (Exception e)
? ?? ?? ?? ?{
? ?? ?? ?? ?? ? string str = e.Message.ToString();
? ?? ?? ?? ?}
? ?? ???}
? ?? ???/// <summary>
? ?? ???/// 隱藏等待信息
? ?? ???/// </summary>
? ?? ???public static void HideWaiting()
? ?? ???{
? ?? ?? ?? ?if (waiting != null)
? ?? ?? ?? ?? ? waiting.Dispatcher.BeginInvoke(() => waiting.Visibility = Visibility.Collapsed);
? ?? ???}
? ? }
}
寫好上面之后,還不能直接使用,現在使用的話,還不能顯示等待信息,還要在程序集中加入一下代碼(一般是index.xaml中)
xmlns:local="clr-namespace:HIEG2.Portal.Common;assembly=HIEG2.Portal.Common"
<Grid x:Name="loadingGrid">
? ?? ???<local:Waiting x:Name="waiting" Visibility="Collapsed"/>
? ? </Grid>
設置上面之后,基本上已經可以了,下面就可以直接使用了,比如要加載一個數據,比較大,那么直接可以用,如下
Windows.ShowWaiting();
//寫加載的代碼,如果你使用異步加載,那么隱藏時要注意一下,要等待加載完數據之后再隱藏
Windows.HideWaiting();
自己能力有限,大家可以發揮自己的才智,可以把它做的更好
轉載于:https://www.cnblogs.com/888h/archive/2010/08/17/1801692.html
總結
以上是生活随笔為你收集整理的Silverlight数据加载时,等待图标显示与隐藏(Loading)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Solaris的syslog机制
- 下一篇: C#.NET通过Socket实现平行主机