Images from a variable not loading/displaying

SmartmanAppsSmartmanApps AUMember ✭✭✭
edited April 26 in General

Not sure where to stick this, as not even sure why this isn't working (i.e. if it's Forms, or VS - which had been playing up right before I discovered this - or UWP, or...), so I'll stick this under General.

Working on something, things were just not working (for reasons that turned out to be nothing to do with my code, but it's led to a discovery),so I decided to delete and reclone. Having done that, I went through making sure everything was now working, only to find it wasn't. My single image display from an image search, which had previously been working (at this last commit), now wasn't. After some detective work, I eventually found that...

this (still) works - ResultsImage="https://developer.xamarin.com/demo/IMG_1415.jpg";
this doesn't - ResultsImage=imageResults.Value.ElementAt(0).ContentUrl; (but had been working previously)

note: imageResults is a Bing image search ImageObject. ResultsImage is a source string property which has been bound to an image in my view.

i.e. if I hardcode an image source in, it works, but if I try to load it from a variable it doesn't. I even tried adding a .tostring() at the end just to be doubly-sure, but that doesn't work either. I suspect an unfortunate co-incidence that this (whatever this is) broke just when I changed from loading single images to trying a ListView (so all this time I couldn't get ListView working, was actually that my images were no longer loading from a variable to begin with). BTW ContentUrl is definitely valid, as if I copy+paste it into a browser the image comes up. Just won't load within the app for some reason (well, the image from it isn't displaying anyhow).

So, anyone have any idea what is going on here and how to fix it?

thanks,
Donald.

Answers

  • yousufctecyousufctec USMember ✭✭✭

    Try imageResults.value().get(0).contentUrl()

    Regards,
    Yousuf

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    @yousufctec said:
    Try imageResults.value().get(0).contentUrl()

    Regards,
    Yousuf

    Thanks for the reply, but there is no get method under value (only getenumerator, gethashcode or gettype). Is that maybe part of an extension I need to add?

  • yousufctecyousufctec USMember ✭✭✭

    Can you elaborate your issue by providing some code samples with the nuget(s) you're using?

    Thanks!

    Regards,
    Yousuf.

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    No problem...

    VIEW

    Image ResultsPicture;
    SearchBar LookupBar;
    viewmodel vm=new viewmodel();
    ResultsPicture=new Image{};
    LookupBar=new SearchBar{
    Placeholder="To search click here then type search term or use hand-held barcode scanner"
    };
    ResultsPicture.SetBinding( Image.SourceProperty,new Binding("ResultsImage",source: vm) );
    LookupBar.SetBinding( SearchBar.TextProperty,new Binding("SearchTerm",source:vm) );
    LookupBar.SearchCommand = vm.LookupCommand;

    VIEWMODEL (insert your own AzureKey)

    in constructor - LookupCommand=new Command(LookupImage);

    private string _resultsImage;
    public string ResultsImage
    {
    get { return _resultsImage; }
    set {
    if (_resultsImage!=value){
    _resultsImage=value;
    PropertyChanged?.Invoke(this,new PropertyChangedEventArgs("ResultsImage"));
    }
    }
    }

    private string _searchTerm;
    public string SearchTerm
    {
    get { return _searchTerm; }
    set {
    if (_searchTerm!=value) {
    _searchTerm=value;
    PropertyChanged?.Invoke(this,new PropertyChangedEventArgs("SearchTerm"));
    }
    }
    }

    private void LookupImage()
    {
    string searchterm=SearchTerm;
    using(var client=new ImageSearchClient(new ApiKeyServiceClientCredentials(AzureKey))){
    client.Endpoint="https://api.cognitive.microsoft.com/";
    imageResults=client.Images.SearchAsync(query: SearchTerm).Result;
    if (!imageResults.TotalEstimatedMatches.Equals(null)) {
    // ResultsImage="https://developer.xamarin.com/demo/IMG_1415.jpg"; // THIS LINE WORKS (when uncommented)
    ResultsImage=imageResults.Value.ElementAt(0).ContentUrl; // THIS LINE USED TO WORK,BUT NOT NOW
    } else {
    ResultsText=string.Format("*** No results found for {0} ***",searchterm);
    }
    }

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    Oops! Plus you need this nuget - Microsoft.Azure.CognitiveServices.Search.ImageSearch

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭

    Is your subscription key still valid? The trial ones expire after 7 days.

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    @DirkWilhelm said:
    Is your subscription key still valid? The trial ones expire after 7 days.

    Yes. I'm still getting images, they're just not displaying if I try to load them from the URL.

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    Well, this is weird. Back on the laptop I've been working on, and it's working again! Haven't installed any updates. Will have to see tonight if it's working on my PC as well, but at the moment it's looking like whatever the problem was has resolved itself. Good, but frustrating - has been broken for a week.

  • SmartmanAppsSmartmanApps AUMember ✭✭✭

    So yeah, it's working on my PC as well now. First suspect was a Windows update, but nothing has happened there in over 3 weeks. Haven't had any VS updates (one of the first things I did when it broke was install updates in case it fixed it). I'll leave this as unanswered for now, in case someone sees it and knows what happened in the background in the last week of April to cause this, for future reference, because I still don't know what broke it or caused it to be fixed again. :-\

Sign In or Register to comment.