2017-12-18 14 views
0

私はページ間を移動して同時にデータをバインドしようとしています。mvvmを使用してxamarinフォームでビュー間でデータを渡す

これは私が試したものです:

public ICommand GetIdeasCommand 
{ 
    get 
    { 
     return new Command(async() => 
     { 
      Ideas = await _apiServices.GetIdeasAsync(); 
      await Application.Current.MainPage.Navigation.PushAsync(new IdeasSinglePage(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 
    } 
} 

おかげ この投稿は、私のアイデアを与えました。

+0

期待される出力は何ですか? 「IdeasSinglePage」にはコントロールがありますか? – EvZ

+0

「Ideas = await _apiServices.GetIdeasAsync(); '@EvZ –

+0

あなたのIdeasSinglePageコードビハインドとXAMLがある場合は、その情報を共有してください。 @Aldo – EvZ

答えて

0

BindingContextの理解が不足しています。通常は、ViewModelをBindingContextにバインドします。あなたがここに

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

をやっていることは意味がありません。

ロードするページにコンテキストとして渡していますか?この行を完全に削除するだけです。

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

そして、あなたのxmlにあなたのリストビューの名前を与える:あなたの最近のコメントであるので、あなたは何をあなたの分離コードで行いますがあり、ViewModelには、そもそもたくないと言いました。この名前は、コードの背後にあるリストを参照するために必要です。

希望します。

+0

あなたはまったく正しい@greggzです。説明をありがとう、本当にそれを感謝:)。 –

+0

@Aldo Npとハッピーコーディング;) – Greggz

1

あなたの問題は明らかです。あなたはContentPageにデータを渡していますが、何もしません。一般的に言えば、ViewModelから別のViewModelにパラメータを渡すことは非常に簡単な問題です。 2ページ目に直接データをダウンロードしないのはなぜ
1:いくつかの質問があり、この解決策を見て、

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

:ここ

XAMLなしのイラストのですか?
2.アプリ全体で必要な場合は、キャッシュまたはデータベースにデータを保存しないでください。

関連する問題