首页 > 解决方案 > 使用 App.current.MainPage 导航后图像源重置

问题描述

在登录时,我正在获取并存储一些属性(例如用户名、创建日期和电子邮件),包括设置插件中登录用户的图像路径(字符串类型)属性,并将其用作图像元素的来源主详细信息页面。当我转到其他页面并希望使用 Application.Current.MainPage 导航回主详细信息页面时,所有其他属性都将保留,但图像会清除,这显然意味着图像路径属性丢失。有没有人有这个问题,你是如何解决的?

这就是我在 JObject 和设置中存储图像路径属性的方式:

 var imagepath = jObject.Value<string>("ImagePath");

   Settings.Path = imagepath;

这就是我设置图像元素源路径的方式:

     profileImg.Source = 
     string.Format(UrlConstants.url2,Settings.Path.Substring(1));

这就是我从其他页面导航到主详细信息的方式:

  Application.Current.MainPage = new PeppyMasterDetail();

这是我的 Master Detail 页面的 Master Page 部分的构造函数:

  public MasterPage()
        {
            InitializeComponent();
            usernameLbl.Text = Settings.UserName;
            emailLbl.Text = Settings.Email;
            dateLbl.Text = "JOINED: " + Settings.Date;
            profileImg.Source = string.Format(UrlConstants.url2, Settings.Path.Substring(1));

        }

这是我的主详细信息页面的构造函数

    public PeppyMasterDetail ()
        {
            InitializeComponent ();                 
            mPage.listView.ItemSelected += OnItemSelected;

        }

使用 Application.Current.MainPage 导航后,我希望仍然能够看到我的图像而不会被清除。

PS:当我使用 Navigation.PushAsync 或 Navigation.PopAsync() 进行导航时,不会发生此行为;

标签: c#xamarin.formsnavigationmaster-detail

解决方案


推荐阅读