[英]How to use the source generators from CommunityToolkit.Mvvm for a .NET Framework 4.7.2 WPF Application
I recently tested the WPF UI library ( https://wpfui.lepo.co/ ).我最近测试了 WPF UI 库 ( https://wpfui.lepo.co/ )。 I created a sample project, and that project targeted .NET 6.0.
我创建了一个示例项目,该项目针对 .NET 6.0。 The sample project contained some basic Models and ViewModels, and in those files I found properties declared using the
[ObservableProperty]
attribute.示例项目包含一些基本模型和视图模型,在这些文件中我发现使用
[ObservableProperty]
属性声明的属性。 I really liked how that reduced the amount of code needed for simple properties, so I wanted to use that for an existing project which targets .NET Framework 4.7.2.我真的很喜欢它如何减少简单属性所需的代码量,所以我想将它用于一个以 .NET Framework 4.7.2 为目标的现有项目。
But I don't know how or if it is even possible.但我不知道如何或什至可能。 Existing information that I find online is very confusing, but the accepted answer to this question sounds like it is possible: Roslyn Source Generator not generating any source in a .net framework 4.7.2
我在网上找到的现有信息非常混乱,但这个问题的公认答案听起来是可能的: Roslyn Source Generator not generating any source in a .net framework 4.7.2
I tried the following, but the application won't build:我尝试了以下操作,但应用程序无法构建:
using CommunityToolkit.Mvvm.ComponentModel;
namespace MatlogUtility
{
public partial class HeatListEntry : ObservableObject
{
[ObservableProperty]
private int? heatListId;
}
}
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;
}
}
}
The error list shows that 'HeatListEntry' does not contain a definition for HeatListId'
错误列表显示
'HeatListEntry' does not contain a definition for HeatListId'
I also tried installing a bunch of NuGet-packages related to 'Roslyn', for example Microsoft.CodeAnalysis.CSharp and Microsoft.CSharp, but it still doesn't work.我还尝试安装一堆与“Roslyn”相关的 NuGet 包,例如 Microsoft.CodeAnalysis.CSharp 和 Microsoft.CSharp,但它仍然不起作用。
Is what I am trying to do even possible?我正在尝试做的事情有可能吗? Thanks in advance, any pointers are appreciated!
在此先感谢,任何指针表示赞赏!
I also got into the same issue.我也遇到了同样的问题。 It seems MVVM Source Generators don't support older .net frameworks (till 4.8).
似乎 MVVM 源生成器不支持较旧的 .net 框架(直到 4.8)。 I changed my project framework from 4.8 to .net 6 and it worked fine.
我将我的项目框架从 4.8 更改为 .net 6,它运行良好。
You can also try, Upgrade a WPF App to .NET 6 with the .NET Upgrade Assistant您也可以尝试使用 .NET 升级助手将 WPF 应用程序升级到 .NET 6
Worked with CommunityToolkit.Mvvm 8.0.0 for me in .NET 4.8:在 .NET 4.8 中为我使用 CommunityToolkit.Mvvm 8.0.0:
Also, you need to set the LangVersion to a minimum of 8.0此外,您需要将 LangVersion 设置为至少 8.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.