我在新的WPF应用程序中使用BackgroundWorker,我需要报告进度/更新UI,因为它在后台工作。WPF/XAML Property'not'on'object'
我在WIndows Forms应用程序中做了很长一段时间。我刚刚为WPF重写了它,这让我感到头疼。
它一直在运行时引发了以下错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=5046349)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=5046349); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=5046349)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=5046349); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=6619237)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=6619237); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=6619237)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=6619237); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=7536755)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=7536755); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=7536755)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=7536755); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=7536755)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=7536755); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=7536755)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=7536755); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=6357089)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=6357089); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=6357089)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=6357089); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=6750311)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=6750311); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=6750311)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=6750311); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Sender' property not found on 'object' ''Char' (HashCode=6619237)'. BindingExpression:Path=Sender; DataItem='Char' (HashCode=6619237); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 40 : BindingExpression path error: 'Subject' property not found on 'object' ''Char' (HashCode=6619237)'. BindingExpression:Path=Subject; DataItem='Char' (HashCode=6619237); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
我不知道什么实际意义。一些谷歌搜索没有透露任何有用的信息。
我还会指出,如果我不使用WPF中的BGWorker,代码实际上检索所有邮件就好了。但是,它只会停止工作,并在我使用后台工作时停止绑定。我不知道为什么。 完全相同的代码适用于BGWorker的WinForms。
这个错误的真正含义是什么,我能做些什么来摆脱它?
代码隐藏:
public partial class MainWindow : Window
{
public BackgroundWorker worker = new BackgroundWorker();
public ObservableCollection<Message> messages = new ObservableCollection<Message>();
public MailMessage msg;
int count = 0;
public class Message
{
public string Sender { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
public DateTime DateReceived { get; set; }
public DateTime DateRead { get; set; }
public MailMessage Mail { get; set; }
}
public MainWindow()
{
InitializeComponent();
worker.WorkerSupportsCancellation = true;
worker.WorkerReportsProgress = true;
worker.ProgressChanged += Worker_ProgressChanged;
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
worker.DoWork += Worker_DoWork;
}
private void RetrieveMessages()
{
// Working code.
using (var imap = new AE.Net.Mail.ImapClient())
{
for(int i = 0; i < count; i++)
{
MailMessage msg = imap.GetMessage(i, true, false);
worker.ReportProgress(i);
}
}
}
private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
if(count != 0)
RetrieveMessages();
else
{
using(var imap = new AE.Net.Mail.ImapClient())
{
count = imap.GetMessageCount("Inbox");
}
}
}
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
status.Text = "Status: Idle.";
list.ItemsSource = messages;
}
private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Console.WriteLine(msg.Sender.Address + " " + msg.Subject);
MessageBox.Show(msg.Subject);
if(msg != null)
{
messages.Add(new Message()
{
Sender = "hi",
Subject = msg.Subject,
Content = msg.Body,
Mail = msg
});
msg = null;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// DEBUG ONLY
worker.RunWorkerAsync();
status.Text = "Status: Synchronizing.";
}
}
XAML:
<ScrollViewer ScrollViewer.CanContentScroll="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<ListView x:Name="list" ItemsSource="{Binding Source=Message}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="{x:Null}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch">
<TextBlock x:Name="senderLabel" Text="{Binding Sender}" HorizontalAlignment="Stretch" TextTrimming="WordEllipsis" TextWrapping="Wrap" FontWeight="SemiBold" />
<TextBlock x:Name="subjectLabel" Text="{Binding Subject}" HorizontalAlignment="Stretch" TextTrimming="WordEllipsis" TextWrapping="Wrap" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollViewer>
改变你的ListView绑定到Collection:的ItemsSource =“{绑定邮件}“ – Mark
@Mark谢谢Mark当我回到楼梯时,我会试试这个片刻。 –
@Mark,我已将它从消息更改为XAML中的消息,但我仍然显示相同的错误。 –