Ted
Ted

Reputation: 13

does Prism support compiled binding

It's not clear to me if Compiled binding is supported in prism. Its an extremely good feature that avoids reflection and speeds up the load of the page.

Hopefully either Brian Lagunas or Dan Siegel will answer this one.

Can somebody clarify if compiled binding is supported in prism and how to do it or does it magically do it or do we need to manually set bindingcontext.

An answer would be greatly appreciated by anyone?

updated with noddy sample

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="PrismCompiledBinding.Views.MainPage"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:models="clr-namespace:PrismCompiledBinding.Models">

        <!--x:DataType="ViewModel:MainPageViewModel"-->
        <!--mc:Ignorable="d"-->
        <ContentPage.Content>
            <ListView
                ItemsSource="{Binding Monkeys}"
                SeparatorVisibility="None"
                VerticalOptions="FillAndExpand">
                <ListView.ItemTemplate>
                    <DataTemplate  x:DataType="models:Monkey">
                        <ViewCell>
                            <StackLayout
                                Padding="20,10,0,10"
                                Orientation="Horizontal"
                                Spacing="20"
                                VerticalOptions="FillAndExpand">

                                <Label
                                    FontSize="Medium"
                                    Text="{Binding Name}"
                                    TextColor="Black"
                                    VerticalOptions="Center" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </ContentPage.Content>
    </ContentPage>

    using System.Collections.ObjectModel;
    using Prism.Navigation;
    using PrismCompiledBinding.Models;

    namespace PrismCompiledBinding.ViewModels
    {
        public class MainPageViewModel : ViewModelBase
        {
            public MainPageViewModel(INavigationService navigationService)
                : base(navigationService)
            {
                Monkeys = GetMonkeys();
            }


            private ObservableCollection<Monkey> monkeys;

            public ObservableCollection<Monkey> Monkeys
            {
                get => monkeys;
                set => SetProperty(ref monkeys, value);
            }

            private ObservableCollection<Monkey> GetMonkeys()
            {
                ObservableCollection<Monkey> list=new ObservableCollection<Monkey>();
                list.Add(new Monkey
                {
                    Name = "Baboon",
                    Location = "Africa & Asia",
                    Details =
                        "Baboons are African and Arabian Old World monkeys belonging to the genus Papio, part of the subfamily Cercopithecinae.",
                });
                list.Add(new Monkey
                {
                    Name = "Capuchin Monkey",
                    Location = "Central & South America",
                    Details =
                        "The capuchin monkeys are New World monkeys of the subfamily Cebinae. Prior to 2011, the subfamily contained only a single genus, Cebus.",
                });
                return list;
            }
        }
    }

       public class Monkey
        {
            public string Name { get; set; }
            public string Location { get; set; }
            public string Details { get; set; }
        }

thanks

Upvotes: 1

Views: 302

Answers (2)

Cherry Bu - MSFT
Cherry Bu - MSFT

Reputation: 10346

According to your description, You want to binding in Prism.Form. If you want to use Prism.Forms, you need to install Prism Template Pack, go to Tools > Extensions and Updates select Online and search for Prism Template Pack. Locate the the extensions, click Download, and complete the installation firstly.

Then needing to do following thing:

 xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"

The Prism library is referenced.

prism:ViewModelLocator.AutowireViewModel="True"

This view (MainPage.xaml) is wired to the view model (MainPageViewModel.cs) automatically via naming conventions allowing for databinding to the view model. So you don't need to bind BindContext.

I do one sample that you can take a look:

https://github.com/CherryBu/PrismApp

There are also two article that may help you:

https://prismlibrary.com/docs/xamarin-forms/Getting-Started.html

https://dzone.com/articles/getting-started-with-xamarin-forms-and-prism

Upvotes: 0

VahidShir
VahidShir

Reputation: 2106

Yes Compiled Bindings works fine with Prism.

I specifically tested it using Prism version 7.2.0.1422 and Xamarin.Forms 4.2.0.848062.

I followed this tutorial.

Upvotes: 1

Related Questions