0

A self maded Xaml customized control has a binded image in a data template via the source property to an object. In this case a BitmapImage like this:

Code behind:

enter public BitmapImage Icon => new BitmapImage(new Uri($"ms-appx:///Assets/{Tag.IconFileName}"));

Xaml:

<ListView ItemsSource="{x:Bind BiTaggedElements, Mode=OneWay}">
                                    <ListView.ItemTemplate>
                                        <DataTemplate x:DataType="content:TaggedElement">

                                            <!--  List item  -->
                                            <Grid Margin="0,0,0,5">
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="5" />
                                                    <RowDefinition Height="Auto" />
                                                    <RowDefinition Height="Auto" />
                                                    <RowDefinition Height="5" />
                                                </Grid.RowDefinitions>

                                                <StackPanel Grid.Row="1" Orientation="Horizontal">
                                                    <Image
                                                        Width="48"
                                                        Height="48"
                                                        Margin="0,0,10,0"
                                                        Source="{x:Bind Icon}"/>
                                                    <TextBlock Margin="0,0,10,0" Text="{x:Bind Title}" />
                                                    <TextBlock Margin="0,0,10,0" Text="{x:Bind Origin}" />
                                                </StackPanel>
                                                <TextBlock Grid.Row="2" Text="{x:Bind Note}"></TextBlock>
                                            </Grid>
                                        </DataTemplate>
                                    </ListView.ItemTemplate>
                                </ListView

In the end, this customized control is packed by nutget. The image files are included in the nuget nuspec file.

The problem is, i don't now the way to have an access to the assets folder in the customized control environment. In the example above, the image file needs to be in the assets folder of the main app. This customized control is implemented in a own uwp library project (ClassLibrary Uwp). This code in a main app works good.

hope to find a solution, thanks!

KusiTec
  • 23
  • 6
  • Are you asking as the author of the control, how to bundle/distribute your resources when the control is used in other projects, or because you want to customise the control template or is it just that you want access to the image to re-use it elsewhere? – Chris Schaller Mar 17 '21 at 22:20
  • As a control author, if you truly want a dynamic image path like that, then you should expose either the ImageSource as a public dependency property, taking the logic out of the custom control itself, or at the very least expose the path prefix as a property, but that is usually more of an anti-pattern. Either way, more information on the use case, perhaps even posting the custom control xaml will help to find you an acceptable answer – Chris Schaller Mar 17 '21 at 22:24
  • I have created a UWP app with a **Class Library(UWP)** project to test your thought. In my side, the `BitmapImage` in a `UserConrol` could get an image from the `Assets` folder with the code provided by you. Could you please provide us a minimal sample by OneDrive or GitHub so that we could help you find the question? – YanGu Mar 18 '21 at 03:22
  • Hi Chris, yes, its self maded. I had the same ides to get the images outside by dependency properties, but this way is a real anti pattern. it shoud be a stand alone customized control. @Yango;: Now you see the Xaml snipped. its really nothing special, standard.. – KusiTec Mar 18 '21 at 07:18
  • Yupiii, I found the solution: See https://stackoverflow.com/questions/51879891/how-to-reference-an-asset-in-class-library-in-uwp-with-xaml-syntax – KusiTec Mar 18 '21 at 21:03

0 Answers0