2014-02-25 4 views
3

WPF 프로젝트가 있고 프로젝트 마법사가 스플래시 화면을 추가했습니다. 같은 시작 화면에서 진행률 막대 스타일 미터를 추가하고 싶습니다. 누군가이 일을하는 방법을 알고 있습니까?WPF SplashScreen with ProgressBar

답변

9

여기에 대한 나의 계획입니다. 내가 UI 스레드에서 실행되는 초기화 코드를 갖고 싶지 않고 일반적으로 내 App 클래스 (시작 화면이 아님)에 초기화 코드를 넣고 싶지는 않다.

기본적으로 볼을 굴리는 내 스플래시 화면에 App StartupUri을 설정합니다.

스플래시 화면에서 응용 프로그램에 대한 대리자를 다시 호출합니다. 이것은 작업자 스레드에서 실행됩니다. 스플래시 화면에서 나는 EndInvoke을 처리하고 창을 닫습니다.

응용 프로그램 초기화 대리인에서 작업을 수행하고 끝에 일반 기본 창을 만들고 엽니 다. 작업로드 중에 슬래시를 사용하여 진행 상황을 업데이트 할 수있는 메서드가 있습니다.

좋습니다, 코드는 매우 짧으며 (이 모든 영향을받지 않는) 기본 창 코드는 포함되어 있지 않지만 익명의 대리인과 함께 오리와 다이빙을하므로주의 깊게 읽고 이상적으로 사용하십시오 디버거. 여기

뒤에

<Application x:Class="SplashScreenDemo.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    StartupUri="Splash.xaml"> 
    <Application.Resources> 

    </Application.Resources> 
</Application> 

응용 프로그램 코드 ...

internal delegate void Invoker(); 
public partial class App : Application 
{ 
    public App() 
    { 
     ApplicationInitialize = _applicationInitialize; 
    } 
    public static new App Current 
    { 
     get { return Application.Current as App; } 
    } 
    internal delegate void ApplicationInitializeDelegate(Splash splashWindow); 
    internal ApplicationInitializeDelegate ApplicationInitialize; 
    private void _applicationInitialize(Splash splashWindow) 
    { 
     // fake workload, but with progress updates. 
     Thread.Sleep(500); 
     splashWindow.SetProgress(0.2); 

     Thread.Sleep(500); 
     splashWindow.SetProgress(0.4); 

     Thread.Sleep(500); 
     splashWindow.SetProgress(0.6); 

     Thread.Sleep(500); 
     splashWindow.SetProgress(0.8); 

     Thread.Sleep(500); 
     splashWindow.SetProgress(1); 

     // Create the main window, but on the UI thread. 
     Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Invoker)delegate 
     { 
      MainWindow = new Window1(); 
      MainWindow.Show(); 
     });   
    } 
} 

시작 xaml (실제로, 여기 너무 흥미로운 아무것도 ...)

<Window x:Class="SplashScreenDemo.Splash" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Splash" Height="300" Width="300"> 
    <Grid> 
     <TextBlock Height="21" Margin="91,61,108,0" VerticalAlignment="Top">Splash Screen</TextBlock> 
     <ProgressBar Name="progBar" Margin="22,122,16,109" Minimum="0" Maximum="1"/> 
    </Grid> 
</Window> 
.... 코드입니다

스플래시 코드 숨김 ...

public partial class Splash : Window 
{ 
    public Splash() 
    { 
     InitializeComponent(); 
     this.Loaded += new RoutedEventHandler(Splash_Loaded); 
    } 

    void Splash_Loaded(object sender, RoutedEventArgs e) 
    { 
     IAsyncResult result = null; 

     // This is an anonymous delegate that will be called when the initialization has COMPLETED 
     AsyncCallback initCompleted = delegate(IAsyncResult ar) 
     { 
      App.Current.ApplicationInitialize.EndInvoke(result); 

      // Ensure we call close on the UI Thread. 
      Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Invoker)delegate { Close(); }); 
     }; 

     // This starts the initialization process on the Application 
     result = App.Current.ApplicationInitialize.BeginInvoke(this, initCompleted, null); 
    } 

    public void SetProgress(double progress) 
    { 
     // Ensure we update on the UI Thread. 
     Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Invoker)delegate { progBar.Value = progress; });   
    } 
} 

작업이 작업자 스레드에서 수행됨에 따라 진행률 표시 줄이 잘 업데이트되고 시작 화면에있는 애니메이션으로 인해 엔터테인먼트 롤링이 유지됩니다.

+0

그래서 초기화하는 모든 것이 전역 변수에 저장됩니까? –

+0

(Invoker)가 나를 위해 작동하지 않고 "새로운 액션 (delegate()"으로 대체했습니다 –

+3

밑줄로 시작하는 메소드 선언 ... 정말요? – Slugart

관련 문제