使用mvvm在xamarin表单中的视图之间传递数据

时间:2017-12-18 17:11:58

标签: c# xamarin mvvm binding xamarin.forms

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

这就是我的尝试:

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

假设Ideas是我从json获得的数组列表。但这种方法对我没有帮助,因为我得到一个空白页面。此外,如果我在页面内调用此功能一切都很好。 这篇文章给了我一个想法: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
    }
}

感谢。

2 个答案:

答案 0 :(得分:2)

您的问题很明显,您将数据传递给ContentPage,但您没有对其执行任何操作。一般来说,将参数从一个ViewModel传递到另一个ViewModel是一个非常简单的问题。

以下是没有XAML的说明:

public class MyFirstPage : ContentPage
{
  public MyFirstPage()
  {
    this.BindingContext = new MyFirstPageViewModel();
  }
}

public class MyFirstPageViewModel : INotifyPorpertyChanged
{
  public ICommand<List<string>> DownloadDataCmd { get; }

  public MyFirstPageViewModel()
  {
    DownloadDataCmd = new Command<List<string>>(async () => {
        var data = await dataService.DownloadData();
        await navService.PushAsync(new MySecondPage(data));
    });
  }
}

public class MySecondPage : ContentPage
{
  public MySecondPage(List<string> downloadedData)
  {
    this.BindingContext = new MySecondPageViewModel(downloadedData);
  }
}

public class MySecondPageViewModel : INotifyPropertyChanged
{
  public List<string> Data { get; }
  public MySecondPageViewModel(List<string> downloadedData)
  {
     // Do whatever is needed with the data
     Data = downloadedData;
  }
}

现在,看看这个解决方案,几乎没有问题:
1.为什么不直接在第二页下载数据?
2.如果您需要在整个应用程序中使用它,为什么不将数据存储在缓存或数据库中?

答案 1 :(得分:1)

缺乏对BindingContext的理解。通常将ViewModel绑定到BindingContext。你在这做什么

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

没有意义。

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

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

在你的xml中你给listView一个名字。您需要此名称来引用后面代码的列表。

希望有所帮助