使用 mvvm 在 xamarin 形式的视图之间传递数据

4 c# binding mvvm xamarin xamarin.forms

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

这是我尝试过的:

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

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

        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Padding="20, 10">
                        <Label Text="{Binding Ideas}"
                               FontSize="12"
                               TextColor="RoyalBlue"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
Run Code Online (Sandbox Code Playgroud)

背后的代码:

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

      InitializeComponent();
      this.BindingContext = new IdeasSinglePage(ideas); //the app breaks here
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢。

EvZ*_*EvZ 7

您的问题很明显,您正在将数据传递给 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;
  }
}
Run Code Online (Sandbox Code Playgroud)

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


Gre*_*ggz 6

您对 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;
  }
}
Run Code Online (Sandbox Code Playgroud)

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

希望能帮助到你