我的目标是创建一个具有新依赖项属性SearchText的自定义TextBlock控件.此属性将包含正则表达式.将使用自定义样式(另一个DP)突出显示TextBlock文本中所有出现的此正则表达式.
我当前的实现涉及清除TextBlock的InlineCollection中的所有Inline对象.然后我使用未突出显示的文本运行TextBlock并运行应用了样式的突出显示文本(此方法不支持直接向TextBlock添加内联,而是必须使用TextBlock.TextProperty).
工作得很好,但有时我在尝试清除Inlines时遇到一个奇怪的异常:InvalidOperationException:"此时无法修改此节点的逻辑子节点,因为正在进行树步行."
这个问题似乎是与此一个.我正在修改TextChanged函数中的内联,但我使用一个标志来避免无限递归编辑.
有关如何构建此自定义控件的任何想法?有一个更好的方法吗?我该如何解决这个异常?
谢谢!
wpf textblock custom-controls invalidoperationexception inlines
我们遇到了这个例外的多个问题,但是我找不到关于问题真正原因的技术文档,这个错误的所有可能来源以及我们应该避免什么来避免异常.
我看过以下内容:
暂停调度程序处理以避免在更新可视树时出现重入问题.
但我不确定"更新可视树"的含义以及导致消息被发送到Dispatcher并重现问题的原因.
以下示例代码重现了该问题:
XAML
<Window x:Class="SuspendedPOF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="350"
Width="525">
<StackPanel>
<Button Content="1" x:Name="Button1" IsVisibleChanged="Button1_OnIsVisibleChanged" />
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
C#代码
using System.Windows;
namespace SuspendedPOF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button1_OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
MessageBox.Show("Hello");
}
}
}
Run Code Online (Sandbox Code Playgroud)
MessageBox调用不是唯一一个触发此异常的函数,像Focus这样的东西有时也会出现问题.
任何帮助都会很棒.
我制作了一个程序,我想手动更新数据网格视图. - 我有一个通过清除DGV然后重新插入数据来刷新DGV的方法. - 使用设计器,我为DGV的CellEndEdit创建了一个事件处理程序.在事件处理程序内部,数据会更新并调用DGV的自定义刷新方法.
在运行程序时,每当我开始编辑单元格并通过选择另一个单元格结束它时,就会抛出异常:
InvalidOperationException操作无效,因为它导致对SetCurrentCellAddressCore函数的可重入调用.
Visual C#的调试器标记清除数据的行:datagridview1.Rows.Clear();
如果您想重现问题,请使用visual c#创建一个新的Windows窗体项目,在窗体上放置一个DataGridView对象,并粘贴Form1.cs的以下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Error___DataGridView_Updating___Cell_endedit
{
public partial class Form1 : Form
{
// Objects
DataTable dt;
DataColumn colID;
DataColumn colName;
DataColumn colInfo;
// Constructor
public Form1()
{
InitializeComponent();
Initialize_dt();
InsertSampleData_dt();
Initialize_dataGridView1();
}
// Methods
private void Initialize_dt()
{
dt = new DataTable();
// Building Columns
// ID
colID = new DataColumn();
colID.ColumnName = "ID";
colID.DataType …Run Code Online (Sandbox Code Playgroud) 我知道并使用xxx.Dispatcher.Invoke()方法来获取后台线程来操作GUI元素.我想我正在碰到类似的东西,但略有不同,我想要一个长时间运行的后台任务来构建一个对象树,并在完成时将其交给GUI进行显示.
尝试这样做会导致InvalidOperationException,"由于调用线程无法访问此对象,因为其他线程拥有它." 奇怪的是,简单类型不会发生这种情况.
下面是一些示例代码,演示抛出异常的简单案例.知道如何解决这个问题吗?我很确定问题是后台线程拥有工厂构造的对象,并且前台GUI线程不能取得所有权,尽管它适用于更简单的系统类型.
private void button1_Click(object sender, RoutedEventArgs e)
{
// These two objects are created on the GUI thread
String abc = "ABC";
Paragraph p = new Paragraph();
BackgroundWorker bgw = new BackgroundWorker();
// These two variables are place holders to give scoping access
String def = null;
Run r = null;
// Initialize the place holders with objects created on the background thread
bgw.DoWork += (s1,e2) =>
{
def = "DEF";
r = new Run("blah");
};
// …Run Code Online (Sandbox Code Playgroud) c# wpf dispatcher invalidoperationexception backgroundworker
我有一个好老的InvalidOperationException被抛出标准信息
收集被修改; 枚举操作可能无法执行.
问题是,枚举器不会自行修改,例如:
private TRoute _copyRoute(TRoute route)
{
TRoute tempRoute = new TRoute();
tempRoute.Initialize(route.Resource);
foreach (TVisit visit in route)
{
tempRoute.Add(visit);
}
tempRoute.EndLocation = route.EndLocation;
return tempRoute;
}
Run Code Online (Sandbox Code Playgroud)
我的代码是多线程的(本例中大约12-15个线程),每个线程应该在自己的路由深度克隆上工作.显然某些地方出了问题,但是,我的问题是如何通过如此多的线程来追踪这一点?减少数量可以显着阻止问题的显现.
在这种情况下,我的路由实例是一个IList,所以我可以玩添加到界面的东西.它下面有它自己的List实现.
编辑
只是添加,我可以ToArray()或ToList()这可能忽略这里的问题,但我真的不想那样做,我想找到原因.例如:
如果我将其更改为以下内容:
private TRoute _copyRoute(TRoute route)
{
TRoute tempRoute = new TRoute();
tempRoute.Initialize(route.Resource);
foreach (TVisit visit in route.ToList())
{
tempRoute.Add(visit);
}
tempRoute.EndLocation = route.EndLocation;
return tempRoute;
}
Run Code Online (Sandbox Code Playgroud)
然后我在这个断言上失败了,因为在ToList()之前发生了一次机会......我需要尝试找出发生变化的地方
TRoute tempRoute1 = CopyRoute(route1);
TRoute tempRoute2 = CopyRoute(route2);
Debug.Assert(tempRoute1.Count == route1.Count);
Run Code Online (Sandbox Code Playgroud) 我创建了一个实现IEnumerable(T)和自定义IEnumerator(T)的自定义集合.
我还在自定义集合中添加了一个Add()方法,如下所示:
public void Add(T item)
{
T[] tempArray = new T[_array.Length + 1];
for (int i = 0; i < _array.Length; i++)
{
tempArray[i] = _array[i];
}
tempArray[_array.Length] = item;
_array = tempArray;
tempArray = null;
}
Run Code Online (Sandbox Code Playgroud)
该实现基于此示例http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx.
当我使用我的数组进行foreach循环时,我想阻止集合修改(比如在循环中调用Add())并抛出一个新的InvalidOperationException.我怎么能这样做?
合同类型HelloIndigo.Service未归属于ServiceContractAttribute.为了定义有效的合同,必须使用ServiceContractAttribute来声明指定的类型(合同接口或服务类).
我构建了一个库类,并在控制台应用程序中引用了该类.
图书馆类:
namespace HelloIndigo
{
public class Service : IHelloIndigoService
{
public string HelloIndigo()
{
return "Hello Indigo";
}
}
[ServiceContract(Namespace = "http://www.thatindigogirl.com/samples/2006/06")]
interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo();
}
}
Run Code Online (Sandbox Code Playgroud)
控制台应用程序:
namespace Host
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.Service),
new Uri("http://localhost:8000/HelloIndigo")))
{
host.AddServiceEndpoint(typeof(HelloIndigo.Service),
new BasicHttpBinding(),"Service");
host.Open();
Console.WriteLine("Press enter to terminate the host service");
Console.ReadLine();
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我宣布了一个字段:
WriteableBitmap colorBitmap;
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个简单的线程来执行某些操作
private void doSomething()
{
// ... bla bla bla
colorBitmap = new WriteableBitmap(/* parameters */);
myImage.Source = colorBitmap; // error here:S
}
Run Code Online (Sandbox Code Playgroud)
在Windows_Loaded事件中,我声明并启动了一个新线程:
private void window_Loaded(object sender, RoutedEventArgs e)
{
Thread th = new Thread(new ThreadStart(doSomething));
th.Start();
}
Run Code Online (Sandbox Code Playgroud)
问题是我无法改变myImage的来源.我有一个错误,如:
InvalidOperationException未处理调用线程无法访问此对象,因为另一个线程拥有它.
我试图使用Dispatcher.Invoke,但它没有帮助...
Application.Current.Dispatcher.Invoke((Action)delegate
{
myImage.Source = colorBitmap;
});
Run Code Online (Sandbox Code Playgroud)
我正在寻找一些答案,但从来没有像我一样找到这个案子.any1帮助我理解如何解决这样的问题(我最近遇到了同样的问题,但我无法调用该方法,因为其他线程拥有它).
我想在DataGrid中显示一个文件的内容.(该文件包含超过200,000行)
使用数据显示网格很快.
但是,当我使用滚动条(向下滚动)时,我有以下异常:
System.InvalidOperationException:
{"An ItemsControl is inconsistent with its items source.\n See the inner exception for more information."}
Run Code Online (Sandbox Code Playgroud)
的InnerException:
Information for developers (use Text Visualizer to read this):
This exception was thrown because the generator for control 'System.Windows.Controls.DataGrid Items.Count:0' with name '(unnamed)' has received sequence of CollectionChanged events that do not agree with the current state of the Items collection. The following differences were detected:
Accumulated count 0 is different from actual count 200000. [Accumulated count is (Count at …Run Code Online (Sandbox Code Playgroud) 我经常发现它没有真正指定究竟是什么样的集合导致了这种类型的异常.这是真的还是应该是显而易见的?也许我只是不明白如何正确解释异常消息..
我特别想知道这个.它指的是什么系列?
事件委托的参数只是(对象发送者),并且引发的事件传递null参数.虽然引发事件的类本身继承了一个列表:
public class TimeSerie : List<BarData>
Run Code Online (Sandbox Code Playgroud)
这里是否清楚"集合"是指引发事件的对象,还是它可以是另一个对象?可以这么说,一个动态改变的方法的事件处理程序的集合?或者会创建一个不同的例外?
************** Exception Text **************
System.InvalidOperationException:
Collection was modified; enumeration operation may not execute.
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
at SomeNameSpace.SomeUserControl.InvokeOnUpdateHistory(Object sender) in D:\SomePath\SomeUserControl.cs:line 5179
at OtherNameSpace.OtherClass.TimeSerie.HistoryUpdateEventHandler.Invoke(Object sender)
Run Code Online (Sandbox Code Playgroud)
UserControl中发生异常:
public class SomeUserControl
private void InvokeOnUpdate(object sender)
{
this.Invoke(new GenericInvoker(Method)); // << Exception here!
}
private void Method() {...}
Run Code Online (Sandbox Code Playgroud)
编辑: 添加了一些代码.有点简化,但认为它包括相关位.
private void Method()
{
if (this.instrument == null) return;
UnRegisterTimeSerieHandlers(this.ts); …Run Code Online (Sandbox Code Playgroud)