2016-10-12 41 views
0

只要我輸入用戶名和密碼它會引發異常。但是當我單獨運行它時,它運行完美。內部異常:調用線程不能訪問此對象,因爲不同的線程擁有它

拋出異常: 'System.Windows.Markup.XamlParseException' 在PresentationFramework.dll

代碼

private void testing() 
{ 
    try 
    { 
     Thread newWindowThread = new Thread(new ThreadStart(() => 
     { 
      SynchronizationContext.SetSynchronizationContext(
       new DispatcherSynchronizationContext(
        Dispatcher.CurrentDispatcher)); 

      var loading_Win = new Loading_Window(); 

      loading_Win.Show(); 

      loading_Win.Closed += (s, ex) => 
       Dispatcher.CurrentDispatcher.BeginInvokeShutdown(
        DispatcherPriority.Background); 

      // Start the Dispatcher Processing 
      System.Windows.Threading.Dispatcher.Run(); 
     })); 

     // Set the apartment state 
     newWindowThread.SetApartmentState(ApartmentState.STA); 

     // Make the thread a background thread 
     newWindowThread.IsBackground = true; 

     // Start the thread 
     newWindowThread.Start(); 
    } 
    catch (Exception ex) 
    { } 
} 

這是裝載時限CS

public partial class Loading_Window : MetroWindow 
{ 
    public Random rnd = new Random(); 
    public static int intIteration = 1; 
    private System.Windows.Forms.Timer timer1; 

    public Loading_Window() 
    { 
     InitializeComponent(); 
     InitTimer(); 

     this.Closing += Loading_Window_Closing; 
    } 

    private void Loading_Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
    { 
     //Process[] pro = null; 
     //try 
     //{ 
     // pro = Process.GetProcessesByName("LeanPims"); 
     // pro[0].Kill(); 
     //} 
     //catch (Exception ex) 
     //{ } 
     //Process.GetCurrentProcess().Kill(); 
    } 

    public void InitTimer() 
    {    
     intIteration = 0; 
     timer1 = new System.Windows.Forms.Timer(); 
     timer1.Tick += new EventHandler(timer1_Tick); 
     timer1.Interval = 300; // in miliseconds 
     timer1.Start(); 
    } 

    public void timer1_Tick(object sender, EventArgs e) 
    { 
     intIteration += rnd.Next(1, 8); 
     label1.Content = intIteration.ToString() + " %"; 

     try 
     { 
      if (intIteration >= 100) 
      { 
       timer1.Stop(); 
       timer1 = null; 
       this.Close(); 
      } 
     } 
     catch (Exception ex) { } 
    } 
} 

這是加載窗口xaml

<Viewbox Stretch="Fill"> 
    <Grid Height="500" Width="700" > 
     <Image Stretch="Fill" Width="300" Height="350" gif:ImageBehavior.AnimatedSource="Images\Loading1.gif" /> 
     <Rectangle HorizontalAlignment="Left" Height="45" Margin="298,228,0,0" StrokeThickness="2" VerticalAlignment="Top" Width="103" Fill="White"/> 
     <Label Content="" x:Name="label1" HorizontalAlignment="Left" Height="36" Margin="327,236,0,0" VerticalAlignment="Top" Width="62" FontSize="18" FontFamily="Arial" Foreground="#FF837F7F" Background="{x:Null}" FontWeight="Bold"/> 
     <Canvas x:Name="c1" HorizontalAlignment="Left" Height="406" Margin="112,38,0,0" VerticalAlignment="Top" Width="481"/> 
    </Grid> 
</Viewbox> 

拋出異常:在PresentationFramework.dll

'System.Windows.Controls.Button' 初始化 'System.Windows.Markup.XamlParseException' 引發了異常。

+0

究竟哪一行引發異常,那麼Loading_Window真的有什麼作用? –

+0

Loding_Win.Show();拋出異常並加載窗口是一個圓形的進度條.....它的一個Gif文件,看起來像一個加載窗口。 –

+0

這很奇怪......我用一個虛擬的'Window'測試了你的代碼,它看起來非常好。此外,這與我發佈的LOB WPF應用程序中使用的原理完全相同,因爲這些準確的代碼很好。請仔細檢查'InnerException'的堆棧跟蹤。 '.Show()'是真正的原因還是在'InnerException'中可能存在另一個'InnerException'?你可以發佈'Loading_Window'的內容嗎?那裏肯定有一些非常具體的事情發生。 – haindl

回答

0

問題是,您使用System.Windows.Forms.Timer來設置label1.Content

要麼你必須使用一個System.Windows.Threading.DispatcherTimer代替或你必須手動調用調度使用:

Dispatcher.Invoke(() => label1.Content = intIteration.ToString() + " %"); 

這同樣適用於其他所有UI相關的調用正確的。

如果你不想使用DispatcherTimer那麼你已經這樣做還有:

Dispatcher.Invoke(() => this.Close()); 

但是,必須更加UI相關的語句,因爲你XamlParseException指出Initialization of 'System.Windows.Controls.Button' threw an exception和我不在您發佈的代碼中看不到一個Button

因此,檢查是否有任何其他與UI相關的調用,並使用正確的Dispatcher調用它們。 (特別要注意的是你,如果你在你的應用程序有多個Dispatchers不要在您Loading_Window使用Application.Current.Dispatcher。只是在這種情況下使用DispatcherDispatcher.CurrentDispatcher

最後但並非最不重要的我敢肯定,有在XamlParseException中的另一個InnerException表示Initialization of 'System.Windows.Controls.Button' threw an exception。請檢查一下。這可能是另一個原因,或者它可能與上面相同。要麼是在繼承自Button的控件的構造函數中,要麼它可能位於XAML中的某處。

相關問題