2017-02-20 77 views
1

我想在我的条目上显示它没有发生,如果我使用带有消息传递中心的视图模型它将不会显示,告诉我我在做什么错误什么我应该怎么做。我认为该条目无法在消息传递中心找到价值。xamarin.forms - 消息传递中心传递数据到条目

<?xml version="1.0" encoding="utf-8" ?> 
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
       x:Class="Data1.Views.Page1"> 
     <ContentPage.Content> 
     <ListView x:Name = "lstView" ItemTapped="OnItemTapped" /> 
     </ContentPage.Content> 
    </ContentPage> 

    public partial class Page1 : ContentPage 
    { 
     public Page1() 
     { 
      InitializeComponent(); 
      lstView.ItemsSource = new List<string>() { "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers" }; 
     } 
     void OnItemTapped(object sender, ItemTappedEventArgs e) 
     { 
      if (e == null) return; // has been set to null, do not 'process' tapped event 
      Debug.WriteLine(e.Item); 
      ((ListView)sender).SelectedItem = null; // de-select the row 
      var person = new Person 
      { 
       Name = e.Item.ToString() 
      }; 
      MessagingCenter.Send<Page1, string>(this, "Hi", e.Item.ToString()); 
      Navigation.PushModalAsync(new Page2()); 
     } 
    } 

    public partial class Page2 : ContentPage 
    { 
     public Page2() 
     { 
      InitializeComponent(); 

      MessagingCenter.Subscribe<Page1, string>(this, "Hi", (sender, arg) => 
      { 
       txttest.Text = arg; 
      }); 
     } 
    } 
+0

你发送的Page2之前消息已创建的,所以它不尚未收到该消息。 MessagingCenter不排队消息以供将来传送。在这种情况下,只要将该值作为参数传递给Page2的构造函数就简单多了。 – Jason

+0

即使我使用它并将它绑定到条目仍然没有:public class MainViewModel { public Person Person {get;组; } 公共MainViewModel() { MessagingCenter.Subscribe <第1页,字符串>(在此, “您好”,(发件人,ARG)=> { 人=新的Person { 名称= ARG }; }) ; } } –

+0

没有看到上下文中的代码我不知道它是否解决我提到的问题或不。 – Jason

回答

-1

MessagingCenter.Send<Page1, string>(this, "Hi", e.Item.ToString()); 
Navigation.PushModalAsync(new Page2()); 

变线和

Navigation.PushModalAsync(new Page2()); 
MessagingCenter.Send<Page1, string>(this, "Hi", e.Item.ToString()); 

也许它的作品尝试