2017-03-27 141 views
0

我正在创建一个WPF,我有2个线程。一个是主要的,另一个(称为PillTimeOutChecker)检查主窗体中的一些需求。如果需求满足,通过PilleCheckerThread的新表单将在新线程中显示。问题是我得到这个错误:'System.Windows.Controls.Button'的初始化在新窗体的初始化中抛出了一个异常。初始化'System.Windows.Controls.Button'抛出一个异常

这是在主线程的方法,即调用PillCheckerThread:

private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     Thread PillChecker = new Thread(new ThreadStart(PillCheckerThread)); 
     PillChecker.SetApartmentState(ApartmentState.STA); 
     PillChecker.IsBackground = true; 
     PillChecker.Name = "PillTimeOutChecker"; 
     PillChecker.Start(); 
    } 

这是PillCheckerThread方法的内容:

private void PillCheckerThread() 
    { 
      foreach (DataGridObject item in PillList.Items) 
      { 
       if(item.DoIt) 
       { 
         //Show PillWarning window 
         Thread PillWarningWindow = new Thread(new ThreadStart(() => 
         { 
          PillWarningWindow pl = new PillWarningWindow(item.PillName, item.AlertTime); 
          pl.Show(); 
           System.Windows.Threading.Dispatcher.Run(); 
          })); 
         PillWarningWindow.SetApartmentState(ApartmentState.STA); 
         PillWarningWindow.IsBackground = true; 
         PillWarningWindow.Start(); 
       } 
      } 
    } 

这是PillWarningWindow的内容:

public partial class PillWarningWindow : Window 
{ 
    public PillWarningWindow(string PillName, string CurrentTime) 
    { 
     InitializeComponent(); 
     PillNameLbl.Content = PillName; 
     TimeLbl.Content = CurrentTime; 
    } 

    private void CloseBtn_Click(object sender, RoutedEventArgs e) 
    { 
     this.Close(); 
    } 
} 

这是PillWarningWindow的xaml:

<Window x:Class="MedicalReminder.PillWarningWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:MedicalReminder" 
    mc:Ignorable="d" Height="300" Width="713.414" ShowInTaskbar="False" Topmost="True" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None" Background="Transparent" AllowsTransparency="True"> 
<Border BorderBrush="Black" CornerRadius="20" Background="DarkCyan"> 
    <Grid> 
     <Button x:Name="CloseBtn" Content="OK" HorizontalAlignment="Left" Margin="262,239,0,0" VerticalAlignment="Top" Width="179" Click="CloseBtn_Click"/> 
     <Label x:Name="label" Content="Psssttt !! It's time to take your pill: " HorizontalAlignment="Left" Margin="89,93,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="18"/> 
     <Label x:Name="PillNameLbl" Content="PillName" HorizontalAlignment="Left" Margin="395,93,0,0" VerticalAlignment="Top" FontSize="18" FontWeight="Bold"/> 
     <Label x:Name="label2" Content="It's exactly " HorizontalAlignment="Left" Margin="89,132,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="18"/> 
     <Label x:Name="TimeLbl" Content="12:00" HorizontalAlignment="Left" Margin="195,133,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="17" Width="56"/> 
     <Label x:Name="label3" Content="you forgot it ??" HorizontalAlignment="Left" Margin="256,132,0,0" VerticalAlignment="Top" FontWeight="Bold" FontSize="18"/> 

    </Grid> 
</Border> 

随着在PillWarningWindow的构造函数断点我发现,在InitializeComponent方法错误的开始。任何帮助表示赞赏。谢谢。

+0

不能说为什么你的代码将无法正常工作。也许内在的例外将有助于发现问题。但我不建议在你的数据网格中使用行和列,而不是使用边距来对齐你的控件。这是非常糟糕的代码。 –

+0

@MightyBadaboom感谢您的指导。我将更改边距/与行和列对齐! –

+0

欢迎您:) –

回答

0

如果你想访问windows propety,你应该使用Dispatchers。

由于另一个线程无法直接访问窗口。当您想要运行将在UI线程上执行的操作时,可以使用Dispatcher。

Application.Current.Dispatcher.BeginInvoke(
    DispatcherPriority.Background, 
    new Action(() => PillNameLbl.Content = PillName; 
    TimeLbl.Content = CurrentTime)); 
+0

不幸的是,这并没有解决我的问题。我一直在收到同样的错误。我试图把InitializeComponent方法也放在一个调度程序中......但是然后我得到这个错误:调用线程无法访问这个对象,因为不同的线程拥有它这里是一个截图:http://prntscr.com/ep0qit –

0

如果调试器停在InitializeComponent方法,我会说有可能是出错了XAML代码(尽管是满的不良做法,似乎没有是不正确的)。你确定你的控件没有在其他地方定义的样式资源吗?我之前有过相同的错误,并且是由错误顺序定义样式导致的。

尝试直接在应用程序启动时启动窗口,而不是从另一个线程启动它,只是为了测试它是否正常。如果有效,那么问题在于线程管理。

相关问题