Home > other >  Xamarin how to load image contorol if Url is an image and if video load video control
Xamarin how to load image contorol if Url is an image and if video load video control

Time:11-17

I am developing Facebook like feeds page where i have a list of Urls ( videos and images ). How can i load in xaml the Ui control specific to the Url type ( image or video ).

Example :

   ```<CollectionView ItemsSource="{Binding UrlList}">
            <Grid>
                If ( url is image)


                <Image Source="{Binding Url}"/>

                If ( url is video )

                <MediaElement Source="{Binding Url}" />
            </Grid>
     </CollectionView>```

CodePudding user response:

Yes, you can use DataTemplateSelector to achieve this.

A DataTemplateSelector can be used to choose a DataTemplate at runtime based on the value of a data-bound property. This enables multiple DataTemplates to be applied to the same type of object, to customize the appearance of particular objects.

1.Creating a DataTemplateSelector

A data template selector is implemented by creating a class that inherits from DataTemplateSelector. The OnSelectTemplate method is then overridden to return a particular DataTemplate.

You can refer the following code:

public class UrlTemplateSelector : DataTemplateSelector
{
    public DataTemplate ImageTemplate { get; set; }

    public DataTemplate VideoTemplate { get; set; }

    public DataTemplate OtherTemplate { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        ItemModel model = (ItemModel)item;

        if (model.Url.EndsWith(".mp4")|| model.Url.EndsWith(".avi")|| model.Url.EndsWith(".rmvb")) // you can add multiple video file suffixes
        {
            return VideoTemplate;

        }
        else if (model.Url.EndsWith(".png")|| model.Url.EndsWith(".bmp") || model.Url.EndsWith(".jpg"))//you can add multiple image file suffixes
        {
            return ImageTemplate;
        }
        else {
            return OtherTemplate;
        }
    }
}

Suppose ItemModel is the binded item.

public class ItemModel
{
    public string Name { get; set; }
    public string Url { get; set; }
}

2. Usage(suppose the page is TestPage)

TestPage.xaml

<ContentPage.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="imageTemplate">
            <ViewCell>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.4*" />
                        <ColumnDefinition Width="0.6*" />
                    </Grid.ColumnDefinitions>
                    <Label Text="{Binding Name}" TextColor="Green" FontAttributes="Bold" />
                    <Image Grid.Column="1" Source="{Binding Url}"  />
                </Grid>
            </ViewCell>
        </DataTemplate>
        <DataTemplate x:Key="videoTemplate">
            <ViewCell>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.4*" />
                        <ColumnDefinition Width="0.6*" />
                    </Grid.ColumnDefinitions>
                    <Label Text="{Binding Name}" TextColor="Red" FontAttributes="Bold" />
                <behaviors:MediaElement  Grid.Column="1" Source="{Binding Url}"  />
                </Grid>
            </ViewCell>
        </DataTemplate>
    <local:UrlTemplateSelector x:Key="mediaUrlTemplateSelector" ImageTemplate="{StaticResource imageTemplate}" VideoTemplate="{StaticResource videoTemplate}" />
    </ResourceDictionary>
</ContentPage.Resources>
<StackLayout Margin="20">
    <Label Text="ListView with a DataTemplateSelector" FontAttributes="Bold" HorizontalOptions="Center" />
<ListView x:Name="listView" Margin="0,20,0,0" ItemTemplate="{StaticResource mediaUrlTemplateSelector}" />
</StackLayout>

For more details, you can check:https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/selector.

And there is a sample included in above document, you can try it.

  • Related