How to handle empty Uri to bind path to image source

how can i initialize the Uri object in empty state.

First thought would be to do something like this

//does not work
Uri myUri = Uri.Empty

      

This one doesn't work either

Uri myUri = new Uri(string.Empty)

      

The reason is that I am storing my image path in the db, and if there is no image, I have a clean database entry.

I know there are ways to handle this with a converter and return an empty Uri image by default in case the image path is empty ...

How do I best bind the binding to my ViewModel Uri?

Is the converter a way to go and how do you handle it if you have many different default images to be returned ...

+4


source to share


4 answers


Another way to deal with this is you can change your data query, for example if you are using sql employees table then you can write as below.

select Employeeid, FirstName, LastName, 
case when ImageUrl is null then 'http://www.codeodor.com/images/Empty_set.png' else ImageUrl end as ImageUrl from employees

      



this will handle null and return the default url for an empty image;

hope this suits you.

0


source


You can do something like this



Uri uri = null;
bool someValue = Context.TryGetUri(entity, out uri);

      

0


source


I have the same problem, and on thought, I think System.Uri

there is no default constructor for that because it doesn't make sense. Indeed, it URI

identifies a resource, so if the resource does not exist or cannot be located, I think the best approach is to use a reference null

instead of an empty one URI

.

In your code, instead of (pseudocode):

if (myUri != Uri.Empty)
{
    // do something
}

      

you will have:

if (myUri != null)
{
   // do something
}

      

In this case, you can also use the Null-conditional operator if it suits your workflow.

0


source


Just install:

new Uri("about:blank");

      

Works for me.

0


source







All Articles