Home > Blockchain >  Using FileResult in dotnet MAUI to populate DataTemplate in View
Using FileResult in dotnet MAUI to populate DataTemplate in View

Time:08-17

I am upgrading a Xamarin forms app to Dotnet MAUI. I have a control that allows the user to pick one or more files from the device, saves the details in an IEnumerable<FileResult> and displays the list in a view. In the Xamarin version, the relevant XAML looks like this:

<CollectionView x:Name="AttachmentList" ItemsSource="{Binding Attachments}" SelectedItem="{Binding SelectedAttachment}" SelectionMode="Single">
    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="essentials:FileResult" >
           <StackLayout Spacing="0">
                <Frame Style="{StaticResource AttachmentBubble}">
                    <StackLayout Orientation="Horizontal">
                       <Image Source="{Binding FullPath}" HeightRequest="80" />
                       <Label Text="{Binding FileName}" HorizontalOptions="StartAndExpand"/>
                    </StackLayout>
                </Frame>
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

MAUI doesn't recognise the datatype "essentials:FileResult" (fair enough as there is no longer an Essentials namespace in MAUI). FileResult is in the Microsoft.Maui.Storage namespace.

I've tried replacing the DataTemplate entry with:

<DataTemplate x:DataType="FileResult" >

and

<DataTemplate x:DataType="storage:FileResult" >

but neither work, giving me an error that the type FileResult (or storage:FileResult) does not exist.

What is the MAUI equivalent that I need to make this work?

CodePudding user response:

because FileResult is in a different assembly than your page, you have to specify the namespace and the assembly

xmlns:storage="clr-namespace:Microsoft.Maui.Storage;assembly=Microsoft.Maui.Essentials"
  • Related