我寫我的第一個WPF應用程序,其中包括幾個頁面:WPF:dispatcher.invoke不運行
- 歡迎頁面一些標誌
- 登錄頁面與登錄表單
- 帶帳號信息的主頁
MainWindow包含<Frame>
WPF控件和我使用動畫顯示下一頁/上一頁。
我寫我自己的MainAnimation
類來執行動畫。
此應用程序在我的筆記本電腦上正常工作,但是當我嘗試在我的朋友動畫的機器上運行它時,什麼都不做。
我覺得麻煩Dispatcher.Invoke()
方法調用相關的,我試圖找到解決方案通過網絡(hereherehere和here),我嘗試:
- 使用Application.Current.Dispatcher
- 使用Dispatcher.BeginInvoke()代替Dispatcher.Invoke()
但它什麼都不做。
所以,我顯示歡迎頁面只有2秒,登錄頁面必須自動加載。
這是WelcomePage.xaml.cs文件中的代碼:
public partial class WelcomePage : Page {
public WelcomePage (MainWindow parent) {
InitializeComponent();
this.parent = parent;
Task.Factory.StartNew(() => ShowLoginForm());
}
private MainWindow parent;
private void ShowLoginForm()
{
Thread.Sleep(2000);
this.parent.GoToLoginForm();
}
}
這是MainWindow.xaml.cs文件中的代碼:
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
animation = new MainAnimation(this, this, Main, new WelcomePage(this));
}
private MainAnimation animation;
public void GoToLoginForm() => animation.ShowNextPage(new LoginPage(this));
public void GoToVideosForm() => animation.ShowNextPage(new MainPage(this));
}
這關係零件MainAnimation
類(MainAnimation.cs):
public class MainAnimation
{
public MainAnimation(FrameworkElement resourcesOwner, DispatcherObject dispatcherOwner, Frame currentPageContainer, Page firstPage)
{
this.resourcesOwner = resourcesOwner;
this.dispatcherOwner = dispatcherOwner;
this.currentPageContainer = currentPageContainer;
pages = new Stack<Page>();
pages.Push(firstPage);
currentPageContainer.Content = pages.Peek();
}
private Stack<Page> pages;
private FrameworkElement resourcesOwner;
private DispatcherObject dispatcherOwner;
private Frame currentPageContainer;
private void ShowPageForward()
{
dispatcherOwner.Dispatcher.Invoke((Action)delegate {
if (currentPageContainer.Content != null)
{
var page = currentPageContainer.Content as Page;
if (page != null)
{
page.Loaded -= NextPage_Loaded;
UnloadPageForward(page);
}
}
else
{
LoadPageForward();
}
});
}
private void UnloadPageForward(Page page)
{
Storyboard sb = (resourcesOwner.FindResource("SlideForwardOut") as Storyboard).Clone();
sb.Completed += StoryboardForward_Completed;
sb.Begin(currentPageContainer);
}
private void StoryboardForward_Completed(object sender, EventArgs e)
{
LoadPageForward();
}
private void LoadPageForward()
{
pages.Peek().Loaded += NextPage_Loaded;
currentPageContainer.Content = pages.Peek();
}
private void NextPage_Loaded(object sender, RoutedEventArgs e)
{
Storyboard sb = resourcesOwner.FindResource("SlideForwardIn") as Storyboard;
sb.Begin(currentPageContainer);
}
}
我是新來的WPF,可能只是不明白一些細節,所以如果你能幫我解決這個小問題但是非常討厭的話,我會很高興的。
更新#1:軟件版本
- OS的開發:視窗10 64
- OS測試:Windows 8.1中的x64
- VS版本:Visual Studio的2017年社區版
- 應用目標框架:v.4。5
爲什麼要在後臺線程上調用ShowLoginForm? WPF控件具有線程關聯。你爲什麼要睡覺? – mm8
我認爲如果我不在後臺線程上調用ShowLoginForm,應用程序看起來像「不響應」。但我怎麼能做另一種方式? –
如果你只是刪除Task.Factory.StartNew和Thread.Sleep? – mm8