is possible this wrote simple, code is here:
foreach (var friend in friends)
{
friend.Value.blockQuote = GetBlockQuote(friend.Value.nick);
if (friend.Value.photo == "0")
{
if (friend.Value.sex == 1)
{
var img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(@"avatars\man.jpg",
UriKind.Relative);
img.EndInit();
friend.Value.profilePhoto = img;
}
if (friend.Value.sex == 2)
{
//da default
var img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(@"avatars\woman.jpg",
UriKind.Relative);
img.EndInit();
friend.Value.profilePhoto = img;
}
}
else
{
var img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(friend.Value.photo.Replace(@"\", "").Replace(@"s_", ""), UriKind.Absolute);
img.EndInit();
friend.Value.profilePhoto = img;
}
}
Break out the uri-setting part
foreach (var friend in friends)
{
friend.Value.blockQuote = GetBlockQuote(friend.Value.nick);
Uri uri;
if (friend.Value.photo == "0")
{
if (friend.Value.sex == 1)
{
uri = new Uri(@"avatars\man.jpg", UriKind.Relative);
}
else if (friend.Value.sex == 2)
{
//da default
uri = new Uri(@"avatars\woman.jpg", UriKind.Relative);
}
else
{
uri = null; // insert error handling here
}
}
else
{
uri = new Uri(friend.Value.photo.Replace(@"\", "").Replace(@"s_", ""), UriKind.Absolute);
}
var img = new BitmapImage();
img.BeginInit();
img.UriSource = uri;
img.EndInit();
friend.Value.profilePhoto = img;
}
Edit
Note that the if-else-part is now a good candidate for Refactor->Extract method
You could factor out the lines
var img = new BitmapImage();
img.BeginInit();
and
img.EndInit();
friend.Value.profilePhoto = img;
by putting them before (for the former) and after (for the latter) the if
/ else
block.
foreach (var friend in friends)
{
friend.Value.blockQuote = GetBlockQuote(friend.Value.nick);
var img = new BitmapImage();
img.BeginInit();
if (friend.Value.photo == "0")
{
if (friend.Value.sex == 1)
{
img.UriSource = new Uri(@"avatars\man.jpg",
}
if (friend.Value.sex == 2)
{
img.UriSource = new Uri(@"avatars\woman.jpg",
UriKind.Relative);
}
}
else
{
img.UriSource = new Uri(friend.Value.photo.Replace(@"\", "").Replace(@"s_", ""), UriKind.Absolute);
}
img.EndInit();
friend.Value.profilePhoto = img;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.