Avalonia Diagnostics报错解决方法总结

问题描述

程序启动运行不报错,但开发时,只要查看.axaml文件时,Avalonia Diagnostics就报错,导致无法查看实时预览

原因分析

Object reference not set to an instance of an object

代码中使用了相对路径,设计模式预览时无法解析相对路径从而导致找不到文件,最后呈现的则是未将对象引用至实例化。比如你注入ORM时,对path变量直接赋值appsettings.json,从而使用了相对路径

"Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object."

An item with the same key has already been added.

使用了Nuget包Material.Styles.Controls中的SnackbarHost控件,这个控件有个bug,它的属性HostName要求是唯一值,而你每修改一次,预览加载时会默认赋值时会报错,这个时候你会发现你随便改什么它都会报这个错,但是你把这个控件的属性HostName重新赋值,报错会停止,但继续修改任意地方又会报错

[Error] 1168 UpdateXamlResult error
System.Xaml.XamlException: An item with the same key has already been added. Key: Root

解决方法

对于未将对象引用至实例化

这里提供一个案例,只要活用Design.IsDesignMode很多报错都可以规避

public static AppConfig ImportConfig(string filePath)
{
    try
    {
        if(Design.IsDesignMode)
        {
            filePath = AppDomain.CurrentDomain.BaseDirectory + "\\appconfig.json";
        }
        string json = File.ReadAllText(filePath);

        return JsonSerializer.Deserialize<AppConfig>(json);
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error importing config: {ex.Message}");
        return null;
    }
}

这样做负责实时预览的后台线程会进入Design.IsDesignMode里,从而使用了绝对路径,避免报错;而当启动运行时,则不会进入Design.IsDesignMode中。

对于使用了SnackbarHost

在你完全设计好页面前,先把它注释掉,目前没找到很好的解决方法

<!--设计页面时请注释-->
<!--<controls:SnackbarHost HostName="Root" TemplateApplied="TemplatedControl_OnTemplateApplied">-->
### Avalonia 实现导出 Excel 功能的方法Avalonia 应用程序中实现导出 Excel 的功能可以通过多种方式完成。通常情况下,开发者会选择第三方库(如 EPPlus 或 ClosedXML)来简化这一过程。以下是关于如何在 Avalonia 项目中实现导出 Excel 功能的具体方法。 #### 使用 ClosedXML 进行 Excel 文件创建 ClosedXML 是一个流行的 .NET 库,它允许开发者轻松地创建、修改和保存 Excel 文档而无需安装 Microsoft Office。以下是一个简单的示例代码片段: ```csharp using System.Collections.Generic; using System.IO; using ClosedXML.Excel; public void ExportToExcel(List<Dictionary<string, object>> data, string filePath) { using (var workbook = new XLWorkbook()) { var worksheet = workbook.Worksheets.Add("Sheet1"); // 添加表头 int colIndex = 1; foreach (var key in data[0].Keys) { worksheet.Cell(1, colIndex).Value = key; colIndex++; } // 填充数据 int rowIndex = 2; // 数据从第二行开始填充 foreach (var row in data) { colIndex = 1; foreach (var value in row.Values) { worksheet.Cell(rowIndex, colIndex).Value = value.ToString(); colIndex++; } rowIndex++; } // 将工作簿保存到指定路径 workbook.SaveAs(filePath); } } ``` 上述代码展示了如何通过 `ClosedXML` 创建一个新的 Excel 工作簿并写入数据[^4]。此函数接受两个参数:一个是包含字典列表的数据结构,另一个是要保存的文件路径。 #### 配合 Avalonia UI 完成导出逻辑 为了使该功能集成到 Avalonia 应用程序中,可以将按钮绑定到命令或事件处理程序,在点击时触发导出操作。例如: ```xml <Button Content="Export to Excel" Command="{Binding ExportCommand}" /> ``` 对应的 ViewModel 可能如下所示: ```csharp using CommunityToolkit.Mvvm.Input; using System.Collections.Generic; using System.IO; public class MainViewModel : ObservableObject { public IRelayCommand ExportCommand { get; } private List<Dictionary<string, object>> _data; public MainViewModel() { _data = new List<Dictionary<string, object>> { new Dictionary<string, object> { { "Name", "Alice" }, { "Age", 30 } }, new Dictionary<string, object> { { "Name", "Bob" }, { "Age", 25 } } }; ExportCommand = new RelayCommand(() => { SaveFileDialog dialog = new SaveFileDialog { DefaultExt = ".xlsx", Filter = "Excel Files (*.xlsx)|*.xlsx" }; if (dialog.ShowDialog() is string fileName && !string.IsNullOrWhiteSpace(fileName)) { ExportToExcel(_data, fileName); } }); } private void ExportToExcel(List<Dictionary<string, object>> data, string filePath) { using (var workbook = new XLWorkbook()) { var worksheet = workbook.Worksheets.Add("Sheet1"); int colIndex = 1; foreach (var key in data[0].Keys) { worksheet.Cell(1, colIndex++).Value = key; } int rowIndex = 2; foreach (var row in data) { colIndex = 1; foreach (var value in row.Values) { worksheet.Cell(rowIndex, colIndex++).Value = value?.ToString(); } rowIndex++; } workbook.SaveAs(filePath); } } } ``` 这段代码定义了一个带有导出功能的 ViewModel,并使用了社区工具包中的 `IRelayCommand` 来管理命令执行逻辑[^2]。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值