简体   繁体   English

如何将 CommunityToolkit.Mvvm 中的源代码生成器用于 .NET 框架 4.7.2 WPF 应用程序

[英]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:

  1. Right click the packages.config-File.右键单击 packages.config-File。
  2. Choose "Migrate packages.config to PackageReference..."选择“将 packages.config 迁移到 PackageReference ...”

Also, you need to set the LangVersion to a minimum of 8.0此外,您需要将 LangVersion 设置为至少 8.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 CommunityToolkit.Mvvm 调用事件 - How to call events using CommunityToolkit.Mvvm 显示 ObservableGroupedCollection 的正确方法<string, telement>使用 Wpf .NET 6 和 CommunityToolkit.Mvvm 包</string,> - Proper way of displaying an ObservableGroupedCollection<string, TElement> using Wpf .NET 6 and the CommunityToolkit.Mvvm Package 如何在 C# 中使用 Community - How do I bind an object that is periodically modified to a treeview in C# WPF using the CommunityToolkit.MVVM? CommunityToolkit.MVVM 的依赖注入如何工作? - How does the dependency injection with the CommunityToolkit.MVVM work? WinUI 3 CommunityToolkit Datagrid 在使用 CommunityToolkit.Mvvm 时显示来自两个模型的数据 - WinUI 3 CommunityToolkit Datagrid displaying data from two models while using CommunityToolkit.Mvvm C# CommunityToolkit.Mvvm ObservableProperty 列表 - C# CommunityToolkit.Mvvm ObservableProperty on a list 使用 CommunityToolkit.Mvvm 处理可观察对象属性 - Handling observable object properties with CommunityToolkit.Mvvm 如何在应用程序的框架中使用视图? (WPF MVVM) - How can I use a view from a framework in an application? (WPF MVVM) 无法在使用 CommunityToolkit.Mvvm 的视图模型中使用 ICommand 属性 - Can't use ICommand attribute in view model using CommunityToolkit.Mvvm 有没有办法在 .net framework 4 中使用源生成器? - Is there a way to use source generators with .net framework 4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM