2011-12-19 48 views
1

我在Silverlight 4中有一個登錄頁面。我正在使用MVVM處理與頁面的所有交互。成功登錄後,我有一個自定義事件在我的視圖模型中觸發,隨後是導航到主頁面的代碼。我使用事件聚合,發佈&訂閱在視圖之間導航。等待故事板動畫在使用MVVM更改Silverlight 4中的視圖之前完成

AnimateLoginSuccess(this, null); 
//code to navigate to the main page after successful login. 

我處理這個事件在我看來,包裝它這個樣子,哪裏LoginSuccessStoryboard是我對於一些基本的動畫創建的故事板:

//constructor of view 
    public Login(LoginVM vm) 
    { 
     InitializeComponent(); 

     DataContext = vm; 
     thisVM = vm; 

     vm.AnimateLoginSuccess += new EventHandler(vm_AnimateLoginSuccess); 

    } 


//event handling method in view 
    void vm_AnimateLoginSuccess(object sender, EventArgs e) 
    { 
     LoginSuccessStoryboard.Begin(); 
    } 

現在,問題是,即使雖然動畫是在成功登錄後開始的,但下一行處理移動到成功登錄時的不同視圖時,會快速移動到下一個視圖,以至於不會等待動畫完成。從而使動畫幾乎不存在。關於如何使這項工作的任何想法?

回答

5

您可以使用Storyboard的Completed事件在動畫結束後導航到您的視圖。向ViewModel添加一個方法,該方法在動畫之後執行,以導航到默認的第一個視圖。

public Login(LoginVM vm) { 
    InitializeComponent(); 

    LoginSuccessStoryboard.Completed += new EventHandler(NavigateToViewAfterAnimation); 
    DataContext = vm; 
    thisVM = vm; 

    vm.AnimateLoginSuccess += new EventHandler(vm_AnimateLoginSuccess); 

} 

private void NavigateToViewAfterAnimation(object sender, EventArgs e) { 
    thisVW.NavigateToFirstView(); // Navigates to the first view. 
} 
相關問題