只要我輸入用戶名和密碼它會引發異常。但是當我單獨運行它時,它運行完美。內部異常:調用線程不能訪問此對象,因爲不同的線程擁有它
拋出異常: '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' 引發了異常。
究竟哪一行引發異常,那麼Loading_Window真的有什麼作用? –
Loding_Win.Show();拋出異常並加載窗口是一個圓形的進度條.....它的一個Gif文件,看起來像一個加載窗口。 –
這很奇怪......我用一個虛擬的'Window'測試了你的代碼,它看起來非常好。此外,這與我發佈的LOB WPF應用程序中使用的原理完全相同,因爲這些準確的代碼很好。請仔細檢查'InnerException'的堆棧跟蹤。 '.Show()'是真正的原因還是在'InnerException'中可能存在另一個'InnerException'?你可以發佈'Loading_Window'的內容嗎?那裏肯定有一些非常具體的事情發生。 – haindl