首页 > 解决方案 > 连续操作 inkstrorkecontainer.clear() 和 addstrokes() 导致 UWP 中的 EXCEPTION

问题描述

Sink1 是一个 inkstroke 容器,它不绑定到 inkcanvas(我也尝试将其绑定到 inkcanvas 以避免异常,但仍然没用)。ink1 是 xaml 上的 inkcanvas。

 public sealed partial class wpage : Page


         {
            public wpage()
            {
                this.InitializeComponent();

                ink1.InkPresenter.StrokeContainer = inkStrokeContainer1;
                ink1.InkPresenter.StrokesCollected += InkPresenter_StrokesCollected;
                ink1.InkPresenter.InputDeviceTypes = CoreInputDeviceTypes.Pen | CoreInputDeviceTypes.Mouse;
             }

                public  void InkPresenter_StrokesCollected(InkPresenter sender, InkStrokesCollectedEventArgs args)

            {sink1.Clear();
             sink1.AddStrokes(args.Strokes);
            }


    }

above code will cause an


    #if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
                UnhandledException += (sender, e) =>
                {
                    if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();

我也试过:

public async void InkPresenter_StrokesCollected(InkPresenter sender, InkStrokesCollectedEventArgs args)

   {     await CoreApplication.MainView.Dispatcher.RunAsync(CoreDispatcherPriority.Low, new DispatchedHandler(() =>
         {    {sink1.Clear();
             sink1.AddStrokes(args.Strokes);
            });

     }

但还是没用还用炸弹

 #if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
            UnhandledException += (sender, e) =>
            {
                if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break(); .

我还尝试在 taks.run 和 Dispatcher.RunAsync 中同时运行它们,也同样是炸弹。我这样做是因为我需要用 inkstrokecontainer 类中的 save 方法序列化 isf 格式的 inkstroke,用其他方法序列化结果大小太大。

标签: cuwpuwp-xaml

解决方案


Sink1 是一个 inkstroke 容器,它不绑定到 inkcanvas

这就是问题的原因。

sink1ink1.InkPresenter.StrokeContainer不一样InkStrokeContainer,也就是说StrokeinInkCanvas不能直接加到另一个StrokeContainer上。

您可以尝试修改您的代码:

private void InkPresenter_Collect(InkPresenter sender, InkStrokesCollectedEventArgs args)
{
    sink1.Clear();
    foreach (var item in args.Strokes)
    {
        sink1.AddStroke(item.Clone());
    }
}

简单来说,Stroke屏幕上已经显示的属于ink1,不能添加到另一个InkStrokeContainer,但是可以获取它的克隆对象,克隆的对象不属于ink1,可以添加。

此致。


推荐阅读