Page 1 of 1

Binding Image source issue

Posted: 08 Feb 2019, 05:55
by destrugter
I can't seem to bind my image element's source. What I have is the following:
            <ListBox Grid.Row="1"
                     ItemsSource="{Binding Turrets, UpdateSourceTrigger=PropertyChanged}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button Command="{Binding DataContext.ButtonClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                            CommandParameter="{Binding}">
                        <Button.Content>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>

                                <Image Name="TurretImage"
                                       Grid.Row="0"
                                       Grid.RowSpan="6"
                                       Grid.Column="0"
                                       Width="50"
                                       Height="50"
                                       Source="{Binding Image}"/>

                                <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Radius}" />
                                <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding Damage}" />
                                <TextBlock Grid.Column="1" Grid.Row="2" Text="{Binding Name}" />
                                <TextBlock Grid.Column="1" Grid.Row="3" Text="{Binding FireDelay}" />
                                <TextBlock Grid.Column="1" Grid.Row="4" Text="{Binding Kills}" />
                                <TextBlock Grid.Column="1" Grid.Row="5" Text="{Binding Cost}" />
                            </Grid>
                        </Button.Content>
                    </Button>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
The interesting part being:
                                <Image Name="TurretImage"
                                       Grid.Row="0"
                                       Grid.RowSpan="6"
                                       Grid.Column="0"
                                       Width="50"
                                       Height="50"
                                       Source="{Binding Image}"/>
The Image is a member of a class and is a path to the .png image I want to display.

The error I get is:
NoesisException: Unable to load image 'Assets/Sprites/turret.png'.
Rethrow as NoesisException
Noesis.Error.Check () (at Assets/NoesisGUI/Plugins/API/Core/NoesisError.cs:17)
Noesis.Extend.Noesis_InstantiateExtend_ (System.IntPtr nativeType) (at Assets/NoesisGUI/Plugins/API/Core/NoesisExtendImports.cs:26)
Noesis.Extend.NewCPtr (System.Type type, System.Object instance) (at Assets/NoesisGUI/Plugins/API/Core/NoesisExtend.cs:3896)
Noesis.Extend.GetInstanceHandle (System.Object instance) (at Assets/NoesisGUI/Plugins/API/Core/NoesisExtend.cs:4288)
Noesis.Extend.ListGet (System.IntPtr cPtr, System.UInt32 index) (at Assets/NoesisGUI/Plugins/API/Core/NoesisExtend.cs:2036)
Rethrow as NoesisException
Noesis.Error.Check () (at Assets/NoesisGUI/Plugins/API/Core/NoesisError.cs:17)
Noesis.View.Noesis_View_SetSize_ (System.Runtime.InteropServices.HandleRef view, System.Int32 width, System.Int32 height) (at Assets/NoesisGUI/Plugins/API/Core/NoesisView.cs:409)
Noesis.View.SetSize (System.Int32 width, System.Int32 height) (at Assets/NoesisGUI/Plugins/API/Core/NoesisView.cs:44)
NoesisView.UpdateSettings () (at Assets/NoesisGUI/Plugins/NoesisView.cs:660)
NoesisView.LateUpdate () (at Assets/NoesisGUI/Plugins/NoesisView.cs:673)

Re: Binding Image source issue

Posted: 11 Feb 2019, 10:35
by sfernandez
This looks like a problem with dependencies. As xaml is not directly referencing the texture, only the code does, the texture is not included in your scene (not registered in the TextureProvider), so when code tries to load the texture it can't find the asset.

There are several ways of solving this, for example one option is having all your textures in a xaml dictionary (as ImageSource resources) that you reference in the root xaml.
Another would be placing the textures inside a Resources folder, then load them with Resources.Load to create a TextureSource and return it in the ViewModel (instead of a path).

Have you tried something like that? Let us know if this helps.