Visual Studio Intellisense在WPF中绑定时,不建议源生成的属性

发布于 2025-02-13 15:25:56 字数 2947 浏览 2 评论 0原文

我没有获得有关MVVM社区工具包生成的属性的Intellisense建议。有办法解决这个问题吗?如果我无法获得建议,那么使用[observableProperty]甚至不值得IMO。下面的示例。

ViewModel具有一个生成的属性和一个手工制作属性:

public partial class SimplisticViewModel : ObservableObject
{
    [ObservableProperty]
    private int sourceGeneratedProperty;

    private float handmadeProperty;
    public float HandmadeProperty
    {
        get => handmadeProperty;
        set => SetProperty(ref handmadeProperty, value);
    }
}

XAML:

<Window x:Class="WPFEFTest.TestWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFEFTest"
        mc:Ignorable="d"
        Title="TestWindow" Height="450" Width="800">

    <Window.DataContext>
        <local:SimplisticViewModel/>
    </Window.DataContext>
    
    <StackPanel>
        <TextBox Text=""/>
    </StackPanel>
</Window>

为手工制作属性设置绑定时,建议工作:

“手工属性绑定”

在设置源生成属性的绑定时,没有建议:

”源生成的属性绑定“

但是,如果我编写整个属性名称(避免错别字等),然后将光标悬停在它上(光标在屏幕截图中不可见),Intellisense将正确显示属性的类型。因此,问题可能不是Intellisense根本没有“看到”该属性。

编辑:要明确我不尝试绑定到一个字段,以下是MVVM社区的代码工具包由于observableProperty属性而生成:

public partial class SimplisticViewModel
{
    [global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "7.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public int SourceGeneratedProperty
    {
        get => sourceGeneratedProperty;
        set
        {
            if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(sourceGeneratedProperty, value))
            {
                OnPropertyChanging(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.SourceGeneratedPropertyPropertyChangingEventArgs);
                sourceGeneratedProperty = value;
                OnPropertyChanged(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.SourceGeneratedPropertyPropertyChangedEventArgs);
            }
        }
    }
}

I'm not getting intellisense suggestions for properties that were source generated by the MVVM community toolkit. Is there a way to fix this? If I can't get the suggestions to work, then using [ObservableProperty] isn't even worth it IMO. Example below.

ViewModel has one source generated property and one handmade property:

public partial class SimplisticViewModel : ObservableObject
{
    [ObservableProperty]
    private int sourceGeneratedProperty;

    private float handmadeProperty;
    public float HandmadeProperty
    {
        get => handmadeProperty;
        set => SetProperty(ref handmadeProperty, value);
    }
}

XAML:

<Window x:Class="WPFEFTest.TestWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFEFTest"
        mc:Ignorable="d"
        Title="TestWindow" Height="450" Width="800">

    <Window.DataContext>
        <local:SimplisticViewModel/>
    </Window.DataContext>
    
    <StackPanel>
        <TextBox Text=""/>
    </StackPanel>
</Window>

When setting up binding for the handmade property, the suggestions work:

handmade property binding

When setting up binding for the source generated property, there are no suggestions:

source generated property binding

However, if I write the entire property name (avoiding typos and such...) and hover my cursor over it (cursor not visible in screenshot), intellisense will correctly show the type of the property. So the problem probably isn't that intellisense doesn't "see" the property at all.

cursor hover over source generated property

EDIT: To make it clear I'm not trying to bind to a field, below is code that MVVM community toolkit generates because of the ObservableProperty attribute:

public partial class SimplisticViewModel
{
    [global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "7.1.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    public int SourceGeneratedProperty
    {
        get => sourceGeneratedProperty;
        set
        {
            if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(sourceGeneratedProperty, value))
            {
                OnPropertyChanging(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.SourceGeneratedPropertyPropertyChangingEventArgs);
                sourceGeneratedProperty = value;
                OnPropertyChanged(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.SourceGeneratedPropertyPropertyChangedEventArgs);
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给不了的爱 2025-02-20 15:25:56

也许您可以尝试设置dataContext for window ...当我删除d:dataContext哪个指向viewModel类以及dataContext <时/代码>已设置,Intellisense从ViewModel拾取了属性。

<Window x:Class="WPFEFTest.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPFEFTest"
    xmlns:viewmodels="clr-namespace:WPFEFTest.ViewModels"
    mc:Ignorable="d"
    Title="TestWindow" Height="450" Width="800" d:DataContext="{d:DesignInstance Type=viewmodels:SimplisticViewModel>

Maybe you could try setting DataContext for window... Same thing/issue you describe happens when I remove d:DataContext which points to ViewModel class and when DataContext is set, Intellisense picks up properties from ViewModel.

<Window x:Class="WPFEFTest.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WPFEFTest"
    xmlns:viewmodels="clr-namespace:WPFEFTest.ViewModels"
    mc:Ignorable="d"
    Title="TestWindow" Height="450" Width="800" d:DataContext="{d:DesignInstance Type=viewmodels:SimplisticViewModel>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文