bre*_*usn 4 .net c# wpf nuget sourcegenerators
我最近测试了 WPF UI 库 ( https://wpfui.lepo.co/ )。我创建了一个示例项目,该项目针对 .NET 6.0。示例项目包含一些基本的模型和视图模型,在这些文件中我发现使用[ObservableProperty]属性声明的属性。我真的很喜欢它如何减少简单属性所需的代码量,因此我想将其用于面向 .NET Framework 4.7.2 的现有项目。
但我不知道如何或是否可能。我在网上找到的现有信息非常令人困惑,但这个问题的公认答案听起来是可能的:Roslyn Source Generator not generated any source in a .net Framework 4.7.2
我尝试了以下操作,但应用程序无法构建:
using CommunityToolkit.Mvvm.ComponentModel;
namespace MatlogUtility
{
public partial class HeatListEntry : ObservableObject
{
[ObservableProperty]
private int? heatListId;
}
}
Run Code Online (Sandbox Code Playgroud)
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading.Tasks;
using System.Windows;
using MatlogUtility.Models;
namespace MatlogUtility
{
public static class SqlQueries
{
public static List<HeatListEntry> GetHeatList()
{
List<HeatListEntry> heatList = new List<HeatListEntry>();
string queryString = "SELECT a as heatListId FROM someTable;";
using (SqlConnection connection = new SqlConnection(Globals.ConnectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
var reader = command.ExecuteReader();
try
{
while (reader.Read())
{
heatList.Add(new HeatListEntry
{
HeatListId = reader["heatListId"] == DBNull.Value ? null : (int?)reader["heatListId"]
});
}
}
finally
{
// Always call Close when done reading.
reader.Close();
}
}
return heatList;
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误列表显示'HeatListEntry' does not contain a definition for HeatListId'
我还尝试安装一堆与“Roslyn”相关的 NuGet 包,例如 Microsoft.CodeAnalysis.CSharp 和 Microsoft.CSharp,但它仍然不起作用。
我想做的事情可能吗?预先感谢您的任何指点!
小智 7
在 .NET 4.8 中为我使用 CommunityToolkit.Mvvm 8.0.0:
另外,您需要将 LangVersion 设置为至少 8.0
小智 5
我也遇到了同样的问题。MVVM 源生成器似乎不支持较旧的 .net 框架(直到 4.8)。我将项目框架从 4.8 更改为 .net 6,并且运行良好。
您还可以尝试使用 .NET Upgrade Assistant 将 WPF 应用程序升级到 .NET 6
| 归档时间: |
|
| 查看次数: |
2518 次 |
| 最近记录: |