.net c#使用ObservableAsPropertyHelper混合崩溃

Blend crash with ObservableAsPropertyHelper
2021-02-23
  •  译文(汉语)
  •  原文(英语)

当我尝试加载在Visual Studio 2013中开发的WPF解决方案时,Microsoft Blend 2013立即崩溃.我收到的(缩写)错误消息是:System.Exception:对象(通常是ObservableAsPropertyHelper)上发生OnError,它将破坏绑定或命令.为防止这种情况,请订阅对象的ThrownExceptions属性---> System.InvalidOperationException:在应用程序配置文件中找不到名为'RecipeModelContainer'的连接字符串.

该应用程序可以很好地构建并在我运行它时执行.在此特定问题开始之前,Blend始终显示丢失的连接字符串消息.此消息起源于我在代码隐藏区中设置DataContext时,并且自从将DataContext引用传输到XAML以来一直持续存在.无论如何,连接字符串绝对是存在的.

我正在尝试使用ReactiveUI开发用户控件,并且正在使用ObservableAsPropertyHelper属性.这是属性:

private ObservableAsPropertyHelper<IList<string>> _matches;
public IList<string> Matches
{
    get
    {
        return _matches.Value;
    }
}

...这是我的ViewModel构造函数中的相关内容:

var searchTerms = this.ObservableForProperty(x => x.IngredientFilter)
                   .Value()
                   .Throttle(TimeSpan.FromSeconds(0.2));
var searchResults = searchTerms.SelectMany(st => SearchIngredients(st));
_matches = searchResults.ToProperty(this, x => x.Matches);

由于相对不熟悉Reactive Extensions,并且混淆了Blend为什么关心我的app.config文件中的内容的原因,我无所适从如何实现建议的错误处理或解决此问题.我尝试清洁解决方案以及删除.suo文件.有什么建议么?

解决过程1

我已经为这个问题确定了两种解决方案.第一个根据Blend错误消息建议实现ThrownException处理:

_matches.ThrownExceptions.Subscribe(e => MessageBox.Show(e.Message));

这将在Blend启动时启动两个MessageBox窗口,显示为"在应用程序配置文件中找不到名为RecipeModelContainer的连接字符串",但至少Blend不会崩溃.

其次,事实证明,问题是由我的异步SearchIngredients方法的零延迟引起的,该方法基本上是查询Entity Framework DbContext对象以查找匹配项的.我await Task.Delay(0)只是为了伪造searchTerms.SelectMany的异步方法而添加(如果该方法不是异步的,则应用程序挂起).我仍然不清楚这与app.config文件有什么关系,但是当我更改为非零延迟时,该问题完全消失了.

Microsoft Blend 2013 crashes immediately when I try to load a WPF solution developed in Visual Studio 2013. The (abbreviated) error message I receive is: System.Exception: An OnError occurred on an object (usually ObservableAsPropertyHelper) that would break a binding or command. To prevent this, Subscribe to the ThrownExceptions property of your objects ---> System.InvalidOperationException: No connection string named 'RecipeModelContainer' could be found in the application config file.

The application builds fine and executes when I run it. Before this particular issue started, Blend consistently showed the missing connection string message; this message originated when I set the DataContext in my code-behind, and has persisted ever since I transferred the DataContext reference to my XAML. In any case, the connection string is most definitely there.

I am experimenting with ReactiveUI to develop a user control, and am using an ObservableAsPropertyHelper property. Here is the property:

private ObservableAsPropertyHelper<IList<string>> _matches;
public IList<string> Matches
{
    get
    {
        return _matches.Value;
    }
}

... And here is the related content in my ViewModel constructor:

var searchTerms = this.ObservableForProperty(x => x.IngredientFilter)
                   .Value()
                   .Throttle(TimeSpan.FromSeconds(0.2));
var searchResults = searchTerms.SelectMany(st => SearchIngredients(st));
_matches = searchResults.ToProperty(this, x => x.Matches);

Being relatively unfamiliar with Reactive Extensions and confused why Blend cares what is in my app.config file, I'm at a loss how to implement the recommended error handling or otherwise manage this problem. I have tried cleaning my solution as well as deleting the .suo files. Any suggestions?

Solutions1

I have identified two solutions for this problem. The first implements the ThrownException handling per the Blend error message recommendation:

_matches.ThrownExceptions.Subscribe(e => MessageBox.Show(e.Message));

This launches two MessageBox windows on Blend startup that read "No connection string named RecipeModelContainer could be found in the application config file", but at least Blend doesn't crash.

Second, it turns out that the issue resulted from a zero delay in my async SearchIngredients method, which basically queries an Entity Framework DbContext object for matches. I had added await Task.Delay(0) simply to fake an async method for searchTerms.SelectMany (the application hangs if the method is not async). It's still not clear to me what this has to do with the app.config file, but the issue disappears altogether when I change to a non-zero delay.

转载于:https://stackoverflow.com/questions/23712238/blend-crash-with-observableaspropertyhelper

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入