Reputation: 61
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'.'
Upvotes: 0
Views: 4752
Reputation: 61
The answer is in this issue on github. If it doesn't work immediately, try restarting visual studio.
Upvotes: 0