【问题标题】:System.StackOverflowException in windows dllWindows dll 中的 System.StackOverflowException
【发布时间】:2017-07-26 18:13:15
【问题描述】:
public static void AlphabeticPasting(object sender, DataObjectPastingEventArgs e)
{
    if (e.DataObject.GetDataPresent(typeof(string)))
    {
        var text = e.DataObject.GetData(typeof(string)) as string;
        if (!Regex.IsMatch(text, (Properties.Resources.Alpha)))
        {
            //replace and recopy
            var trimmed = Regex.Replace(text,Properties.Resources.AlphaPasting, string.Empty);
            e.CancelCommand();
            Clipboard.SetData(DataFormats.Text, trimmed);
            ApplicationCommands.Paste.Execute(trimmed, e.Source as FrameworkElement);

        }
    }
}

我想限制数字部分粘贴到文本框中, 我在文本框的 DataObject.Pasting 属性中调用此方法,Properties.Resources.Alphabet 和 Properties.Resources.AlphabetPasting 是正则表达式部分,我在资源文件中提到了正则表达式方程。

Alpha = ^[[A-Za-z-~`!@#$%^&*()_+=|{}':;.,<>/\\]?]*$ and AlphaPsting = [^[A-Za-z-~`!@#$%^&*()_+=|{}':;.,<>/\\]?]+

我只需要从资源文件中获取正则表达式。

如果我尝试跳过文本框中的任何内容,我会收到 System.StackOverFlowException。请帮我解决这个问题。

【问题讨论】:

  • 如果您正在处理AlphabeticPasting 的事件,是不是在您执行ApplicationCommand.Paste.Execute 时再次调用它?这可能会导致它...

标签: c# regex wpf


【解决方案1】:

我似乎调用了整个方法作为粘贴事件的结果。如果不是这种情况,那么请忽略此答案。 ApplicationCommands.Paste.Execute() 将触发事件,将您的代码置于无限循环中。

不要使用 ApplicationCommands.Paste.Execute(),而是尝试将 sender 对象转换为 TextBox,然后相应地设置 Text 属性。要使其工作,您可能需要跟踪更多信息(光标位置、长度等),但您不应陷入循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多