Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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在xamarin上绑定数据收集_C#_Xamarin_Mvvm_Xamarin.forms_Models - Fatal编程技术网

C# 使用mvvm在xamarin上绑定数据收集

C# 使用mvvm在xamarin上绑定数据收集,c#,xamarin,mvvm,xamarin.forms,models,C#,Xamarin,Mvvm,Xamarin.forms,Models,所以我开始学习xamarin并尝试从视图到模型的不同数据绑定方法。我使用post请求从服务中检索数据,在获得数据后,我无法将它们绑定到视图。经过大量研究,我找到了一些有趣的解决方案,并尝试了不同的方法。 这就是我迄今为止所取得的成就: 我的看法是: <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

所以我开始学习xamarin并尝试从视图到模型的不同数据绑定方法。我使用post请求从服务中检索数据,在获得数据后,我无法将它们绑定到视图。经过大量研究,我找到了一些有趣的解决方案,并尝试了不同的方法。 这就是我迄今为止所取得的成就: 我的看法是:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="Ideas.Pages.IdeasSinglePage"
         xmlns:vm="clr-namespace:Ideas.ViewModel;assembly=Ideas"
          Title="My idea">

<ContentPage.BindingContext>
    <vm:IdeasViewModel/>
</ContentPage.BindingContext>

<StackLayout>
    <Button Command="{Binding GetIdeasCommand}"
            Text="Bileta Ime"
            TextColor="White"
            FontSize="15"
            BackgroundColor="#29abe2"/>
    <Label Text="test"></Label>

    <ListView ItemsSource="{Binding Ideas}"
              HasUnevenRows="True">

        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Padding="20, 10">
                        <Label  Text="{Binding IdeasName}"
                                 FontSize="16"
                               TextColor="RoyalBlue"/>

                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

</StackLayout>

这是我的viewmodel

public class IdeasViewModel : INotifyPropertyChanged
{
    ApiServices _apiServices = new ApiServices();
    public List<Ideas> Ideas
    {
        get { return Ideas; }
        set
        {
            Ideas= value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public ICommand GetIdeasCommand
    {
        get
        {
            return new Command(async () =>
            {
                Ideas= await _apiServices.GetIdeasAsync();
            });
        }
    }


    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}
公共类IdeasViewModel:INotifyPropertyChanged
{
ApiServices_ApiServices=新的ApiServices();
公开列出想法
{
获取{返回想法;}
设置
{
观念=价值;
OnPropertyChanged();
}
}
公共事件属性更改事件处理程序属性更改;
公共ICOMAND GetIdeasCommand
{
得到
{
返回新命令(异步()=>
{
Ideas=wait_apiServices.GetIdeasAsync();
});
}
}
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
这是我的服务:

public async Task<List<Ideas>> GetIdeasAsync()
    {
        ListIdeasDetails ideas= null;
        try { 
            var client = new HttpClient();
            client.DefaultRequestHeaders.Add("parameter", "parameter");
            client.DefaultRequestHeaders.Add("parameter", parameter);

            HttpContent content = new StringContent("");
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var response = await client.PostAsync("https://heregoes/themethod", content);
            response.EnsureSuccessStatusCode();
            string json = await response.Content.ReadAsStringAsync();
            ideas= JsonConvert.DeserializeObject<ListIdeasDetails>(json);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message.ToString());
        }
        return ideas.Ideas;
    }
}
public异步任务GetIdeasAsync()
{
ListIdeasDetails=null;
试试{
var client=新的HttpClient();
client.DefaultRequestHeaders.Add(“参数”、“参数”);
client.DefaultRequestHeaders.Add(“参数”,参数);
HttpContent=新的StringContent(“”);
content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/json”);
var response=wait client.PostAsync(“https://heregoes/themethod“,内容);
response.EnsureSuccessStatusCode();
string json=wait response.Content.ReadAsStringAsync();
ideas=JsonConvert.DeserializeObject(json);
}
捕获(例外情况除外)
{
Debug.WriteLine(例如Message.ToString());
}
返回想法。想法;
}
}
这是我的两个模型:

public class Ideas
{

    public string IdeasID  { get; set; }

    public string IdeasName  { get; set; }
 }


class ListIdeasDetails
{
    public List<Ideas> Ideas{ get; set; }
    public string ExceptionMessage { get; set; }
    public bool HasException { get; set; }

}
公共课堂理念
{
公共字符串IdeasID{get;set;}
公共字符串IdeasName{get;set;}
}
类ListIdeasDetails
{
公共列表想法{get;set;}
公共字符串例外消息{get;set;}
公共bool有异常{get;set;}
}

我真的非常感谢你的帮助!谢谢

您的以下各项似乎有问题:

公开列出想法
{
获取{返回想法;}
设置
{
想法=价值;//无休止的循环
OnPropertyChanged();
}
}
通过以下方式添加支持字段:

List<Ideas> _ideas;
public List<Ideas> Ideas
{
    get { return _ideas; }
    set
    {
        if(value == _ideas) return;
        _ideas = value;
        OnPropertyChanged();
    }
}
List\u想法;
公开列出想法
{
获取{return\u ideas;}
设置
{
如果(值==\u)返回;
_观念=价值;
OnPropertyChanged();
}
}
我建议使用,以减少与INotifyPropertyChanged相关的锅炉板代码量。使用Fody,您的ViewModel看起来非常简单:

public class IdeasViewModel : INotifyPropertyChanged
{
    ApiServices _apiServices = new ApiServices();
    public List<Ideas> Ideas { get;set; }

    public event PropertyChangedEventHandler PropertyChanged;

    public ICommand GetIdeasCommand
    {
        get
        {
            return new Command(async () =>
            {
                Ideas= await _apiServices.GetIdeasAsync();
            });
        }
    }
}
公共类IdeasViewModel:INotifyPropertyChanged
{
ApiServices_ApiServices=新的ApiServices();
公共列表想法{get;set;}
公共事件属性更改事件处理程序属性更改;
公共ICOMAND GetIdeasCommand
{
得到
{
返回新命令(异步()=>
{
Ideas=wait_apiServices.GetIdeasAsync();
});
}
}
}
注:除了你的主要问题之外,我想指出你的代码中接下来不好的地方

  • 使用POST从WEB下载数据。
  • 类名“ApiServices”

  • 如果您需要进一步的帮助,您可以通过评论让我知道。

    似乎您对以下内容有问题:

    公开列出想法
    {
    获取{返回想法;}
    设置
    {
    想法=价值;//无休止的循环
    OnPropertyChanged();
    }
    }
    
    通过以下方式添加支持字段:

    List<Ideas> _ideas;
    public List<Ideas> Ideas
    {
        get { return _ideas; }
        set
        {
            if(value == _ideas) return;
            _ideas = value;
            OnPropertyChanged();
        }
    }
    
    List\u想法;
    公开列出想法
    {
    获取{return\u ideas;}
    设置
    {
    如果(值==\u)返回;
    _观念=价值;
    OnPropertyChanged();
    }
    }
    
    我建议使用,以减少与INotifyPropertyChanged相关的锅炉板代码量。使用Fody,您的ViewModel看起来非常简单:

    public class IdeasViewModel : INotifyPropertyChanged
    {
        ApiServices _apiServices = new ApiServices();
        public List<Ideas> Ideas { get;set; }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public ICommand GetIdeasCommand
        {
            get
            {
                return new Command(async () =>
                {
                    Ideas= await _apiServices.GetIdeasAsync();
                });
            }
        }
    }
    
    公共类IdeasViewModel:INotifyPropertyChanged
    {
    ApiServices_ApiServices=新的ApiServices();
    公共列表想法{get;set;}
    公共事件属性更改事件处理程序属性更改;
    公共ICOMAND GetIdeasCommand
    {
    得到
    {
    返回新命令(异步()=>
    {
    Ideas=wait_apiServices.GetIdeasAsync();
    });
    }
    }
    }
    
    注:除了你的主要问题之外,我想指出你的代码中接下来不好的地方

  • 使用POST从WEB下载数据。
  • 类名“ApiServices”

  • 如果您需要更多帮助,可以通过评论让我知道。

    我建议将列表更改为Observablecollection@dilmah我认为这不会有多大改变。我的意思是你觉得我的代码还好吗?还是我遗漏了什么?因为从apiservice获取json后,我一直得到一个空视图。@Aldo您确定您的服务实际返回了任何数据吗?设置一个断点,首先查看web请求返回的内容。第二,在你的虚拟机想法设定器中设置一个断点,看看它是否被调用过。离题:使用POST web请求下载数据感觉像一股臭味。@EvZ当然可以肯定。我已经试过了,我收到了一个json,里面有我需要的所有数据。我似乎无法理解为什么我的观点是空的/我建议把名单改为Observablecollection@dilmah我认为这不会改变