xamarin.forms - 消息中心将数据传递给条目

时间:2017-02-20 06:19:24

标签: c# xamarin.forms

我想在我的条目中显示它没有发生,如果我使用带有消息中心的View Model它将无法显示,告诉我我做错了我应该做什么。我认为该条目无法在消息中心找到价值。

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

1 个答案:

答案 0 :(得分:-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());

也许这是有效的尝试