AvaloniaUI / AvaloniaVS

Visual Studio Extension for Avalonia
MIT License
424 stars 82 forks source link

Autocomplete of binding properties doesn't work in Visual Studio 2022 mostly #463

Closed ProviceUnify closed 6 months ago

ProviceUnify commented 6 months ago

Describe the bug

Autocomplete Ctrl + Space for < ...={Binding ...} .../> doesn't work in Visual Studio 2022 mostly. It works in simple solutions, in some other solutions but it is unable yo use this feature in some other solutions. image

Code of AlgorithmsViewModel.cs:

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace Namespace.ViewModels.Pages
{
    public partial class AlgorithmsViewModel : ViewModelBase
    {
        [ObservableProperty]
        private string _pageName = "Algorithm View Model!";

        [RelayCommand]
        private void ButtonBindingTest()
        {
            PageName = "Algorithm View Model Changed !111!";
        }
    }
}

Code of AlgorithmsView.cs

<UserControl xmlns=         "https://github.com/avaloniaui"
             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"
             mc:Ignorable=  "d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class=       "Namespace.Views.Pages.AlgorithmsView"

             Tag=           "ALG"

             xmlns:vm=      "using:Namespace.ViewModels.Pages"

             x:DataType=    "vm:AlgorithmsViewModel">
  <Design.DataContext>
    <vm:AlgorithmsViewModel/>
  </Design.DataContext>
  <StackPanel>
    <TextBlock>Algorithms View!</TextBlock>
    <TextBlock Text="{Binding PageName}"/> <!-- HERE AUTOCOMPLETE DOESN'T APPEAR -->
    <Button Command="{Binding ButtonBindingTestCommand}">Press me!</Button> <!-- HERE AUTOCOMPLETE DOESN'T APPEAR -->
  </StackPanel>
</UserControl>

Code of MainView.cs (part)

<UserControl xmlns=         "https://github.com/avaloniaui"
             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:vm=      "using:Namespace.ViewModels"
             mc:Ignorable=  "d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class=       "Namespace.Views.MainView"

             xmlns:cnvrt=   "using:Namespace.Resources.Converters"

             xmlns:ui=      "using:FluentAvalonia.UI.Controls"
             xmlns:fi=      "using:FluentIcons.Avalonia"

             x:DataType="vm:MainViewModel">

  <Design.DataContext>
    <vm:MainViewModel/>
  </Design.DataContext>
...
    <Grid ColumnDefinitions="* *"
          RowDefinitions="Auto"
          VerticalAlignment="Center">
      <Border>

        <StackPanel Grid.Column="0"
                    Orientation="Horizontal">

          <Image Source="/Assets/Logo.png"
                 Height="40"
                 Margin="12 4"
                 HorizontalAlignment="Left"
                 VerticalAlignment="Center" />
          <StackPanel Margin="0,0,0,0"
                      VerticalAlignment="Center"
                      Orientation="Vertical">
            <TextBlock FontSize="20"
                       FontWeight="Bold"
                       Foreground="DarkGray"
                       Text="Text" />
            <TextBlock FontSize="11"
                       FontWeight="Light"
                       Text="{Binding GuiLocalization[TitleDescription], Converter={StaticResource l18n_mnemonica}}"/> <!-- HERE AUTOCOMPLETE WORKS -->
          </StackPanel>
...

Code of MainViewModel.cs

using System.Collections.Generic;

using CommunityToolkit.Mvvm.ComponentModel;

namespace Namespace.ViewModels;

public partial class MainViewModel : ViewModelBase
{
    [ObservableProperty]
    private static Dictionary<string, string> _guiLocalization = new()
        {
            { "TitleDescription", "{120.TITLE_DESCRIPTION}" }
        };
}

To Reproduce

  1. Create Avalonia template project
  2. Create structure of project like in Description section above
  3. Add property (public type name => "value" or using [ObservableProperty] - not matter)
  4. Go to connected ...ViewModel.cs and try to use Binding autocomplete Ctrl + Space

Expected behavior

See variant of user defined props bottom: image

BUT

Got empty list with default entries (here i have defined PageName prop in ViewModel also): image

Avalonia version

11.0.10

OS

Windows

Additional context

I already used:

  1. Building project/solution - i have another large project where autocomplete is working
  2. Clearing Intellisense cache by extension
  3. Recreating "cursed" files - seems like working but until 1 new prop
  4. Creating new project and transfering files and code to it

I have:

  1. Set x:DataType in .axaml header
  2. Set Design.DataContext in .axaml header
  3. Double-, Triple- and Quadruple checked all paths, namespaces etc. It works sometimes but stops at randomly without any chanes so paths and other params are true
ProviceUnify commented 6 months ago

Important: solution can be buit and works fine - Bindings work. Doesn't work autocomplete exactly

workgroupengineering commented 6 months ago

Try adding the following target to your YourApp.Desktop.csproj

https://github.com/AvaloniaUI/Avalonia/blob/fde8a65157cd9242c1cc0f70f9c9f2705cf56757/packages/Avalonia/AvaloniaBuildTasks.targets#L238-L254

Unload and Lood YourApp.Desktop.csproj or restart VS

Build solution

ProviceUnify commented 6 months ago

Try adding the following target to your YourApp.Desktop.csproj

https://github.com/AvaloniaUI/Avalonia/blob/fde8a65157cd9242c1cc0f70f9c9f2705cf56757/packages/Avalonia/AvaloniaBuildTasks.targets#L238-L254

Unload and Lood YourApp.Desktop.csproj or restart VS

Build solution

Doesn't give any affect. Still no autocomplete. Tried add to YourApp.csproj too for sure

ProviceUnify commented 6 months ago

Checked Same code on Rider Trial - works perfectly (but i cant use Rider for dev anyway). Problem is in VS extension exactly

ProviceUnify commented 6 months ago

Well. Found out reason of this bug and how to fix it. This code will have available autocomplete for bindings:

<UserControl
...
x:DataType="vm:IdentificationViewModel">
  <StackPanel>
    <TextBlock>Identification View!</TextBlock>
    <TextBlock Text="{Binding PageName}"/> <!-- VS will suggest you "PageName" prop from VM here -->
    <Button Command="{Binding ButtonBindingTestCommand}">Press me!</Button>
  </StackPanel>
</UserControl>

This one WILL NOT have autocomplete for bindings:

<UserControl
...
x:DataType= "vm:IdentificationViewModel"> <!-- watch here: space(s) (or tab(s)) between x:DataType= AND "..." (value) -->
  <StackPanel>
    <TextBlock>Identification View!</TextBlock>
    <TextBlock Text="{Binding }"/> <!-- VS will not suggest you any of your props from VM here -->
    <Button Command="{Binding }">Press me!</Button>
  </StackPanel>
</UserControl>

Reason was a Spaces or Tabs between x:DataType= attribute and its value "...". For proper work it should be seamlessly x:DataType="..."

timunie commented 6 months ago

Reason was a Spaces or Tabs between x:DataType= attribute and its value "...". For proper work it should be seamlessly x:DataType="..."

don't know if we can have an XAML warning for this. /cc @maxkatz6

maxkatz6 commented 6 months ago

No, whitespaces should be supported there, as it's a valid XML.