【问题标题】:does Prism support compiled bindingPrism 是否支持编译绑定
【发布时间】:2020-04-19 05:50:38
【问题描述】:

我不清楚 prism 是否支持编译绑定。它是一个非常好的功能,可以避免反射并加快页面的加载速度。

希望 Brian Lagunas 或 Dan Siegel 能回答这个问题。

有人可以澄清一下 prism 是否支持编译绑定以及如何做到这一点,或者它是否神奇地做到了,或者我们是否需要手动设置 bindingcontext。

任何人都将不胜感激?

更新了 noddy 示例

    <?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; }
        }

谢谢

【问题讨论】:

    标签: xamarin.forms prism


    【解决方案1】:

    是的 Compiled Bindings 可以很好地与 Prism 配合使用。

    我专门使用 Prism 版本 7.2.0.1422 和 Xamarin.Forms 4.2.0.848062 对其进行了测试。

    我关注了this tutorial

    【讨论】:

    • 感谢您的回复。你是说它只是“工作”,你不必手动设置 bindnigcontext
    • 是的,我不需要更改任何与 Prism 相关的代码,例如 BindingContext。
    • 你在某个地方有一个虚拟项目吗?
    • 不。我在自己的项目中使用过它。您可以简单地按照我提供的教程链接并将其应用于 Prism 新项目模板。
    • 如果您在将它应用到您的项目时遇到任何问题,请随时回来再次询问。
    【解决方案2】:

    根据您的描述,您希望在 Prism.Form 中进行绑定。如果要使用 Prism.Forms,需要安装 Prism Template Pack,进入 Tools > Extensions and Updates 选择 Online 并搜索 Prism 模板包。找到扩展,点击下载,首先完成安装。

    然后需要做以下事情:

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

    引用了 Prism 库。

    prism:ViewModelLocator.AutowireViewModel="True"
    

    此视图 (MainPage.xaml) 通过允许数据绑定到视图模型的命名约定自动连接到视图模型 (MainPageViewModel.cs)。所以不需要绑定BindContext。

    我做了一个样本,你可以看看:

    https://github.com/CherryBu/PrismApp

    还有两篇文章可能对你有所帮助:

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

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

    【讨论】:

    • 卜感谢您的回复。我已经在使用 Prism 模板包,在这里我们谈论的是“Prism 中的编译绑定”我在没有编译绑定的情况下使用 prism 没有问题。关于根本不需要的“AutowireViewModel”,并且像我正在做的那样使用反射 。再一次,这篇文章是关于编译绑定的。非常感谢
    • @Ted,我找了一篇关于设置bindingcontext的文章,可能对你有帮助:blog.nuits.jp/…
    猜你喜欢
    • 2014-09-30
    • 1970-01-01
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多