locked
Dynamically change an Image.Source property RRS feed

  • Question

  • User288477 posted

    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?

    Monday, July 17, 2017 5:26 PM

Answers

  • User180523 posted

    Null bindings don't work well, or the way one would expect. Normally they just stop being evaluated. I've raised Bugzilla's about it - oh - a year ago. No progress.

    Instead you might consider having a solid value to fall back on. Maybe an image that looks like a placeholder for [no image]

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, July 17, 2017 5:29 PM

All replies

  • User180523 posted

    Null bindings don't work well, or the way one would expect. Normally they just stop being evaluated. I've raised Bugzilla's about it - oh - a year ago. No progress.

    Instead you might consider having a solid value to fall back on. Maybe an image that looks like a placeholder for [no image]

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, July 17, 2017 5:29 PM
  • User288477 posted

    @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.

    Monday, July 17, 2017 5:40 PM
  • User318788 posted

    @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

    Tuesday, July 18, 2017 4:25 AM
  • User288477 posted

    @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!

    Thursday, July 20, 2017 12:04 AM
  • User318788 posted

    @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

    Thursday, July 20, 2017 4:10 AM
  • User288477 posted

    @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?

    Sunday, July 23, 2017 11:35 PM
  • User318788 posted

    @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

    Monday, July 24, 2017 4:46 AM