Hello,
Welcome to our Microsoft Q&A platform!
If you want to update the image at the runtime.
You need to change following places.
First, you need to edit the savedfirebaseauth.User.PhotoUrl = "msn.svg";
line in the GetProfileInformationAndRefreshToken
method of FirebaseServices.cs
. you give an specific value, so this image will not change in the MainPage. If we login this application firstly, set a default value like following code.
public async Task<Firebase.Auth.User> GetProfileInformationAndRefreshToken() {
var savedfirebaseauth = JsonConvert.DeserializeObject<FirebaseAuth>(Preferences.Get(App.FirebaseRefreshToken, string.Empty));
var RefreshedContent = await AuthProvider.RefreshAuthAsync(savedfirebaseauth);
Preferences.Set(App.FirebaseRefreshToken, JsonConvert.SerializeObject(RefreshedContent));
if (string.IsNullOrEmpty(savedfirebaseauth.User.PhotoUrl))
{
savedfirebaseauth.User.PhotoUrl = "msn.svg";
}
return savedfirebaseauth.User;
}
Second, you need to give FireUser value after you update the image in the ProfilePageViewModel.cs
. If you do not give value to FireUser, you always get the MSN result.
private async void SelectAvatarAction() {
FireUser = await App.services.UpdateUserData(SelectedAvatar, DisplayName);
Console.WriteLine(FireUser.PhotoUrl);
}
Third, the code order is wrong in UpdateUserData
method of FirebaseServices.cs
. You need get the update user firstly then set it to savedfirebaseauth.User
, fresh the token in the end like following method.
public async Task<Firebase.Auth.User> UpdateUserData(string PhotoUri, string DisplyName)
{
var savedfirebaseauth = JsonConvert.DeserializeObject<FirebaseAuth>(Preferences.Get(App.FirebaseRefreshToken, string.Empty));
var newUser = await AuthProvider.UpdateProfileAsync(Preferences.Get(App.FirebaseToken, string.Empty),
DisplyName, PhotoUri);
savedfirebaseauth.User.DisplayName = DisplyName;
savedfirebaseauth.User.PhotoUrl = PhotoUri;
var RefreshedContent = await AuthProvider.RefreshAuthAsync(savedfirebaseauth);
Preferences.Set(App.FirebaseRefreshToken, JsonConvert.SerializeObject(RefreshedContent));
return newUser.User;
}
Then you can update image in the ProfilePage
at run time. If you want to update the profile image in the mainpage as well when you back to the mainpage, you need to change value from private async void GetUserData()
to public async void GetUserData()
method in the MainPageViewModel.cs
, you can call it in the onappearing method like following background code in MainPage . when you back to the mainpage, OnAppearing
will execute again. judge the current viewmodel and get it, we can execute GetUserData
to get the latest data.
public partial class MainPage : ContentPage {
public MainPage() {
InitializeComponent();
}
override protected void OnAppearing() {
if(BindingContext is MainPageViewModel vm){
//when you back to the mainpage, you can update the profile image.
vm.GetUserData();
}
}
}
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.