-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


Xamarin.Forms: !. 2

, 21 2017 . 12:12 +


, , . - , , ViewModel . , , .
. , , , , ViewModel , - .

: , . , , , , , , , . .



, . , , , . . ViewModel , . singleton, DI . , , , DI , , . . , , . . .

.
, API :
  • (api/catalogue/search)
  • SKU (api/catalogue/materials)
  • SKU (api/calatogue/price)
  • SKU (api/catalogue/remains)
  • (api/cart/add)
  • (api/cart/remove)


, , , . , .
, API . , , ( - ). , , , , , . , , , . . Mobile Dimension, .
, , , .

, , , , ( ). , , , , . , , , . , , .

, , :

public event Action OnProductAddedSuccessfully;
public event Action OnProductAddedFailure;
 
 
public void StartAddingProduct(string sku)
{
    var newProduct = new BasketProduct() { Sku = sku, State = Enums.RequestState.InProgress };
    //       
    _products.Add(newProduct);
    //   ,  ,    ...
    var tiket = _basketRepository.AddToBasket(sku);
    tiket.OnSuccess += (response) =>
    {
        //  ,          
        if(response.Data.Succseeded != null && response.Data.Succseeded.Value)
        {
            //        ,      (positionId)
            newProduct.PositionId = response.Data.PositionId;
            newProduct.State = Enums.RequestState.Succseeded;
            //  (),    
            OnProductAddedSuccessfully?.Invoke(newProduct.Sku, newProduct.PositionId);
        }
        else
        {
            // ,     
            OnProductAddedFailure?.Invoke(sku);
            newProduct.State = Enums.RequestState.Failed;
        }
    };
    //   ,    (     )
    var priceTicket = _catalogRepository.GetPriceTicket(sku);
    priceTicket.OnSuccess += (response) =>
    {
        if(response.Data != null){
            //   
            newProduct.Price = response.Data.Price;
        }
    };
}



, event, , - ( , , ). , 3 , , , . .

ViewModel , . , view . :

public int? TotalCount
{
    get
    {
        return _basketService.TotalCount;
    }
}
 
 
public int? TotalPrice
{
    get
    {
        return _basketService.TotalPrice;
    }
}
void _basketService_OnProductAddedSuccessfully(string sku, string positionId)
{
    var product = Products.ToList().FirstOrDefault(x => x.Sku == sku);
    product.CountInBasket++;
    product.IsAddingInProfress = false;
    product.PositionIds.Add(positionId);
    //     ,     ,     - 
    RaizePropertyChanged(nameof(TotalCount), nameof(TotalPrice));
}



C ViewModel, , , . . ViewModel, , .



. , - NRE ViewModel. Visual Studio , callback . - , call-stack, .

image image

GitHub.

, . , , , NuGet . .
Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/331196/


: [1] []
 

:
: 

: ( )

:

  URL