东北大学全栈开发课程笔记8

连接View和ViewModel:ViewModelLocator
1.实现IPreferenceStorage、PreferenceStorage类
2.注册
public class ViewModelLocator {
    /// <summary>
    /// 搜索结果页ViewModel
    /// </summary>
    public ResultPageViewModel ResultPageViewModel =>
        SimpleIoc.Default.GetInstance<ResultPageViewModel>();

    /// <summary>
    /// ViewModelLocator
    /// </summary>
    public ViewModelLocator() {
        SimpleIoc.Default.Register<ResultPageViewModel>();
        SimpleIoc.Default.Register<IPoetryStorage, PoetryStorage>();
        SimpleIoc.Default.Register<IPreferenceStorage, PreferenceStorage>();
    }
}
3.App.xaml注册vm命名空间
xmlns:vm="clr-namespace:MasterDetailTemplate.ViewModels;assembly=MasterDetailTemplate"

<Application.Resources>
    <ResourceDictionary>
        <vm:ViewModelLocator x:Key="ViewModelLocator" />
        <!--Global Styles-->
        <Color x:Key="NavigationPrimary">#2196F3</Color>
        <Style TargetType="NavigationPage">
            <Setter Property="BarBackgroundColor"
                    Value="{StaticResource NavigationPrimary}" />
            <Setter Property="BarTextColor"
                    Value="White" />
        </Style>
    </ResourceDictionary>
</Application.Resources>
4.ResultPage.xaml (View层) Binding到ViewModel
BindingContext="{Binding ResultPageViewModel, Source={StaticResource ViewModelLocator}}"
ContentPage没有Command但有Appearing事件
5.安装依赖包:Behaviors.Forms 将Appearing事件关联到PageAppearingCommand
6.ContentPage.Behaviors
xmlns:b="clr-namespace:Behaviors;assembly=Behaviors"
<ContentPage.Behaviors>
    <b:EventHandlerBehavior EventName="Appearing">
        <b:ActionCollection>
            <b:InvokeCommandAction Command="{Binding PageAppearingCommand}" />
        </b:ActionCollection>
    </b:EventHandlerBehavior>
</ContentPage.Behaviors>
7.ResultPage引入命名空间 ListViewBehavior
xmlns:s="clr-namespace:Xamarin.Forms.Extended;assembly=Xamarin.Forms.Extended.InfiniteScrolling"
<ListView.Behaviors>
    <s:InfiniteScrollBehavior />
</ListView.Behaviors>

Behaviors机制用来扩展控件功能

8.在MainPage里修改显示视图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值