PreviousPageModel为空

时间:2019-02-14 14:06:31

标签: c# xamarin.forms freshmvvm

我有一个带有freshmvvm框架的Xamarin.Forms应用程序。根据文档,我可以使用FreshBasePageModel基类的PreviousPageModel属性来访问我从中导航的PageModel的数据。我这样导航:

    public FirstPageModel()
    {
        _validator = new CalculatorValidator();
        CalculateCommand = new Command(execute: () => 
        {
            ValidationResult = _validator.Validate(this);
            RaisePropertyChanged(nameof(ValidationResult));

            if (ValidationResult.IsValid)
            {
                CoreMethods.PushPageModel<SecondPageModel>();
            }
        });
    }

发生导航,但在SecondPageModel构造函数中,PreviousPageModel为null:

    public SecondPageModel()
    {
        _previousModel = (FirstPageModel)PreviousPageModel;
    }

我在做什么错了?

谢谢。

编辑:

我也尝试过:

    public FirstPageModel()
    {
        _validator = new CalculatorValidator();
        CalculateCommand = new Command(Calculate);
    }

    private void Calculate()
    {
        ValidationResult = _validator.Validate(this);
        RaisePropertyChanged(nameof(ValidationResult));

        if(ValidationResult.IsValid)
        {
            CoreMethods.PushPageModel<SecondPageModel>(this);
        }
    }

2 个答案:

答案 0 :(得分:0)

问题在于,直到此时,在您的PageModel生命周期中,PreviousPageModel属性尚未分配值。

现在,我可以确定在调用Init生命周期方法时此属性可用。

public override void Init(object initData)
{
  _previousModel = (FirstPageModel)PreviousPageModel;
}

但是,如果您不想执行此操作,并且希望严格将其放在构造函数中,则可以执行基本调用,并希望我们的朋友micheal在那里分配了此属性:

如下所示:

public SecondPageModel() : base()
{

}

答案 1 :(得分:0)

我在这里得到了答案:

https://forums.xamarin.com/discussion/comment/365262/#Comment_365262

PreviousPageModel为null,因为尚未在构造函数中设置它。将您的代码放入ViewIsAppearing生命周期事件中,然后您将获得正确的先前模型:

$('#example').DataTable( {
    "dom": 'Bfirtlp',
    buttons: [

            {
            extend: 'excelHtml5',
            exportOptions: {
                columns: ':visible'
            }
            },
        {
            extend: 'pdfHtml5',
            orientation: 'landscape',
            pageSize: 'A3',
            exportOptions: {
                columns: [ 0, 1, 2,3,4,5,6,7,8],

            },
        ],
   initComplete: function () {
        this.api().columns().every( function () {
            var column = this;
             var eachHeader = $(column.header())[0];
             var headingVal = eachHeader.getAttribute("value");
             var select = $('<select class="btn btn-secondary btn-sm dropdown-toggle"><div class="dropdown-menu"><option  class="dropdown-item" value="">'+ headingVal +'</option><div></select>')
                .appendTo( $(column.footer()).empty() )
                .on( 'change', function () {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                    );

                    column
                        .search( val ? '^'+val+'$' : '', true, false )
                        .draw();
                } );

            column.data().unique().sort().each( function ( d, j ) {
                select.append( '<option value="'+d+'">'+d+'</option>' )
            } );
        } );
    }
} );