0

I've started a brand new project in Visual Studio 2019 using the Avalonia MVVM Application template. I've included the Avalonia, Avalonia.Desktop, Avalonia.Controls.DataGrid, and Avalonia.ReactiveUI packages from NuGet, and updated them all to version 0.8.3. The first control I've attempted to put on my form is a DataGrid, backed by a simple PersonModel class.

Every time I try to run the program, I get an error. This is literally the first project I've tried to use Avalonia for, and I have very little experience with WPF or UWP, so I am completely lost. Please help me figure out how to get a DataGrid on my form.

Below are literally the only changes I've made to anything. Everything else is exactly as the template laid it out, which worked until I tried to add the DataGrid.

PersonModel.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace MyrinaUI.Models {
    public class PersonModel {
        public int DepartmentNumber { get; set; }
        public string DeskLocation { get; set; }
        public int EmployeeNumber { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

MainWindowViewModel.cs

using MyrinaUI.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace MyrinaUI.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
        public ObservableCollection<PersonModel> People { get; }

        public MainWindowViewModel() {
            People = new ObservableCollection<PersonModel>(GenerateMockPeopleTable());
        }

        private IEnumerable<PersonModel> GenerateMockPeopleTable() {
            var defaultPeople = new List<PersonModel>() {
                new PersonModel() {
                    FirstName = "Pat",
                    LastName = "Patterson",
                    EmployeeNumber = 1010,
                    DepartmentNumber = 100,
                    DeskLocation = "B3F3R5T7"
                },
                new PersonModel() {
                    FirstName = "Jean",
                    LastName = "Jones",
                    EmployeeNumber = 1011,
                    DepartmentNumber = 101,
                    DeskLocation = "B3F3R5T8"
                },
                new PersonModel() {
                    FirstName = "Terry",
                    LastName = "Thompson",
                    EmployeeNumber = 2010,
                    DepartmentNumber = 200,
                    DeskLocation = "B3F3R5T9"
                },
            };

            return defaultPeople;
        }
    }
}

App.xaml:

<Application xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyrinaUI"
             x:Class="MyrinaUI.App">
    <Application.DataTemplates>
        <local:ViewLocator/>
    </Application.DataTemplates>

    <Application.Styles>
        <StyleInclude Source="avares://Avalonia.Themes.Default/DefaultTheme.xaml"/>
        <StyleInclude Source="avares://Avalonia.Themes.Default/Accents/BaseDark.xaml"/>
        <StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Default.xaml"/>
    </Application.Styles>
</Application>

MainWindow.xaml

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:MyrinaUI.ViewModels;assembly=MyrinaUI"
        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="MyrinaUI.Views.MainWindow"
        Icon="/Assets/avalonia-logo.ico"
        Title="MyrinaUI">

    <Design.DataContext>
        <vm:MainWindowViewModel/>
    </Design.DataContext>

  <StackPanel>
    <DataGrid AutoGenerateColumns="True" Items="{Binding People}"/>
  </StackPanel> 
</Window>

The exception that is thrown is: System.IO.FileNotFoundException: 'The resource avares://Avalonia.Controls.DataGrid/Themes/Default.xaml could not be found.'

Which seems pretty straight forward, but having looked at the source on GitHub and seen the file in question, I'm not sure where to look to make sure it exists locally, or if that would even help me.

Removing that line from App.xaml results in: Portable.Xaml.XamlObjectWriterException: 'Cannot create unknown type '{https://github.com/avaloniaui}DataGrid'.'

lk75
  • 61
  • 1
  • 7
  • 1
    That sounds like [this issue](https://github.com/AvaloniaUI/Avalonia/issues/2925). I can confirm the solution posted there works. – Herohtar Nov 05 '19 at 01:41
  • It does sound like the same issue, but I found another post in the "issues" section of github contending that it's no longer necessary. Admittedly, I didn't actually try it until just now, but it doesn't help. – lk75 Nov 05 '19 at 01:45
  • What do you mean it doesn't help? Are you getting an error using that solution? And where's the post saying it's no longer necessary? – Herohtar Nov 05 '19 at 01:48
  • Strangely, I took the StyleInclude out of appl.xaml, and tried just running it with .UseDataGrid(); on AppBuilder, as recommended in the post linked above. It compiled and ran without issue, but did not display a data grid. I added the StyleInclude back and saved, and the error was back. I then restarted Visual Studio, and now it's working. So I guess that is the solution after all. Thanks Herohtar. – lk75 Nov 05 '19 at 01:55
  • By "doesn't help" I mean it didn't change the behavior in any way. Here is the post stating that the UseDataGrid() portion should no longer be necessary. https://github.com/AvaloniaUI/Avalonia/issues/2426 I'm currently unable to find a post that says the xaml portion of that solution isn't necessary. I probably misread something. At any rate, it looks like it *does* work, and Visual Studio was just in some sort of odd state. Thanks again. If you want to make an actual post with this as the answer, I will accept it. – lk75 Nov 05 '19 at 02:04

1 Answers1

0

The answer is in this issue on github. If it doesn't work immediately, try restarting visual studio.

lk75
  • 61
  • 1
  • 7