Forum Xamarin.Forms

Dynamically change an Image.Source property

Hello,

I am trying to reset an image in my xamarin.forms project upon a button click. I have a view model with an ImageSource property which the image.Source in xaml is bound to using Image Source="{Binding ImageSource}", when I initially go to this page the image is blank which is correct, I then take a picture and set the image.Source to the path of the picture and the picture appears which is also correct. However, when I click my submit button (to submit the photo to storage), I try to reset the image.Source = null but the picture doesn't reset, it just keeps the photo that was taken. Any thoughts?

Best Answer

Answers

  • CodySigvartsonCodySigvartson USMember ✭✭

    @ClintStLaurent That's interesting, it'd make a lot of sense for it to be that simple (setting it to null). I thought of your solution but I hate having hacks like that in my code, I guess for now it'll have to do. Thanks.

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @CodySigvartson ,

    I had same thing going on with my project, I used a 1px by 1px image to reset the ImageSource, which does the great job.
    Since you do not like Hacks, you might not consider this as an answer, however believe me, by doing this you are saving a good amount of memory when your app is in use.

    Hope this helps.

    Regards,
    N Baua

  • CodySigvartsonCodySigvartson USMember ✭✭

    @N_Baua Yes that seems like a good approach to the problem. What I actually ended up doing was setting image.Source = " "; and it seemed to work. What was weird was I was trying to do this in an async event handler telling me a submission to a web service had completed then I navigated to a new page and when I tried to do it in that event handler it wouldn't work. When I moved it outside the event handler it worked. It had me stumped on why that was the case but I just accepted it and moved on!

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @CodySigvartson ,

    O'm glad that the blank space is working for you, somehow I do not have good feelings about such stuff.
    If I order Latte, definitely I won't like Cappuccino served :smile: .

    If you're still able to commit the change with a single pixel transparent gif/png that would be more subtle in all manner.
    Kudos for sharing your experience here. Good luck.

    Regards,
    N Baua

  • CodySigvartsonCodySigvartson USMember ✭✭

    @N_Baua What exactly makes you uneasy about the blank space solution? I've tested it multiple times with success so it seems fine. Just wanted to hear your concerns about possible problems it may introduce, if any?

  • N_BauaN_Baua INMember ✭✭✭✭✭

    @CodySigvartson ,

    Not against of using any specific solution, it just happens many time we could not just notice " " in our code (unless you put comments on top, why this is being used. or your editor shows the non-visible markers like blank spaces and new lines.)

    This is totally unrelated, however for Example, I prefer.
    var someString = string.empty; over var someString="" or var someString=" ";

    And in the same way, I prefer explicit value assignment in markups, so nothing wrong with your approach if it requires string and you give them a " ", however if it requires ImageSource and you provide " " its a little concern.

    Hope I am not being rude here, just putting my point of view.
    Thanks

    N Baua

Sign In or Register to comment.