2013-06-26 27 views
0

我正在開發WPF應用程序並遵循MVVM方法。當用戶在驗證時點擊「Enter」按鈕意味着我必須在我的登錄屏幕上顯示忙碌指示符。在'輸入'按鈕我有一個名爲'EnterCommand'的ICommand然後檢查身份驗證,然後在成功身份驗證加載MainWindow。在線程上進行用戶身份驗證時顯示Busy Indicator:線程錯誤(調用線程無法訪問此對象)

private ICommand _EnterCommand; 
public ICommand EnterCommand 
{ 
    get 
    { 
     return _EnterCommand ?? (_EnterCommand = new DelegateCommand(() => 
     { 
      Thread objThread = new Thread(LoadApplication); 
      objThread.SetApartmentState(ApartmentState.STA); 
      objThread.Start(); 

     })); 
    } 
} 

IsBusy屬性爲綁定到這個showprogress

private bool _ShowProgress = false; 
public bool ShowProgress 
{ 
    get { return _ShowProgress; } 
    set 
    { 
     if (_ShowProgress != value) 
     { 
      _ShowProgress = value; 
      FirePropertyChanged("ShowProgress"); 
     } 
    } 
} 

我創建這個命令然後從(bool Property name : ShowProgress) MVVM設置IsBusy屬性的線程。

在LoadApplication:

public void LoadApplication() 
{ 
    ShowProgress= true; 
    if (AuthenticateUser) 
    { 
     MainWindow objMainWindow = new MainWindow(); 
     objMainWindow.Show(); 
     Application.Current.MainWindow.Close(); 
    } 
    ShowProgress= false; 
} 

Error: objMainWindow.Show()拋出錯誤 - 因爲不同的線程擁有它調用線程不能訪問該對象。

另外在App.xaml中,我將StartupUri設置爲我的「登錄」窗口。

只要用戶點擊「Enter」按鈕,就能顯示Busy Indicator,但在顯示mainWindow時失敗。

只要我的MainWindow(這是主屏幕)沒有啓動,我必須顯示busyindicator

Error: objMainWindow.Show() in LoadApplication()拋出錯誤 - 調用線程不能訪問此對象,因爲不同的線程擁有它。

Stack Trace: 
    at System.Windows.Markup.XamlReader.RewrapException(Exception e, Uri baseUri) 
    at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter) 
    at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter) 
    at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField) 
    at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren) 
    at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate) 
    at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container) 
    at System.Windows.FrameworkElement.ApplyTemplate() 
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
    at System.Windows.UIElement.Measure(Size availableSize) 
    at System.Windows.Controls.StackPanel.MeasureOverride(Size constraint) 
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
    at System.Windows.UIElement.Measure(Size availableSize) 
    at System.Windows.Controls.Grid.MeasureOverride(Size constraint) 
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
    at System.Windows.UIElement.Measure(Size availableSize) 
    at System.Windows.Controls.Control.MeasureOverride(Size constraint) 
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
    at System.Windows.UIElement.Measure(Size availableSize) 
    at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV) 
    at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV) 
    at System.Windows.Controls.Grid.MeasureOverride(Size constraint) 
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
    at System.Windows.UIElement.Measure(Size availableSize) 
    at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint) 
    at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint) 
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
    at System.Windows.UIElement.Measure(Size availableSize) 
    at System.Windows.Documents.AdornerDecorator.MeasureOverride(Size constraint) 
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
    at System.Windows.UIElement.Measure(Size availableSize) 
    at System.Windows.Controls.Border.MeasureOverride(Size constraint) 
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
    at System.Windows.UIElement.Measure(Size availableSize) 
    at System.Windows.Window.MeasureOverrideHelper(Size constraint) 
    at System.Windows.Window.MeasureOverride(Size availableSize) 
    at System.Windows.FrameworkElement.MeasureCore(Size availableSize) 
    at System.Windows.UIElement.Measure(Size availableSize) 
    at System.Windows.Interop.HwndSource.SetLayoutSize() 
    at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value) 
    at System.Windows.Interop.HwndSource.set_RootVisual(Visual value) 
    at System.Windows.Window.SetRootVisual() 
    at System.Windows.Window.SetRootVisualAndUpdateSTC() 
    at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight) 
    at System.Windows.Window.CreateSourceWindow(Boolean duringShow) 
    at System.Windows.Window.CreateSourceWindowDuringShow() 
    at System.Windows.Window.SafeCreateWindowDuringShow() 
    at System.Windows.Window.ShowHelper(Object booleanBox) 
    at System.Windows.Window.Show() 
    at RBS.MIB.DS.Reporting.UI.MainViewModelStartupWindow.LoadApplication() in C:\DSReporting\trunk\Projects\csharp\UI\RBS.MIB.DS.Reporting.UI\StartUpWindow\StartupViewModel\MainViewModelStartupWindow.cs:line 104 
    InnerException: System.InvalidOperationException 
    Message=The calling thread cannot access this object because a different thread owns it. 
+0

這是最初的碼?你可以發佈stacktrace嗎? – Pragmateek

+0

@Pragmateek是的,這是原始代碼。 – user2519971

+0

你爲什麼要問同樣的問題兩次? http://stackoverflow.com/questions/17297895/displaying-busy-indicator-on-a-thread-and-launching-the-application-threading – makc

回答

1

請試試這個:

private BackgroundWorker _BgWorker; 

public LoginFormViewModel() 
{ 
    _BgWorker = new BackgroundWorker(); 
    _BgWorker.DoWork += new DoWorkEventHandler(bgw_DoWork); 
    _BgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted); 
    _BgWorker.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged); 
    _BgWorker.RunWorkerAsync(); 
} 

void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    // write your code to check authentication 
} 

void bgw_DoWork(object sender, DoWorkEventArgs e) 
{ 
    // write you code to open window 
} 

void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    // write the code for busy indicator like 
    ShowProgress= true; 
} 

希望這可以幫助你。

+0

謝謝。試過這個,但是既沒有顯示busyindicator也沒有顯示主窗口。 – user2519971

+0

我應該把LoginFormViewModel()代碼放在我的ICommand EnterCommand中 – user2519971

+0

在你使用這個命令我命令 – Krish

相關問題