使用 mvvm 在 xamarin 形式的视图之间传递数据 [英] Passing data between view in xamarin forms using mvvm

查看:25
本文介绍了使用 mvvm 在 xamarin 形式的视图之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在页面之间导航并同时绑定数据.

I'm trying to navigate between pages and bind data at same time.

这是我尝试过的:

public ICommand GetIdeasCommand
{
    get
    {
        return new Command(async () =>
        {
            Ideas = await _apiServices.GetIdeasAsync();
            await Application.Current.MainPage.Navigation.PushAsync(new IdeasSinglePage(Ideas));
        });
    }
}

假设 Ideas 是我从 json 中获取的数组列表.但是这种方法对我没有帮助,因为我得到了一个空白页.此外,如果我在页面内调用此函数,一切都很好.这篇文章给了我一个想法:如何在 Xamarin.Forms 中将参数从一个页面传递到另一个页面?

It is supposed the Ideas is a list of arrays I get from the json. But this approach is not helping me since I get a blank page. Also if I call this function inside the page everything is fine. This post gave me an idea : How to pass a parameter from one Page to another Page in Xamarin.Forms?

我的观点:

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

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

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

背后的代码:

public partial class IdeasSinglePage : ContentPage
{
    public IdeasSinglePage(List<Models.Ideas> ideas)
    {

      InitializeComponent();
      this.BindingContext = new IdeasSinglePage(ideas); //the app breaks here
    }
}

谢谢.

推荐答案

您对 BindingContext 的理解不足.通常您将 ViewModel 绑定到 BindingContext.你在这里做什么

Your understanding of BindingContext is lacking. Usually you bind a ViewModel to a BindingContext. What you're doing here

this.BindingContext = new IdeasSinglePage(ideas);//应用程序在这里中断

没有意义.

您将要加载的页面作为上下文传递?只需完全删除此行.由于在您最近的评论中您说您不希望从 ViewModel 开始,您将在 CodeBehind 中执行的操作是:

You are passing as context the page you want to load ? Just delete this line completely. Since in your recent comments you said you didn't want a ViewModel to begin with, what you will do in your CodeBehind is:

public partial class IdeasSinglePage : ContentPage
{
  public IdeasSinglePage(List<Models.Ideas> ideas)
  {
    InitializeComponent();
    listViewName.ItemsSource = ideas;
  }
}

在您的 xml 中,您为 listView 指定一个名称.你需要这个名字来引用后面的代码列表.

And in your xml you give your listView a Name. You need this Name for referencing the list on code behind.

希望能帮到你

这篇关于使用 mvvm 在 xamarin 形式的视图之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆