我认为做这样的事情会很好(lambda做一个yield return):
public IList<T> Find<T>(Expression<Func<T, bool>> expression) where T : class, new()
{
IList<T> list = GetList<T>();
var fun = expression.Compile();
var items = () => {
foreach (var item in list)
if (fun.Invoke(item))
yield return item; // This is not allowed by C#
}
return items.ToList();
}
Run Code Online (Sandbox Code Playgroud)
但是,我发现我不能在匿名方法中使用yield.我想知道为什么.该产量的文档只是说,这是不允许的.
由于不允许,我只创建了List并将项目添加到其中.
我最近做了一个UserControl,花了很长时间,因为我不得不使用自定义依赖属性等等...
无论如何,它只是一堆3个控件:TextBox,Popup with Hierarchical Tree.
现在我意识到我可能只能写一个ControlTemplate.那么使用UserControl有什么好处?
我正在使用MVVM模式开发WPF应用程序.
该应用程序从服务器加载验证码图像,并在准备好时将其分配给WPF表单上的图像.我正在使用BackgroundWorker为我做线程,如下所示:
加载Window时,会调用以下内容:
BackgroundWorker _bgWorker = new BackgroundWorker();
_bgWorker.DoWork += GetCaptchaImage;
_bgWorker.RunWorkerAsync();
Run Code Online (Sandbox Code Playgroud)
GetCaptchaImage函数非常简单,在另一个线程中加载图像:
BitmapSource _tempBitmap = GetCaptchaFromServer();
Run Code Online (Sandbox Code Playgroud)
我需要知道如何调用Dispatcher将此ImageSource分配给我的Window的图像源,目前我在加载_tempBitmap之后调用调度程序,如下所示:
Application.Current.Dispatcher.Invoke(
new Action(() => CaptchaBitmap = _tempBitmap));
Run Code Online (Sandbox Code Playgroud)
CaptchaBitmap数据绑定到我的图像源.
但是,当我这样做时,抛出InvalidOperationException,并且对_tempBitmap的任何引用都会在GUI线程中返回错误.我知道它是因为我在调度程序GUI线程中访问它,当它在BackgroundWorker线程中创建时,但我该如何解决它?
非常感谢帮助!:)