Xamarin.forms 棱镜形状不';我再也不想去航海了。如何在查看前运行一些代码';谁的构造函数?

Xamarin.forms 棱镜形状不';我再也不想去航海了。如何在查看前运行一些代码';谁的构造函数?,xamarin.forms,navigation,prism,Xamarin.forms,Navigation,Prism,请看下面的代码。在视图的构造函数中设置了两个DelegateCommand: public DelegateCommand DeletePromotionCommand { get; set; } public DelegateCommand EditPromotionCommand { get; set; } public PromotionDetailViewModel(INavigationService navigationService, IPageDialo

请看下面的代码。在视图的构造函数中设置了两个DelegateCommand:

    public DelegateCommand DeletePromotionCommand { get; set; }
    public DelegateCommand EditPromotionCommand { get; set; }

    public PromotionDetailViewModel(INavigationService navigationService, IPageDialogService pageDialogService)
        : base(navigationService, pageDialogService)
    {
        Title = "Promoção";

        DeletePromotionCommand = new DelegateCommand(DeletePromotion, CanDeletePromotion);
        EditPromotionCommand = new DelegateCommand(EditPromotion, CanEditPromotion);
    }
在构造函数中设置EditPromotionCommand时调用CanEditPromotion。CANEDIT促销方式如下所示:

    private bool CanEditPromotion()
    {
        var userString = Preferences.Get("user", string.Empty);

        if (userString == string.Empty)
            return false;

        var userId = (Guid)JObject.Parse(userString)["id"];

        if (userId == Promotion.CreatedBy)
            return true;
        else
            return false;
    }
注意,在第四句中,我需要Promotion属性。此属性需要在视图的构造函数之前设置,因此它将为null,并且正好在第行,它将中断应用程序

在此之前,我应该使用下面的代码设置升级属性,但Prism不再具有OnNavigationTo方法。促销信息来自之前的页面,并作为参数导航传递:

    public override async void OnNavigatingTo(INavigationParameters parameters)
    {
        base.OnNavigatedTo(parameters);

        try
        {
            IsBusy = true;

            Promotion = parameters["promotion"] as Promotion;

            var marketService = new Service<Market>();

            Market = await marketService.GetAsync(Promotion.MarketId);

            IsBusy = false;
        }
        catch (Exception)
        {
            IsBusy = false;
        }
    }
public override async void onNavigationTo(INavigationParameters)
{
base.OnNavigatedTo(参数);
尝试
{
IsBusy=true;
升级=参数[“升级”]作为升级;
var marketService=新服务();
Market=等待marketService.GetAsync(Promotion.MarketId);
IsBusy=false;
}
捕获(例外)
{
IsBusy=false;
}
}

当我尝试在我的BaseViewModel中使用INavigatingTo时,它会向我显示一条消息,告诉我改为使用IInitialize。我试过了,但是初始化方法仍然在视图的构造函数之后被触发。

正如Prism 7.2的官方发行说明中所指出的那样,导航到在Prism社区进行了大量考虑和反馈后被弃用。部分原因是,在将视图推送到导航堆栈之前,OnNavigationTo应该运行以初始化ViewModel。问题是,随着时间的推移,它的意图逐渐消失,人们试图滥用API。我们前进的唯一方法是从
INavigationAware
中删除对
INavigatingAware
的引用,不幸的是,这造成了一个软中断,
onnavigationto
根本不被调用。如果直接引用了INavigatingAware,则会出现硬编译错误

要迁移代码,应将新的初始化API与
IInitialize
IInitializeAsync
IAutoInitialize
一起使用。假设您只需使用IInitialize,您将从以下位置更新旧代码:

public void OnNavigatingTo(INavigationParameters parameters)
{
    // your code here
}
到新的初始化版本

public void Initialize(INavigationParameters parameters)
{
    // your code here
}
请记住,如果使用异步版本,则长时间运行的任务必须在推送页面之前完成,从而导致导航明显延迟。因此,通常更希望使用
async void
来避免阻塞导航


您可以在Prism 7.2发行说明中阅读更多内容,正如Prism 7.2的官方发行说明所示,OnNavigationTo在Prism社区经过大量考虑和反馈后被弃用。部分原因是,在将视图推送到导航堆栈之前,OnNavigationTo应该运行以初始化ViewModel。问题是,随着时间的推移,它的意图逐渐消失,人们试图滥用API。我们前进的唯一方法是从
INavigationAware
中删除对
INavigatingAware
的引用,不幸的是,这造成了一个软中断,
onnavigationto
根本不被调用。如果直接引用了INavigatingAware,则会出现硬编译错误

要迁移代码,应将新的初始化API与
IInitialize
IInitializeAsync
IAutoInitialize
一起使用。假设您只需使用IInitialize,您将从以下位置更新旧代码:

public void OnNavigatingTo(INavigationParameters parameters)
{
    // your code here
}
到新的初始化版本

public void Initialize(INavigationParameters parameters)
{
    // your code here
}
请记住,如果使用异步版本,则长时间运行的任务必须在推送页面之前完成,从而导致导航明显延迟。因此,通常更希望使用
async void
来避免阻塞导航

您可以在Prism 7.2发行说明中阅读更多内容

我是这样解决的:

在CanEditPromotion中,我对升级属性进行了null验证:

    private bool CanEditPromotion()
    {
        var userString = Preferences.Get("user", string.Empty);

        if (userString == string.Empty)
            return false;

        var userId = (Guid)JObject.Parse(userString)["id"];

        if (Promotion != null && userId == Promotion.CreatedBy)
            return true;
        else
            return false;
    }
    public override async void OnNavigatedTo(INavigationParameters parameters)
    {
        try
        {
            IsBusy = true;

            Promotion = parameters["promotion"] as Promotion;                

            var marketService = new Service<Market>();

            Market = await marketService.GetAsync(Promotion.MarketId);

            IsBusy = false;
        }
        catch (Exception)
        {
            IsBusy = false;
        }
    }
当设置EditPromotionCommand时,我观察升级属性:

    public DelegateCommand DeletePromotionCommand { get; set; }
    public DelegateCommand EditPromotionCommand { get; set; }

    public PromotionDetailViewModel(INavigationService navigationService, IPageDialogService pageDialogService)
        : base(navigationService, pageDialogService)
    {
        Title = "Promoção";

        DeletePromotionCommand = new DelegateCommand(DeletePromotion, CanDeletePromotion)
            .ObservesProperty(() => Promotion);
        EditPromotionCommand = new DelegateCommand(EditPromotion, CanEditPromotion)
            .ObservesProperty(() => Promotion);
    }
我使用OnNavigatedTo方法设置升级属性:

    private bool CanEditPromotion()
    {
        var userString = Preferences.Get("user", string.Empty);

        if (userString == string.Empty)
            return false;

        var userId = (Guid)JObject.Parse(userString)["id"];

        if (Promotion != null && userId == Promotion.CreatedBy)
            return true;
        else
            return false;
    }
    public override async void OnNavigatedTo(INavigationParameters parameters)
    {
        try
        {
            IsBusy = true;

            Promotion = parameters["promotion"] as Promotion;                

            var marketService = new Service<Market>();

            Market = await marketService.GetAsync(Promotion.MarketId);

            IsBusy = false;
        }
        catch (Exception)
        {
            IsBusy = false;
        }
    }
public override async void OnNavigatedTo(INavigationParameters)
{
尝试
{
IsBusy=true;
升级=参数[“升级”]作为升级;
var marketService=新服务();
Market=等待marketService.GetAsync(Promotion.MarketId);
IsBusy=false;
}
捕获(例外)
{
IsBusy=false;
}
}
我是这样解决的:

在CanEditPromotion中,我对升级属性进行了null验证:

    private bool CanEditPromotion()
    {
        var userString = Preferences.Get("user", string.Empty);

        if (userString == string.Empty)
            return false;

        var userId = (Guid)JObject.Parse(userString)["id"];

        if (Promotion != null && userId == Promotion.CreatedBy)
            return true;
        else
            return false;
    }
    public override async void OnNavigatedTo(INavigationParameters parameters)
    {
        try
        {
            IsBusy = true;

            Promotion = parameters["promotion"] as Promotion;                

            var marketService = new Service<Market>();

            Market = await marketService.GetAsync(Promotion.MarketId);

            IsBusy = false;
        }
        catch (Exception)
        {
            IsBusy = false;
        }
    }
当设置EditPromotionCommand时,我观察升级属性:

    public DelegateCommand DeletePromotionCommand { get; set; }
    public DelegateCommand EditPromotionCommand { get; set; }

    public PromotionDetailViewModel(INavigationService navigationService, IPageDialogService pageDialogService)
        : base(navigationService, pageDialogService)
    {
        Title = "Promoção";

        DeletePromotionCommand = new DelegateCommand(DeletePromotion, CanDeletePromotion)
            .ObservesProperty(() => Promotion);
        EditPromotionCommand = new DelegateCommand(EditPromotion, CanEditPromotion)
            .ObservesProperty(() => Promotion);
    }
我使用OnNavigatedTo方法设置升级属性:

    private bool CanEditPromotion()
    {
        var userString = Preferences.Get("user", string.Empty);

        if (userString == string.Empty)
            return false;

        var userId = (Guid)JObject.Parse(userString)["id"];

        if (Promotion != null && userId == Promotion.CreatedBy)
            return true;
        else
            return false;
    }
    public override async void OnNavigatedTo(INavigationParameters parameters)
    {
        try
        {
            IsBusy = true;

            Promotion = parameters["promotion"] as Promotion;                

            var marketService = new Service<Market>();

            Market = await marketService.GetAsync(Promotion.MarketId);

            IsBusy = false;
        }
        catch (Exception)
        {
            IsBusy = false;
        }
    }
public override async void OnNavigatedTo(INavigationParameters)
{
尝试
{
IsBusy=true;
升级=参数[“升级”]作为升级;
var marketService=新服务();
Market=等待marketService.GetAsync(Promotion.MarketId);
IsBusy=false;
}
捕获(例外)
{
IsBusy=false;
}
}

Nether Initialize或InitializeAsync方法在构造函数之前运行。我需要一种在应用程序运行构造函数之前设置升级属性的方法。这不是语言的工作方式。。。这也不是OnNavigation的工作原理。构造函数必须首先运行,这样我们就有了一个可以