Image Binding to Texture Source
Posted: 01 Sep 2022, 01:50
I also wasn't able to get Image binding to work to a TextureSource.
The code to assign the view model
And the View Model itself
Nothing shows up except "Other Games", which is the value of OtherGamesTitle.
No binding errors, no exceptions. Verified that the OtherGames ienumerable has 2 objects in it, which both have TextureSources in them.
Code: Select all
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding OtherGamesTitle}" />
<ItemsControl ItemsSource="{Binding OtherGames}" Grid.Row="1">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="1" Text="{Binding Name}" />
<Image Source="{Binding Thumbnail}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</UserControl>
Code: Select all
private async void Start()
{
if (DataObject is MainViewModel mainViewModel)
{
await mainViewModel.OnNavigatedToAsync();
}
View.Content.DataContext = DataObject;
}
Code: Select all
public class MainViewModel : ScriptableObject
{
[SerializeField]
private OtherGameData[] other_games;
public IEnumerable<IOtherGameData> OtherGames { get; private set; }
public string OtherGamesTitle => "Other Games";
public Task OnNavigatedToAsync()
{
if (OtherGames == null)
{
OtherGames = other_games.Select(ogd =>
{
return new OtherGamePresenter() { Name = ogd.Name, Thumbnail = new TextureSource(ogd.Thumbnail) };
});
}
return Task.CompletedTask;
}
class OtherGamePresenter : IOtherGameData
{
public string Name { get; set; }
public ImageSource Thumbnail { get; set; }
}
}
No binding errors, no exceptions. Verified that the OtherGames ienumerable has 2 objects in it, which both have TextureSources in them.