本文介绍了如何在 Xamarin 中获取绑定之外的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个产品页面,我执行导致产品详细信息页面的命令

I have a Product page I execute the command which leads to the Product Detail page

...    
{
   ProductTappedView = new Command<ProductInfo>(OnViewDetailProduct);
}
private async void OnViewDetailProduct(ProductInfo prod)
{
   await Navigation.PushAsync(new DetailProduct(prod));
}

我有一个产品详情页面.这部分一切正常.

I have a product detail page. Everything works fine in this part.

DetailProduct.xaml.cs

DetailProduct.xaml.cs

public partial class DetailProduct : ContentPage
{
    public ProductInfo ProductInfo { get; set; }
    public Customer Customer { get; set; }
    public DetailProduct()
    {            
        //InitializeComponent();
        //NavigationPage.SetHasNavigationBar(this, true);
        //BindingContext = new ViewProductViewModel();
    }
    public DetailProduct(ProductInfo productInfo)
    {
        InitializeComponent();
        NavigationPage.SetHasNavigationBar(this, true);
        BindingContext = new ViewProductViewModel();

        if (productInfo != null)
        {

            //get info customer
            string name = Customer.NameUs; -----------> **Report an error right here: Object reference not set to an instance of an object.'**

            //end get info customer

            //get info Product
            double countall = productInfo.AveragedID;
            //end get info Product

            ((ViewProductViewModel)BindingContext).ProductInfo = productInfo;
        }    
    }
}

ViewProductViewModel

ViewProductViewModel

public class ViewProductViewModel:BaseProductViewModel
{
    public double ScreenWidth { get; set; }
    public ViewProductViewModel()
    {
        var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
        ScreenWidth = mainDisplayInfo.Width / mainDisplayInfo.Density;


        ProductInfo = new ProductInfo();
        CustomerInfo = new Customer();
    }
}

BaseProductViewModel

BaseProductViewModel

public class BaseProductViewModel : INotifyPropertyChanged
{
    public INavigation Navigation { get; set; }
    private ProductInfo _productInfo;
    private Customer _customer;
    public ProductInfo ProductInfo
    {
        get { return _productInfo; }
        set { _productInfo = value;OnPropertyChanged(); }
    }

    public Customer CustomerInfo
    {
        get { return _customer; }
        set { _customer = value; OnPropertyChanged(); }
    }

    bool isBusy = false;
    public bool IsBusy
    {
        get { return isBusy; }
        set
        {
            SetProperty(ref isBusy, value);
        }
    }

    protected bool SetProperty<T>(ref T backingStore, T value, [CallerMemberName] string propertyName = "", Action onChanged = null)
    {
        if (EqualityComparer<T>.Default.Equals(backingStore, value))
            return false;
        backingStore = value;
        onChanged?.Invoke();
        OnPropertyChanged(propertyName);
        return true;
    }    


    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        var changed = PropertyChanged;
        if (changed == null)
            return;
        changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

但是在产品详细信息页面中,我想获得更多客户信息.

However in product detail page I want to get more Customer information.

//get info customer
string name = Customer.NameUs;
//end get info customer

客户(模型)

public class Customer
{
    public string NameUs { get; set; }
}

我怎样才能获得客户.例如:字符串名称 = Customer.NameUs????

How can I get Customer. eg: string name = Customer.NameUs????

请注意:我已获取产品详情,需要获取更多客户信息

Please note: I have obtained product details, I need to get more Customer information

谢谢

推荐答案

您正在页面构造函数中传递 ProductInfo - 大概您会对 Customer 做同样的事情>

you are passing ProductInfo in the page constructor - presumably you would do the same with Customer

public ProductInfo ProductInfo { get; set; }
public Customer Customer { get; set; }

public DetailProduct(ProductInfo productInfo, Customer customer)
{
    this.ProductInfo = productInfo;
    this.Customer = customer;

    ...
}

这篇关于如何在 Xamarin 中获取绑定之外的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 04:58