2014-02-15 44 views
0

你好,我想傳遞一個字符串到另一個viewmodel。我試圖使用這段代碼,但它不符合我的目的,因爲我需要知道收件人視圖的viewmodel中的字符串值。我怎樣才能做到這一點?你也可以用mvvm-light和命令提出更好的方法,因爲我的概念對於這兩者來說並不那麼清楚。將數據傳遞給另一個ViewModel

public MainViewModel() 
{ 
     GiveDetails = new RelayCommand<object>(DoSomething); 
} 

private void DoSomething(object param) 
{ 
     var rootFrame = (App.Current as App).RootFrame; 
     rootFrame.Navigate(new System.Uri("/Details.xaml?ID="+param.ToString(), System.UriKind.Relative)); 
} 

我目前沒有線索如何處理收件人viewmodel上的參數。

回答

1

Stack Careers Browser當從主頁面視圖中選擇職業生涯時,我在DetailsPage.xaml.cs的OnNavigatedTo事件中具有以下內容。

if (DataContext == null) 
     { 
      string selectedIndex = ""; 
      if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex)) 
      { 
       **int index = int.Parse(selectedIndex);** 
       **DataContext = _vm = new JobPostingViewModel(App.ViewModel.JobPostings[index]);** 
       Indicators.SetIndicators(this, DataContext); 
       await _vm.ScrapeThatScreenAsync(_vm.JobPosting.Id); 
      } 
     } 

相應的視圖模型有一個構造函數,看起來像這樣:

public JobPostingViewModel(JobPosting jobPosting) 
    { 
     if (jobPosting == null) 
      throw new ArgumentNullException("jobPosting"); 

     JoelTestResults = new ObservableCollection<JoelTestResult>(); 
     // JoelTestResults = new ObservableCollection<string>(){"a","b","c"}; 
     _jobPosting = jobPosting; 
     if (jobPosting.Categories != null && jobPosting.Categories.Any()) 
      ProcessCategories(jobPosting.Categories); 
    } 

哪裏需要一些基本對象,以處理更多的信息,並在Windows Phone 8的方式顯示出來。

相關問題