Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix For IViewFor missing namespace #65

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@
<PackageVersion Include="Nerdbank.GitVersioning" Version="3.6.143" />
<PackageVersion Include="stylecop.analyzers" Version="1.2.0-beta.556" />
<PackageVersion Include="Roslynator.Analyzers" Version="4.12.5" />
<PackageVersion Include="ReactiveUI" Version="20.1.52" />
<PackageVersion Include="ReactiveUI" Version="20.1.63" />
<PackageVersion Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageVersion Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="$(CodeAnalysisVersion)" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(CodeAnalysisVersion)" />
<PackageVersion Include="PolySharp" Version="1.14.1" />

<PackageVersion Include="Avalonia" Version="11.1.3" />
<PackageVersion Include="Avalonia.Themes.Fluent" Version="11.1.3" />
<PackageVersion Include="Avalonia.Fonts.Inter" Version="11.1.3" />
<PackageVersion Include="Avalonia.ReactiveUI" Version="11.1.3" />
<PackageVersion Include="Avalonia.Diagnostics" Version="11.1.3" />
<PackageVersion Include="Avalonia.Desktop" Version="11.1.3" />

<PackageVersion Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageVersion Include="ReactiveUI.Maui" Version="20.1.63" />
</ItemGroup>
</Project>
20 changes: 19 additions & 1 deletion src/ReactiveUI.SourceGenerators.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,25 @@
<File Path="Directory.Packages.props" />
<File Path="stylecop.json" />
</Folder>
<Project Path="ReactiveUI.SourceGenerators.Execute.Maui\ReactiveUI.SourceGenerators.Execute.Maui.csproj" Type="Classic C#" />
<Project Path="ReactiveUI.SourceGenerators.Execute.Maui\ReactiveUI.SourceGenerators.Execute.Maui.csproj" />
<Project Path="ReactiveUI.SourceGenerators.Execute\ReactiveUI.SourceGenerators.Execute.csproj" />
<Project Path="ReactiveUI.SourceGenerators\ReactiveUI.SourceGenerators.csproj" />
<Folder Name="/TestApps/">
<Project Path="TestApps\TestWpfApplication\TestWpfApplication.csproj" />
<Project Path="D:\Projects\GitHub\ReactiveUI\ReactiveUI.SourceGenerators\src\TestApps\TestWinFormsApplication\TestWinFormsApplication.csproj" />
<Project Path="D:\Projects\GitHub\ReactiveUI\ReactiveUI.SourceGenerators\src\TestApps\TestWpfApplication\TestWpfApplication.csproj" />
<Project Path="TestApps\TestWinFormsApplication\TestWinFormsApplication.csproj" />
<Project Path="D:\Projects\GitHub\ReactiveUI\ReactiveUI.SourceGenerators\src\TestApps\TestAvaloniaApplication\TestAvaloniaApplication.csproj" />
<Project Path="TestApps\TestAvaloniaApplication\TestAvaloniaApplication.csproj" />
<Project Path="TestApps\TestAvaloniaApplication.Desktop\TestAvaloniaApplication.Desktop.csproj" />
<Project Path="TestApps\TestMauiApplication\TestMauiApplication.csproj">
<Deploy />
</Project>
</Folder>
<Properties Name="Visual Studio">
<Property Name="OpenWith" Value="17" />
</Properties>
<Project Path="D:\Projects\GitHub\ReactiveUI\ReactiveUI.SourceGenerators\src\ReactiveUI.SourceGenerators.Execute.Maui\ReactiveUI.SourceGenerators.Execute.Maui.csproj" />
<Project Path="D:\Projects\GitHub\ReactiveUI\ReactiveUI.SourceGenerators\src\ReactiveUI.SourceGenerators.Execute\ReactiveUI.SourceGenerators.Execute.csproj" />
<Project Path="D:\Projects\GitHub\ReactiveUI\ReactiveUI.SourceGenerators\src\ReactiveUI.SourceGenerators\ReactiveUI.SourceGenerators.csproj" />
</Solution>
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal static CompilationUnitSyntax GetIViewForWpfWinUiUno(IViewForInfo iViewF
writer.WriteLine("DependencyProperty.Register(");
writer.WriteLine("nameof(ViewModel),");
writer.WriteLine($"typeof({iViewForInfo.ViewModelTypeName}),");
writer.WriteLine($"typeof(IViewFor<{iViewForInfo.ViewModelTypeName}>),");
writer.WriteLine($"typeof({iViewForInfo.ClassName}),");
writer.WriteLine("new PropertyMetadata(null));");
writer.WriteLine();

Expand Down Expand Up @@ -213,6 +213,7 @@ internal static CompilationUnitSyntax GetIViewForAvalonia(IViewForInfo iViewForI
[
UsingDirective(ParseName("System")),
UsingDirective(ParseName("ReactiveUI")),
UsingDirective(ParseName("Avalonia")),
UsingDirective(ParseName("Avalonia.Controls")),
];

Expand Down Expand Up @@ -258,7 +259,7 @@ internal static CompilationUnitSyntax GetIViewForAvalonia(IViewForInfo iViewForI
writer.WriteLine($"public static readonly StyledProperty<{iViewForInfo.ViewModelTypeName}?> ViewModelProperty =");
writer.Indent++;
writer.WriteLine("AvaloniaProperty");
writer.WriteLine($".Register<IViewFor<{iViewForInfo.ViewModelTypeName}>, {iViewForInfo.ViewModelTypeName}?>(nameof(ViewModel));");
writer.WriteLine($".Register<{iViewForInfo.ClassName}, {iViewForInfo.ViewModelTypeName}?>(nameof(ViewModel));");

writer.Indent--;
writer.WriteLine("/// <summary>");
Expand Down Expand Up @@ -286,28 +287,31 @@ internal static CompilationUnitSyntax GetIViewForAvalonia(IViewForInfo iViewForI
writer.Indent--;
writer.WriteLine(Token(SyntaxKind.CloseBraceToken));
writer.WriteLine();
writer.WriteLine(@"
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);

if (change.Property == DataContextProperty)
{
if (ReferenceEquals(change.OldValue, ViewModel)
&& change.NewValue is null or TViewModel)
{
SetCurrentValue(ViewModelProperty, change.NewValue);
}
}
else if (change.Property == ViewModelProperty)
{
if (ReferenceEquals(change.OldValue, DataContext))
{
SetCurrentValue(DataContextProperty, change.NewValue);
}
}
}
");
writer.WriteLine(
$$"""

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);

if (change.Property == DataContextProperty)
{
if (ReferenceEquals(change.OldValue, ViewModel)
&& change.NewValue is null or {{iViewForInfo.ViewModelTypeName}})
{
SetCurrentValue(ViewModelProperty, change.NewValue);
}
}
else if (change.Property == ViewModelProperty)
{
if (ReferenceEquals(change.OldValue, DataContext))
{
SetCurrentValue(DataContextProperty, change.NewValue);
}
}
}

""");
writer.Indent--;
writer.WriteLine(Token(SyntaxKind.CloseBraceToken));
writer.Indent--;
Expand Down
32 changes: 32 additions & 0 deletions src/TestApps/TestAvaloniaApplication.Desktop/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System;

using Avalonia;
using Avalonia.ReactiveUI;

namespace AvaloniaApplication1.Desktop;

/// <summary>
/// Program.
/// </summary>
internal static class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);

// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace()
.UseReactiveUI();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<!--If you are willing to use Windows/MacOS native APIs you will need to create 3 projects.
One for Windows with net8.0-windows TFM, one for MacOS with net8.0-macos and one with net8.0 TFM for Linux.-->
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Avalonia.Desktop" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TestAvaloniaApplication\TestAvaloniaApplication.csproj" />
</ItemGroup>
</Project>
18 changes: 18 additions & 0 deletions src/TestApps/TestAvaloniaApplication.Desktop/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<!-- This manifest is used on Windows only.
Don't remove it as it might cause problems with window transparency and embeded controls.
For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
<assemblyIdentity version="1.0.0.0" name="AvaloniaTest.Desktop"/>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->

<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>
</assembly>
10 changes: 10 additions & 0 deletions src/TestApps/TestAvaloniaApplication/App.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="AvaloniaApplication1.App"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

<Application.Styles>
<FluentTheme />
</Application.Styles>
</Application>
51 changes: 51 additions & 0 deletions src/TestApps/TestAvaloniaApplication/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;

using AvaloniaApplication1.ViewModels;
using AvaloniaApplication1.Views;

namespace AvaloniaApplication1;

/// <summary>
/// App.
/// </summary>
/// <seealso cref="Avalonia.Application" />
public partial class App : Application
{
/// <summary>
/// Initializes the application by loading XAML etc.
/// </summary>
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

/// <summary>
/// Called when [framework initialization completed].
/// </summary>
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainViewModel()
};
}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
{
singleViewPlatform.MainView = new MainView
{
DataContext = new MainViewModel()
};
}

base.OnFrameworkInitializationCompleted();
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<AvaloniaResource Include="Assets\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Avalonia" />
<PackageReference Include="Avalonia.Themes.Fluent" />
<PackageReference Include="Avalonia.Fonts.Inter" />
<PackageReference Include="Avalonia.ReactiveUI" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\ReactiveUI.SourceGenerators\ReactiveUI.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions src/TestApps/TestAvaloniaApplication/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

namespace AvaloniaApplication1.ViewModels;

/// <summary>
/// MainViewModel.
/// </summary>
/// <seealso cref="AvaloniaApplication1.ViewModels.ViewModelBase" />
public class MainViewModel : ViewModelBase
{
/// <summary>
/// Gets the greeting.
/// </summary>
/// <value>
/// The greeting.
/// </value>
#pragma warning disable CA1822 // Mark members as static
public string Greeting => "Welcome to Avalonia!";
#pragma warning restore CA1822 // Mark members as static
}
16 changes: 16 additions & 0 deletions src/TestApps/TestAvaloniaApplication/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using ReactiveUI;

namespace AvaloniaApplication1.ViewModels;

/// <summary>
/// ViewModelBase.
/// </summary>
/// <seealso cref="ReactiveObject" />
public class ViewModelBase : ReactiveObject
{
}
16 changes: 16 additions & 0 deletions src/TestApps/TestAvaloniaApplication/Views/MainView.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<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="clr-namespace:AvaloniaApplication1.ViewModels"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaApplication1.Views.MainView"
x:DataType="vm:MainViewModel">
<Design.DataContext>
<!-- This only sets the DataContext for the previewer in an IDE,
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
<vm:MainViewModel />
</Design.DataContext>

<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</UserControl>
25 changes: 25 additions & 0 deletions src/TestApps/TestAvaloniaApplication/Views/MainView.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using Avalonia.Controls;

namespace AvaloniaApplication1.Views;

/// <summary>
/// MainView.
/// </summary>
/// <seealso cref="UserControl" />
[ReactiveUI.SourceGenerators.IViewFor<ViewModels.MainViewModel>]
public partial class MainView : UserControl
{
/// <summary>
/// Initializes a new instance of the <see cref="MainView"/> class.
/// </summary>
public MainView()
{
InitializeComponent();
ViewModel = new ViewModels.MainViewModel();
}
}
12 changes: 12 additions & 0 deletions src/TestApps/TestAvaloniaApplication/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:AvaloniaApplication1.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:AvaloniaApplication1.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaApplication1.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="AvaloniaApplication1">
<views:MainView />
</Window>
Loading
Loading