我正在创建一个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方法错误的开始。任何帮助表示赞赏。谢谢。
不能说为什么你的代码将无法正常工作。也许内在的例外将有助于发现问题。但我不建议在你的数据网格中使用行和列,而不是使用边距来对齐你的控件。这是非常糟糕的代码。 –
@MightyBadaboom感谢您的指导。我将更改边距/与行和列对齐! –
欢迎您:) –