2016-10-12 3 views
0

しかし、私はそれを個別に実行すると完全に動作します。内部例外:例外をスローするユーザー名とパスワードを入力するたびに、別のスレッドがそれを所有しているため、呼び出しスレッドはこのオブジェクトにアクセスできません。

例外がスロー: 'System.Windows.Markup.XamlParseException' をコード

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) 
    { } 
} 

PresentationFramework.dll

では、これはロードウィンドウが、これはロード・ウィンドウで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.Markup.XamlParseException' 'System.Windows.Controls.Button' の初期設定が例外をスローしました。

+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()); 

をしかし、あなたのXamlParseExceptionはそのInitialization of 'System.Windows.Controls.Button' threw an exceptionと私ドンを述べているので、さらに多くのUI関連のステートメントがあるように持っていますあなたが掲示したコードの中にただ一つのButtonがあります。

他のUI関連のコールがあるかどうかを確認し、右のDispatcherを使用して呼び出します。 (特に、アプリケーションで複数のDispatchersを持っている場合は、あなたのLoading_WindowApplication.Current.Dispatcherを使用していない注意を払っています。ただ、その場合にはDispatcherまたはDispatcher.CurrentDispatcherを使用しています。)

そして最後にではなく、少なくとも私がそこにあることをかなり確信しているがさらにと記載されているXamlParseExceptionInnerExceptionがあります。それもチェックしてください。それは別の原因かもしれないし、それはちょうど上記と同じかもしれない。どちらかがButtonから継承するコントロールのコンストラクタにあるか、おそらくXAMLのどこかにあります。

関連する問題