【异常报告】自定义窗口 DataGrid控件绑定异常

异常报告 · 1584 次浏览
SilenceDev 创建于 2022-03-27 17:05

版本:1.30.18

模块:自定义窗口

Xaml代码:

<Grid Margin="10">
    <StackPanel Margin="15 20">
        <DataGrid Name="DG1" ItemsSource="{Binding [GridItemsSource], Mode=OneWay}" AutoGenerateColumns="False" IsReadOnly="True" >
            <DataGrid.Columns>
                <DataGridCheckBoxColumn Header="Current?"  Binding="{Binding IsCurrent}"/>
                <DataGridTextColumn Header="Name"  Binding="{Binding Name}"/>
                <DataGridTextColumn Header="Author" Binding="{Binding Author}" />
                <DataGridTextColumn Header="Last Commit Time" Binding="{Binding LastCommitTime}"/>
                <DataGridTextColumn Header="Last Commit Message" Binding="{Binding LastCommitMessage}"/>
            </DataGrid.Columns>
        </DataGrid>     
          <Button Margin="10" qk:Att.Action="close:result" Style="{StaticResource ButtonPrimary}" Width="50">
        关闭
          </Button>
    </StackPanel>
  </Grid>

辅助C#代码:

using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Collections.Generic;
using MessageBox = System.Windows.Forms.MessageBox;
using Newtonsoft.Json;
using Quicker.Public;

public static void OnWindowCreated(Window win, IDictionary<string, object> dataContext,
    ICustomWindowContext winContext
    ){
    var definition = new []{
        new {
            Name = string.Empty,
            Author = string.Empty,
            LastCommitTime = string.Empty,
            LastCommitMessage = string.Empty,
            IsCurrent = false,
    }};

    dataContext["GridItemsSource"] = JsonConvert.DeserializeAnonymousType((string)dataContext["GridItemsSourceJson"], definition);
}

"A TwoWay or OneWayToSource" binding cannot work on the readonly property 'IsCurrent'

InvalidOperationException
   at MS.Internal.Data.PropertyPathWorker.CheckReadOnly(Object item, Object info)
   at MS.Internal.Data.PropertyPathWorker.ReplaceItem(Int32 k, Object newO, Object parent)
   at MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(Int32 k, ICollectionView collectionView, Object newValue, Boolean isASubPropertyChange)
   at MS.Internal.Data.ClrBindingWorker.AttachDataItem()
   at System.Windows.Data.BindingExpression.Activate(Object item)
   at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
   at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
   at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
   at MS.Internal.Data.DataBindEngine.Run(Object arg)
   at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)


回复内容
CL 2022-03-27 18:47
#1

这个方式没有使用过,不过DeserializeAnonymousType 得到的是单个对象,不是一个列表吧?

datagrid 应该绑定到一个列表对象上。

c# 和自定义窗口建议先在VS里调试好没有问题了再搬到Quicker里运行,这个在quicker里比较难调试😂

SilenceDev 2022-03-28 16:54 :

可以是列表。

核心点是没有捕捉WPF解析异常,导致Quciker进程崩溃,动作组合都没来得及保存就退出了

SilenceDev 2022-03-28 17:44
#2

看调用堆栈是框架起了一个Task去解析这个WPF语法树,然后发生错误。不确定你的编译过程是放在主线程还是开了一个额外的线程,要看你具体的实现才能修复好。有几种方法:

1. 尝试直接捕捉这个异常,并在quicker scope中处理

2. 起一个新的AppDomain去运行这类带编译的东西,然后在新appDomain中处理异常,做到隔离,但不确定这种方式会不会导致应用崩溃,需要做测试。

CL 2022-03-28 18:27 :

感谢! AppDomain我还没试过,等试试看。 捕获异常目前是就是那个统一的弹框报错了,不过没法判断是自定义c#里的代码异常,处理的不是很理想。

回复主贴