gpt4 book ai didi

c# - 使用 mvvm 在 xamarin 上绑定(bind)数据收集

转载 作者:行者123 更新时间:2023-12-03 11:02:51 24 4
gpt4 key购买 nike

所以我开始学习 xamarin 并尝试不同的方法将数据绑定(bind)从 View 到模型。我正在使用发布请求从服务中检索数据,在获得数据后,我无法将它们绑定(bind)到 View 。经过大量研究,我找到了一些有趣的解决方案,并尝试了不同的方法。
这是我到目前为止所取得的成就:
我的观点 :

<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>

这是我的 View 模型
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));
}

}

这是我的服务:
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 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; }

}

我真的很感激一些帮助!谢谢!!

最佳答案

看来您的 property definition 有问题:

 public List<Ideas> Ideas
{
get { return Ideas; }
set
{
Ideas= value; // Endless loop
OnPropertyChanged();
}
}

以这种方式添加支持字段:
List<Ideas> _ideas;
public List<Ideas> Ideas
{
get { return _ideas; }
set
{
if(value == _ideas) return;
_ideas = value;
OnPropertyChanged();
}
}

我建议使用 Fody.PropertyChanged为了减少与 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();
});
}
}
}

P.S.:除了你的主要问题,我想指出你的代码中看起来不太好的接下来的事情。
  • 使用 POST 从 WEB 下载数据。
  • 类名“ApiServices”。

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

    关于c# - 使用 mvvm 在 xamarin 上绑定(bind)数据收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47869545/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com