How to set Avatar image with OnpropertyChanged()

I have a property where I get image for avatar from external server.

      private ImageSource avatarImg = "noimg.png";

        public ImageSource AvatarImg
        {
            get { return avatarImg; }
            set
            {
                avatarImg = value;
                OnPropertyChanged();
            }
        }

I tried to set avatar in message like this:

 this.sfMessage.Add(new TextMessage()
                                {
                                    Author = new Author { Name = mess.Name , Avatar = AvatarImg },
                                    Text = mess.Message,
                                });

but after image is downloaded, Avatar near message in chat does not change.

8 Replies 1 reply marked as answer

KK Karthikraja Kalaimani Syncfusion Team March 29, 2021 01:12 PM UTC

Hi Nikola,

Can you please reproduce the issue in simple sample and revert us back with clear replication procedure? because it will help us to provide a better solution asap.

Regards,
Karthik Raja


 



NI Nikola March 29, 2021 01:25 PM UTC

Hi Karthik,

im not sure how to create simple sample. I just want to set  the Avatar image of incoming message. 
Avatar image first should be downloaded from server and then displayed. 
Like I said, I have already property ImageSource which works well (I added Image view on the same page with same source and it works but on Avatar image it does not change after it is downloaded). 


KK Karthikraja Kalaimani Syncfusion Team March 30, 2021 09:54 AM UTC

Hi Nikola,

We don’t know how you are fetching the image from server and converting to image source. So could you please let us know type of the image which you are fetching the image from the server. Also, can you please check by changing the image with Xamarin forms image instead of  SfChat Avatar and let us know the details ?

Regards,
Karthik Raja
 



NI Nikola March 30, 2021 10:59 AM UTC

Here it is how I get PNG image from server using image path:

public async Task<byte[]> DownloadPhotos(string imagepath)
        {
            
            HttpClient client = new HttpClient(new NativeMessageHandler());

            
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Bearer", SettingsForApp.AccessToken);
            var stream = await client.GetByteArrayAsync(Constants.BaseApiAddress + "exercise/getimgs?imagepath=" + imagepath);

            return stream;
        }

then I convert result from above in ImageSource with this:


        public  ImageSource BytesToImageSource(byte[] bytes)
        {
            return ImageSource.FromStream(() => new MemoryStream(bytes));
        }

I dont understand how to change Xamarin forms image instead SF chat avatar?


KK Karthikraja Kalaimani Syncfusion Team March 31, 2021 01:53 PM UTC

Hi Nikola,

Thanks for the update. Currently, we are trying to reproduce the issue in simple sample. So, we will update the details on or before 5th April 2021. In the mean time can you please load the image using ImageSource.FromURI(uri) to Avatar. Please refer to the below code snippet.

Code snippet :

 
  this.messages.Add(new TextMessage() 
            { 
                Author = new Author() { Name = "Margaret", Avatar = ImageSource.FromUri(new Uri("http://lorempixel.com/1920/1080/sports/7/")) }, 
                Text = "What kind of application is it and when are we going to launch?", 
            }); 

Also, please check the behavior in Xamarin forms image instead of SfChat control. Since we used Xamarin Forms image as Avatar in SfChat control.

Regards,
Karthik Raja
 



NI Nikola April 2, 2021 07:23 AM UTC

Hi,

with ImageSource.FromUri it works, but its not adequate for me because I have to download photo from asp net core using authorized httpget request.
Using this request on same page with ffimageloading works fine, image displayed after its downloaded, but on avatar does not.


NI Nikola April 2, 2021 12:25 PM UTC

I finally managed to solve this on my own.

I created property for incoming user:

 public Author senderUser;

 public Author SenderUser
           {
            get
            {
                return this.senderUser;
            }
            set
            {
                this.senderUser = value;
                OnPropertyChanged();
            }
        }

In each incoming message set Author = SenderUser; , and after line of code where image is downloaded in AvatarImg propery I call

this.SenderUser.Avatar = AvatarImg 

And finally it works and update Avatar image!



Marked as answer

KK Karthikraja Kalaimani Syncfusion Team April 5, 2021 07:42 AM UTC

Hi Nikola,

We glad to know that the reported issue has been resolved at your end. Please let us know if you need further assistance on this.

Regards,
Karthik Raja
 


Loader.
Up arrow icon