Merge pull request #436 from denizmaral/patch-4

Update add-content-to-dialog.md
This commit is contained in:
Max Katz 2023-05-20 18:30:02 -04:00 коммит произвёл GitHub
Родитель 98d6db6d17 bdcc644c73
Коммит ecb61c91cf
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 41 добавлений и 2 удалений

Просмотреть файл

@ -161,9 +161,48 @@ Notice this property is instantiated with `= new ();`. Forget this and it will b
Since we are using `ObservableCollection` when we `bind` the `ListBox`s `Items` property to it, then the `ListBox` control will start listening to events and keep the `Items` inside the `ListBox` in sync with the `ObservableCollection` on the `ViewModel`.
The `ListBox` will see that the `SearchResults` has an item inside it, it will check the type of the item, which will be `AlbumViewModel`. The `ListBox` will then see if it has a `DataTemplate` for that type, which we don't. However it will find at the root of the application in `App.axaml`
The `ListBox` will see that the `SearchResults` has an item inside it, it will check the type of the item, which will be `AlbumViewModel`. The `ListBox` will then see if it has a `DataTemplate` for that type, which we don't. However it will find at the root of the application in `App.axaml`.
```markup
In order to do that we will create a special class named `ViewLocator`. Right click on project and `Add``Class/Interface`:
```csharp
using System;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Avalonia.MusicStore.ViewModels;
namespace Avalonia.MusicStore
{
public class ViewLocator : IDataTemplate
{
public bool SupportsRecycling => false;
public IControl Build(object data)
{
var name = data.GetType().FullName!.Replace("ViewModel", "View");
var type = Type.GetType(name);
if (type != null)
{
return (Control) Activator.CreateInstance(type)!;
}
else
{
return new TextBlock {Text = "Not Found: " + name};
}
}
public bool Match(object data)
{
return data is ViewModelBase;
}
}
}
```
And then add that to `App.axaml`:
```markup
<Application.DataTemplates>
<local:ViewLocator />
</Application.DataTemplates>