1.新建一个UserControl
<UserControl x:Class="LoadingPage.PopupSplash" 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}" d:DesignHeight="480" d:DesignWidth="480"> <Grid x:Name="LayoutRoot" Background="White" Width="480" Height="800"> <ProgressBar HorizontalAlignment="Left" Margin="0,755,0,0" Name="progressBar1" Width="480" Background="DarkRed" /> <TextBlock HorizontalAlignment="Left" Margin="171,656,0,97" Name="textBlock1" Text="Loading..." Width="208" Foreground="Black" FontSize="30" /> </Grid> </UserControl>
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace LoadingPage { public partial class PopupSplash : UserControl { public PopupSplash() { InitializeComponent(); this.progressBar1.IsIndeterminate = true;//指示进度条是使用重复模式报告一般进度 } } }
MainPage.xaml没有变动
<phone:PhoneApplicationPage x:Class="LoadingPage.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" mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True"> <!--LayoutRoot 是包含所有页面内容的根网格--> <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitlePanel 包含应用程序的名称和页标题--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock x:Name="PageTitle" Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel> <!--ContentPanel - 在此处放置其他内容--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid> </Grid> </phone:PhoneApplicationPage>
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using System.Windows.Controls.Primitives; using System.ComponentModel; using System.Threading; namespace LoadingPage { public partial class MainPage : PhoneApplicationPage { private Popup popup; private BackgroundWorker backroungWorker; // 构造函数 public MainPage() { InitializeComponent(); ShowPopup(); } private void ShowPopup() { this.popup = new Popup(); this.popup.Child = new PopupSplash();//设置 Popup 控件的内容,把自定义的PopupSplash控件填充到popup控件上 this.popup.IsOpen = true; StartLoadingData();//开始加载数据 } private void StartLoadingData() { //使用BackgroundWorker在单独的线程上执行操作 backroungWorker = new BackgroundWorker(); //调用 RunWorkerAsync后台操作时引发此事件,即后台要处理的事情写在这个事件里面 backroungWorker.DoWork += new DoWorkEventHandler(backroungWorker_DoWork); //当后台操作完成事件 backroungWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backroungWorker_RunWorkerCompleted); //开始执行后台操作 backroungWorker.RunWorkerAsync(); } //后台操作完成 void backroungWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { this.Dispatcher.BeginInvoke(() => { this.popup.IsOpen = false;//关闭popup 注意要使用Dispatcher.BeginInvoke开跟UI通讯 } ); } //后台操作处理 void backroungWorker_DoWork(object sender, DoWorkEventArgs e) { // 程序初始化处理 这里只是模拟了一下 Thread.Sleep(7000); } } }