Xamarin ImageSource in ListView not updating - xamarin

I have a ListView that contains images, the images are fetched from a server. The first API call I make gets the data in var people. I load the listview with a placeholder image, then run the 2nd API call to get the images for each item in the listview. I receive a byte[] as the image, and I convert it to an ImageSource. I have a search button at the top of the page that I set binding to TempImage which uses the byte[] as its source, and it changes to the images that are loaded. So the conversion of byte[] to ImageSource is fine. The initial setting of p.PictureImageSource = "name_circle.png" also works correctly. However, setting p.PictureImageSource to the converted byte[] does NOT work. It never changes from the initial "name_circle.png". Any ideas?
var people = peopleModel.Response;
if(people.Count == 0)
{
ShowNoResults = true;
}
else
{
ShowNoResults = false;
Results = peopleModel.Response;
foreach (PersonViewModel p in Results)
{
p.Initials = p.FirstName[0].ToString() + p.LastName[0];
p.PictureImageSource = "name_circle.png";
}
}
//must do 2 seperate loops so the initials load before going on with 2nd search
foreach (PersonViewModel p in Results)
{
IsBusy = false;
var peopleImage = await peopleService.GetPersonImage("p.Email");
if ((peopleImage.Error == null) && (peopleImage.Response != null))
{
p.Picture = peopleImage.Response;
byte[] imageAsBytes = (byte[])peopleImage.Response;
p.PictureImageSource = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
TempImage = ImageSource.FromStream(() => new MemoryStream(imageAsBytes));
}
}
OnPropertyChanged();
-
public class PersonViewModel : INotifyPropertyChanged
{
public WorkstationViewModel WorkstationDetail { get; set; }
public List<PointViewModel> Points { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public string Initials { get; set; }
public string Email { get; set; }
public string ID { get; set; }
public string Department { get; set; }
public string BuildingName { get; set; }
public string SiteID { get; set; }
public string BuildingID { get; set; }
public string FloorNumber { get; set; }
public string FloorID { get; set; }
public string Workstation { get; set; }
public string Title { get; set; }
public string Phone { get; set; }
public byte[] Picture { get; set; }
public ImageSource PictureImageSource { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}

Apineda was correct in thinking the listview wasn't refreshing at all with the new data. I separated the two API calls into two different methods (not sure if this part is entirely necessary). After the completion of the 2nd call, I set the listview's ItemSource to null, then back to the Results value, forcing a refreshing. Now the images show.
searchGestureRecognizer.Tapped += async (s, e) => {
await _viewModel.GetResults();
await _viewModel.GetImagesForResults();
resultsListView.ItemsSource = null;
resultsListView.ItemsSource = _viewModel.Results;
};

Related

InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List to System.Collections.Generic.IEnumerable

I am trying to implementation clean architecture in netcore and I have Runtime Error
InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List to System.Collections.Generic.IEnumerable
In the WebUI I have Match controller and ViewAllMatch Action like this
public async Task<IActionResult> ViewAllMatch()
{
var matches = await _mediator.Send(new GetMatchesDetail());
return View(matches);
}
In the application Layer I have A queries like this:
public class GetMatchesDetail : IRequest<IEnumerable<MatchesDetail>>
{
}
public class MatchesDetail
{
public string MatchId { get; set; }
public int MatchNumer { get; set; }
public DateTime DateMatch { get; set; }
public TimeSpan TimeMatch { get; set; }
public int MatchYear { get; set; }
public string SeasonId { get; set; }
public string Round { get; set; }
/// <summary>
/// Set value to Qualified for Qualified and Final for Final Round
/// </summary>
public string Stage { get; set; }
public string SubStage { get; set; }
public string HTeam { get; set; }
public string HTeamCode { get; set; } //For Flag get from Table Team from Foreign Key TeamName
public int HGoal { get; set; }
public int GGoal { get; set; }
public string GTeam { get; set; }
public string GTeamCode { get; set; }
public string WinNote { get; set; }
public string Stadium { get; set; }
public string Referee { get; set; }
public long Visistors { get; set; }
public string Status { get; set; }
}
public class GetMatchesHandler : IRequestHandler<GetMatchesDetail, IEnumerable<MatchesDetail>>
{
private readonly IMatchRepository _matchRepository;
public GetMatchesHandler(IMatchRepository matchRepository)
{
_matchRepository = matchRepository;
}
public async Task<IEnumerable<MatchesDetail>> Handle(GetMatchesDetail request, CancellationToken cancellationToken)
{
var matchlistview = await _matchRepository.GetMatchDetailAsync();
return matchlistview;
}
}
And the code for matchRepository to get all the match in Infastructure like this.
public async Task<IEnumerable<MatchesDetail>> GetMatchDetailAsync()
{
var matchDetailList = (from match in _context.Matches
join team1 in _context.Teams on match.HTeam equals team1.TeamName
join team2 in _context.Teams on match.GTeam equals team2.TeamName
join season in _context.Seasons on match.SeasonId equals season.SeasonId
select new
{
match.MatchId,
match.MatchNumber,
match.DateMatch,
match.TimeMatch,
match.MatchYear,
match.SeasonId,
season.SeasonName,
match.Round,
match.Stage,
match.SubStage,
match.HTeam,
HTeamCode = team1.TeamCode,
match.HGoal,
match.GGoal,
match.GTeam,
GTeamCode = team2.TeamCode,
match.WinNote,
match.Stadium,
match.Referee,
match.Visistors
});
return (IEnumerable<MatchesDetail>)await matchDetailList.ToListAsync();
}
Full code have been upload to Github at https://github.com/nguyentuananh921/Betting.git.
for more detail.
Thanks for your help.
I am so confuse about model in clean architech when i have more entities and the model I want to view in the WebUI contain many entities in domain.
Thanks for your help.
I have Modify public IEnumerable GetMatchDetailAsync() like that.
public IEnumerable<MatchesDetail> GetMatchDetailAsync()
{
#region TryOther way
var matchQuery = (from match in _context.Matches
join team1 in _context.Teams on match.HTeam equals team1.TeamName
join team2 in _context.Teams on match.GTeam equals team2.TeamName
join season in _context.Seasons on match.SeasonId equals season.SeasonId
select new
{
#region selectResult
//Remove to clear Select what I want to get.
#endregion
});
MatchesDetail matchesDetail = new MatchesDetail();
List<MatchesDetail> retList = new List<MatchesDetail>();
//IEnumerable<MatchesDetail> retList;
foreach (var item in matchQuery)
{
#region ManualMapping
matchesDetail.MatchId = item.MatchId;
//other field mapping
#endregion
retList.Add(matchesDetail);
}
#endregion
return retList;
}
And it work

Xamarin - How to display en Image from an Object property

In xamarin I have a class where one of the property is an "IMAGE type".
I got this image from one Cloud API I developed myself.
I want now to display this image through a ListView and ImageCell. I'm wondering if there is is a way to bind my "Image" from my object directly thanks to the "ImageSource".
The class is declared the following(indeed the image is created from Byte[]:
public class Item
{
public int ItemId { get; set; }
public string Name { get; set; }
public decimal UnitPrice { get; set; }
public string Description { get; set; }
public string Brand { get; set; }
public bool HasSize { get; set; }
//public virtual List<SizedMeal> AvailableSizes { get; set; }
public bool CanBeSalt { get; set; }
public bool CanBeHotNotCold { get; set; }
public bool CanHaveMeat { get; set; }
public bool CanHaveSauce { get; set; }
public Image Image { get; set; }
}
Where IMAGE is comming from using Xamarin.Forms
and the object is constructed the following way:
Image image = new Image();
Stream stream = new MemoryStream(item.Image);
image.Source = ImageSource.FromStream(() => { return stream; });
ImageSource imageSource = image.Source;
resto.Menu.ItemList.Add(new Item
{
Name = item.Name,
Brand = item.Brand,
Image = image,
UnitPrice = item.UnitPrice,
Description = item.Description,
HasSize = item.HasSize,
ItemId = item.ItemId,
});
Thanks,

aspnetboilerplate IPerWebRequestDependency dont work

C#
public interface IUserInfoOutput: IPerWebRequestDependency
{
string Id { get; set; }
string UserName { get; set; }
string Job { get; set; }
string Sex { get; set; }
}
public class UserInfoOutput : IUserInfoOutput, IPerWebRequestDependency
{
public UserInfoOutput()
{
Id = Guid.NewGuid().ToString();
}
public string Id { get; set; }
public string UserName { get; set; }
public string Job { get; set; }
public string Sex { get; set; }
}
I dont register IUserInfoOutput explicitly because I know ABP will do it in web Project . However,I found it resovled different instance .
note that us and us2 is different . Is anything wrong with my code?or anything else to do ?
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
// var cookie = HttpContext.Request.Cookies["BLUserAcct"];
// var user = await _service.GetUserInfo(cookie);
var us = IocManager.Instance.Resolve<IUserInfoOutput>();
var us2 = IocManager.Instance.Resolve<IUserInfoOutput>();
await base.OnActionExecutionAsync(context, next);
}

How to update the entity in aspnetboilerplate?

I want to update an entity in database. I use the aspnetboilerplate template project. I have a method UpdateAsset in the application layer:
public async Task UpdateAsset(UpdateAssetInput input)
{
var asset = ObjectMapper.Map<Asset>(input.Asset);
asset.Domain = asset.Domain.ToLowerInvariant();
// Update Twitter Id
var twitterName = input.Asset.SocialAccounts?.TwitterInfo?.Name;
if (twitterName != null)
{
var twitterId = await _twitterActivityManager.GetTwitterIdByTwitterName(twitterName);
if (twitterId != null)
{
input.Asset.SocialAccounts.TwitterInfo.Id = twitterId;
}
}
asset.SetData<SocialAccounts>(AssetExtensionData.SocialAccounts, input.Asset.SocialAccounts);
var connectedAsset = await _assetManager.GetAsset(input.Asset.LockedPositionInfo.ConnectedAssetId);
if (connectedAsset != null)
{
input.Asset.LockedPositionInfo.ConnectedAssetUnit = connectedAsset.Unit;
}
asset.SetData<LockedPositionInfo>(AssetExtensionData.LockedPositionInfo, input.Asset.LockedPositionInfo);
asset.SetData(AssetExtensionData.WithdrawalApiInfo, input.Asset.WithdrawalApiInfo);
await _assetManager.UpdateAsset(asset);
}
UpdateAssetInput:
public class UpdateAssetInput
{
public AssetDto Asset { get; set; }
}
AssetDto:
[AutoMap(typeof(Asset))]
public class AssetDto : AuditedEntityDto<string>
{
public const int SYMBOL_LENGTH = 10;
[Required]
[MaxLength(SYMBOL_LENGTH)]
public new string Id { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public string Website { get; set; }
[Required]
public string Domain { get; set; }
public string Logo { get; set; }
public string Organization { get; set; }
public string Unit { get; set; }
public SocialAccounts SocialAccounts { get; set; }
public LockedPositionInfo LockedPositionInfo { get; set; }
public WithdrawalApiInfo WithdrawalApiInfo { get; set; }
public decimal TotalAmount { get; set; }
public decimal Price { get; set; }
public bool IsDisable { get; set; } = false;
}
UpdateAsset in the AssetManager:
public async Task UpdateAsset(Asset asset)
{
try
{
await _assetRepository.UpdateAsync(asset);
}
catch (Exception e)
{
Logger.Error(e.Message, e);
throw new UserFriendlyException(L("AssetUpdateFailed"), asset.Name);
}
}
When I call the UpdateAsset of the application layer in front end, I get the exception:
System.InvalidOperationException: 'The instance of entity type 'Asset' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
So how to solve the problem ?
Normally, when we update a asset entity, we will do following 3 steps:
Get asset entity from db
set updated value to asset entity
Update asset entity
based on your error message, you may get asset entity from db twice
Get asset1 entity from db - tracked
Get asset2 entity from db - tracked
set updated value to asset2 entity
Update asset2 entity
Please check if you have above code snippet

Object reference error in unit testing

i have a controller action
[HttpPost]
public ActionResult CreateFocus(FocusFormModel focus)
{
var errors = focusService.CanAddFocus(Mapper.Map<FocusFormModel, Focus>(focus)).ToList();
ModelState.AddModelErrors(errors);
if (ModelState.IsValid)
{
focusService.CreateFocus(Mapper.Map<FocusFormModel, Focus>(focus));
var createdfocus = focusService.GetFocus(focus.FocusName);
return RedirectToAction("Focus", new { id = createdfocus.FocusId });
}
return View("CreateFocus", focus);
}
This action is working fine but when i am writing unit test it is showing error
Object Reference Not set to an instance of an object
the uni test is
[Test]
public void Create_Focus()
{
// Arrange
GroupController controller = new GroupController(groupService);
// Act
FocusFormModel focus = new FocusFormModel();
focus.GroupId = 1;
focus.FocusName = "t";
focus.Description = "t";
Mapper.CreateMap<FocusFormModel, Focus>().ForAllMembers(opt => opt.Ignore());
Mapper.AssertConfigurationIsValid();
var result = (RedirectToRouteResult)controller.CreateFocus(focus);
Assert.AreEqual("Index", result.RouteValues["action"]);
}
My Model is described below
public class Focus
{
public int FocusId { get; set; }
[StringLength(50)]
public string FocusName { get; set; }
[StringLength(100)]
public string Description { get; set; }
public int GroupId { get; set; }
public virtual Group Group { get; set; }
public DateTime CreatedDate { get; set; }
public Focus()
{
CreatedDate = DateTime.Now;
}
}
and i am writing my view model as
public class FocusFormModel
{
public int FocusId { get; set; }
[Required(ErrorMessage = "*")]
[StringLength(50)]
public string FocusName { get; set; }
[Required(ErrorMessage = "*")]
[StringLength(100)]
public string Description { get; set; }
public int GroupId { get; set; }
public virtual Group Group { get; set; }
public DateTime CreatedDate { get; set; }
public FocusFormModel()
{
CreatedDate = DateTime.Now;
}
}
Can any one please help me
This line actually provides 2 chances to get null reference exception:
var errors = focusService.CanAddFocus(Mapper.Map<FocusFormModel, Focus>(focus)).ToList();
first of all focusService might be null
if focusService is not null then method call CanAddFocus(Mapper.Map<FocusFormModel, Focus>(focus)) might return null
That are just assumptions.
As Greg Smith said stack trace would be very helpful here.

Resources