mandalorianbob
Topic Author
Posts: 9
Joined: 27 Jul 2022, 23:37

Image Binding to Texture Source

01 Sep 2022, 01:50

I also wasn't able to get Image binding to work to a TextureSource.
<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>
    
The code to assign the view model
private async void Start()
    {
        if (DataObject is MainViewModel mainViewModel)
        {
            await mainViewModel.OnNavigatedToAsync();
        }
        View.Content.DataContext = DataObject;
    }
  
And the View Model itself
 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; }
        }
    }
    
    
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.
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Image Binding to Texture Source

01 Sep 2022, 12:50

Hi,

In Noesis the ItemsSource only works when you expose a property that implements the IList interface, could you try to that?
Although I verified that we are not showing any error when you bind any other type, we'll fix it for the next version.

Please let me know if you have any other problem.
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

Re: Image Binding to Texture Source

01 Sep 2022, 13:16

Just an addition... from the point of view of Noesis using an IEnumerable won't be efficient because we access items using index in many cases.
And in the code you attached would be even worse, because everytime the collection is iterated it will create new objects.
 
mandalorianbob
Topic Author
Posts: 9
Joined: 27 Jul 2022, 23:37

Re: Image Binding to Texture Source

01 Sep 2022, 21:38

You're right that the IEnumerable, especially with the Linq query in it, wouldn't be efficient.

That said, you guys very explicitly put in your docs that this is supposed to mimic WPF, and any deviation should be considered a bug. IEnumerable is kind of the foundational collective type in XAML. It's a pretty severe breaking change to require IList ... anywhere, really, but especially in ItemsControl which is the most basic form of repeater control.

So ... just throwing that out there. That's a much bigger break than it sounds. For instance, now in order to use an ItemsControl - the most basic form of repeater - the requirements have gone from
IEnumerator GetEnumerator();
(which is obviously flexible enough to allow for custom IEnumerators to be used)
to requiring
IEnumerator GetEnumerator();
int Count { get; }
bool IsSynchronized { get; }
object SyncRoot { get; 
void CopyTo(Array array, int index);
object this[int index] { get; set; }
bool IsFixedSize { get; }
bool IsReadOnly { get; }
int Add(object value);
void Clear();
bool Contains(object value);
int IndexOf(object value);
void Insert(int index, object value);
void Remove(object value);
void RemoveAt(int index);
That seems like a lot.

I guess I would encourage you to be very explicit about that both in the documentation and yeah, with a very robust error message.
 
mandalorianbob
Topic Author
Posts: 9
Joined: 27 Jul 2022, 23:37

Re: Image Binding to Texture Source

01 Sep 2022, 21:38

Just realized I forgot to add that yes, IList did work.
 
User avatar
jsantos
Site Admin
Posts: 4822
Joined: 20 Jan 2012, 17:18
Contact:

Re: Image Binding to Texture Source

02 Sep 2022, 17:48

In this case, you don't need to implement IList at all, because we support C# arrays. So casting to IOtherGameData[] in your example should do the trick.

It is true, that any deviation from WPF is considered a bug by our team but this is a special situation because only implementing IEnumerable can lead to inefficiencies like:
  • When using with INotifyCollectionChanged, based on indices
  • ItemsControl and virtualization also need to access the collection by index
All these cases generate quadratic inefficiencies in time and memory (IEnumerable was also very inefficient in Unity years ago, and even now it is still doing allocations depending on the scenario).

WPF, supports it, hiding the inefficiency and we think this is dangerous. But yeah, we could open a ticket to support this if you think this is critical. We will emit a warning in these scenarios, so at the end, we are not sure if it makes sense giving support for this pattern. :)
 
mandalorianbob
Topic Author
Posts: 9
Joined: 27 Jul 2022, 23:37

Re: Image Binding to Texture Source

02 Sep 2022, 19:41

I mean it's totally fair - WPF was not very efficient haha. I've just done a lot of XAML development, so I'm trying to reconcile what I expect from XAML vs what's required for game performance. That's why I wasn't even suggesting a change really, just documentation and messaging.

I'm prototyping some stuff while I evaluate NoesisGUI, so I'm trying to figure out the pitfalls so I can evaluate properly.

But thinking about it - is this true of all of your data controls then? Does Noesis just generally not support IEnumerable?

Who is online

Users browsing this forum: No registered users and 7 guests