Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从MVVM中的viewModel绑定到视图_C#_Xamarin.forms_Mvvm - Fatal编程技术网

C# 从MVVM中的viewModel绑定到视图

C# 从MVVM中的viewModel绑定到视图,c#,xamarin.forms,mvvm,C#,Xamarin.forms,Mvvm,我正在处理contentView中的stacklayout,它绑定到保存一些API数据的ViewModel。但是,由于API只返回两(2)项,堆栈出现两(2)次。但是细节没有显示在视图上。 我试过“this.bindingContext”和“bindingContext”。没有人在工作 这是我的模型 using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Text; namespac

我正在处理contentView中的stacklayout,它绑定到保存一些API数据的ViewModel。但是,由于API只返回两(2)项,堆栈出现两(2)次。但是细节没有显示在视图上。 我试过“this.bindingContext”和“bindingContext”。没有人在工作

这是我的模型

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace AstraPolarisApp.Models
{
    // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    public class AccountDetails
    {
        [JsonProperty("accountNumber")]
        public string AccountNumber { get; set; }

        [JsonProperty("accountName")]
        public string AccountName { get; set; }
        public string phoneNumber { get; set; }
        public string email { get; set; }

        [JsonProperty("accountType")]
        public string AccountType { get; set; }
    }

    public class AcctUserDetails
    {
        public string userId { get; set; }
        [JsonProperty("name")]
        public string Name { get; set; }
        public string email { get; set; }
        public string phoneNumber { get; set; }
    }

    public class LinkedBy
    {
        public string linkedById { get; set; }
        public string name { get; set; }
        public string email { get; set; }
    }

    public class MyAccount
    {
        public string linkerId { get; set; }
        public AccountDetails accountDetails { get; set; }
        public AcctUserDetails acctUserDetails { get; set; }
        public LinkedBy linkedBy { get; set; }
        public string requestTimestamp { get; set; }
    }

    public class Account
    {
        [JsonProperty("payload")]
        public List<MyAccount> Accounts { get; set; }
    }


}


AccountType
AccountNumber
AccountDetails
的属性,但您要绑定到
MyAccount
的集合

编辑:

AccountDetails
已经是
MyAccount
的成员,所以您只需像其他任何东西一样绑定到它:

<Label Text="{Binding accountDetails.AccountType}" 
       x:Name="AcctLbl" FontSize="13" TextColor="#E7E8F0" HorizontalOptions="Start"/>

<Label Text="{Binding accountDetails.AccountNumber}"
       x:Name="AcctNo" TextColor="#E7E8F0" FontSize="8" HorizontalOptions="Start"/>


ps.
accountDetails
应该以大写字母开头,因为它是一个属性。我很感激它会与类名冲突,但你可以称它为
详细信息
或其他什么,因为它已经是MyAccount的成员,所以你知道它与accounts有关。

基于@GazTheDestroyer的回复,我能够找到我的答案。因此,在使用了他给出的方法并意识到它不适合我之后,我决定在绑定中添加“path”。 这是最后一份

<Label Text="{Binding Path=Details.AccountType}" 
   x:Name="AcctLbl" FontSize="13" TextColor="#E7E8F0" HorizontalOptions="Start"/>


可以尝试使用or!?如何绑定到AccountDetails?我尝试的方法显示错误:“无法从myAccount转换为AccountDetails”
using AstraPolarisApp.PopUps;
using AstraPolarisApp.ViewModels;
using Rg.Plugins.Popup.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace AstraPolarisApp.Utils
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class OverlayView : ContentView
    {
        public OverlayView()
        {
            InitializeComponent();
            //BindingContext = new TransactionViewModel();
            this.BindingContext = new AccountsViewModel();
        }

    }
}
<Label Text="{Binding accountDetails.AccountType}" 
       x:Name="AcctLbl" FontSize="13" TextColor="#E7E8F0" HorizontalOptions="Start"/>

<Label Text="{Binding accountDetails.AccountNumber}"
       x:Name="AcctNo" TextColor="#E7E8F0" FontSize="8" HorizontalOptions="Start"/>
<Label Text="{Binding Path=Details.AccountType}" 
   x:Name="AcctLbl" FontSize="13" TextColor="#E7E8F0" HorizontalOptions="Start"/>