近期做了两个wpf项目,虽然wpf同属于c#语言,但是并没有系统性的学习过,基本上都是在开发过程中遇到问题然后百度.虽然也学习到了一些知识,但是太过碎片,脑海中细细一想并没有一个清晰的知识画面.因此在这里做一个总结.
1.辅助线程与窗体线程交互数据问题.wpf中辅助线程没有权限与窗体进行交互,因此我们需要通过wpf中提供的Dispach.Invoke函数把辅助线程提供的数据绑定到窗体中.invoke函数的第一个参数是一个delegate,第二个是objects[],对应delegate绑定的函数的参数个数.
示例:(未给出线程创建部分,ThreadForShowSource函数是绑定到线程的函数)
private delegate void ReadToForm(localhost.ReadAndWriteModel model);
private localhost.ReadAndWriteModel ModelToSource(localhost.DataModel model)
{
localhost.PacketsService ps = new localhost.PacketsService();
return ps.GetReadAndWriteModel(model, ComString);
}
private void ThreadForShowSource()
{
ReadToForm myinvoke = new ReadToForm(SourceToForm);
while (true)
{
localhost.DataModel model = new localhost.DataModel();
model.Order = localhost.OrderEnum.高精度读;
model.DevNum = 0;
localhost.ReadAndWriteModel newmodel = ModelToSource(model);
readAndWriteModel = newmodel;
this.Dispatcher.Invoke(myinvoke, newmodel);
Thread.Sleep(new TimeSpan(0, 0, 10));
}
}
private void SourceToForm(localhost.ReadAndWriteModel newmodel)
{
if (newmodel != null && newmodel.Order == localhost.OrderEnum.高精度读)
{
Text_VoltageA.Text = newmodel.VoltageA.ToString("F2");
Text_VoltageB.Text = newmodel.VoltageB.ToString("F2");
Text_VoltageC.Text = newmodel.VoltageC.ToString("F2");
Text_CurrentA.Text = newmodel.CurrentA.ToString("F2");
Text_CurrentB.Text = newmodel.CurrentB.ToString("F2");
Text_CurrentC.Text = newmodel.CurrentC.ToString("F2");
if (newmodel.VoltageA == 0 && newmodel.VoltageB == 0 && newmodel.VoltageC == 0 && newmodel.CurrentA == 0 && newmodel.CurrentB == 0 && newmodel.CurrentC == 0)
{
Text_Frequent.Text = "0.00";
Text_Phrase.Text = "0.00";
}
else
{
Text_Frequent.Text = "50.00";
Text_Phrase.Text = newmodel.PhaseIA.ToString("F2");
}
}
}
2.webservice服务的异步调用.涉及到与服务进行交互,尤其是有数据回传时,采用异步调用的方式可以解决窗体假死的问题.服务的异步调用,分为两步,第一,绑定把异步调用完成处理函数绑定到异步调用完成事件上,第二,异步调用服务函数.
示例:
private void EmergencyClose(object sender, RoutedEventArgs e)
{
MessageText.Text = "正在尝试解除告警.时间:" + DateTime.Now.ToString();
localhost.PacketsService ps = new localhost.PacketsService();
localhost.DataModel model = new localhost.DataModel();
model.DevNum = 0;
model.Order = localhost.OrderEnum.告警解除;
ps.GetReplyModelCompleted += new localhost.GetReplyModelCompletedEventHandler(EmergencyCloseCompleted);//绑定异步完成处理函数
ps.GetReplyModelAsync(model, ComString);//异步调用
}
private void EmergencyCloseCompleted(object o, localhost.GetReplyModelCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show("发生错误:" + e.Error.Message);
MessageText.Text = "告警解除失败,发生错误.请重新操作.时间:" + DateTime.Now.ToString();
}
else
{
localhost.ReplyModel rmodel = e.Result as localhost.ReplyModel;
if (rmodel != null && rmodel.Order == localhost.OrderEnum.确认应答)
{
MessageText.Text = "告警解除成功.时间:" + DateTime.Now.ToString();
localhost.PacketsService ps = new localhost.PacketsService();
ps.RemoveEmergencyAsync(ComString);
}
else
{
MessageBox.Show("源解除告警失败!");
MessageText.Text = "告警解除失败,发生错误.请重新操作.时间:" + DateTime.Now.ToString();
}
}
}
总结:对于这些分散的知识点,目前还没有找到合适的学习方法进行学习,只能通过写写总结来进行学习.今天读了windows系统原理第六版,第一章基本上讲的是微机原理里的东西,日益发现自己的基础薄弱.大学时学习的微机原理基本已经忘光光.年龄越来越大,真怀念高中时的记忆力.