Set Avalonia View Access Modifier to internal

87 views Asked by At

How can I use the internal access modifier on my views in Avalonia? The application will run but the xaml preview says invalid markup. x:ClassModifer="internal" doesn't seem to work or exist.

<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:MyApp.ViewModels"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="MyApp.Views.MainView">
  
    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
    
        <TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </StackPanel>
</UserControl>

internal partial class MainView : ReactiveUserControl<MainViewModel> {
    public MainView() {
        InitializeComponent();
        this.WhenActivated(disposables => { });
        AvaloniaXamlLoader.Load(this);
    }
}

internal class MainViewModel : ReactiveObject, IRoutableViewModel {
    public IScreen HostScreen { get; }
    public string Greeting => "Welcome to Avalonia!";

    public string UrlPathSegment { get; } = Guid.NewGuid().ToString().Substring(0, 5);

    public MainViewModel(IScreen screen) {
        HostScreen = screen;
    }
}
2

There are 2 answers

0
Tarazed On

This occurs in Avalonia because the previewer is being run by the platform specific assembly (such as AvaloniaApplication.Desktop). If you mark a view as internal, the platform application assembly no longer has access to it. Unfortuately there are few ways to work around this. Even InternalsVisibleTo doesn't affect it.

Your options are to

  1. Make it public.
  2. Make it public in DEBUG, since the application won't care.:
#if DEBUG
public partial class MainView : UserControl
#else
internal partial class MainView : UserControl
#endif

In option 2, the previewer seems to always use debug even if your IDE is configured for Release. I know neither of these are really ideal, but that's all I have found to make it work.

0
Max Katz On

x:ClassModifer="internal" doesn't seem to work or exist.

Make sure you use 11.0 Avalonia version. ClassModifer directive wasn't supported in previous versions.