【问题标题】:Xamarin passing list item value, from one class to MainPage class ToolbarItemXamarin 传递列表项值,从一个类到 MainPage 类 ToolbarItem
【发布时间】:2016-05-19 12:19:12
【问题描述】:

很多小时我都在尝试将 ItemSelected 值传递给我的 MainPage ToolbarItem x:name="CityClick"。这是我在 Cities 类后面的 Cities 类和 City 类的代码:

public partial class Cities : ContentPage
    {
        private Action<object, SelectedItemChangedEventArgs> onItemSelected;
        public Cities()
        {
            InitializeComponent();
            Label header = new Label
            {
                Text = "Select here",
                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                HorizontalOptions = LayoutOptions.Center
            };
            List<City> cities = new List<City>
            {
                new City("C1"),
                new City("City2")
            };
            ListView listView = new ListView
            {
                ItemsSource = cities,
                ItemTemplate = new DataTemplate(() =>
                {
                    Label nameLabel = new Label();
                    nameLabel.SetBinding(Label.TextProperty, "Name");
                    BoxView boxView = new BoxView();
                    return new ViewCell
                    {
                        View = new StackLayout
                        {
                            Padding = new Thickness(0, 5),
                            Orientation = StackOrientation.Horizontal,
                            Children =
                            {
                                ...
                            }
                        }
                    };
                })
            };
            this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);
            this.Content = new StackLayout
            {
                Children =
                {
                    ..
                }
            };
            listView.ItemSelected += async (sender, e) =>
            {
                if (e.SelectedItem == null)
                {
                    return;
                }
                else
                {
                    await Navigation.PopAsync();
                }

            };
        }
        public Cities(Action<object, SelectedItemChangedEventArgs> onItemSelected)
        {
            this.onItemSelected = onItemSelected;
        }
    }
    public class City
    {
        public City()
        {
            this.Name = Name;
        }
        public City(string name)
        {
            this.Name = name;
        }
        public string Name { set; get; }
    };

这是我初始化 City() 类的 MainPage 类,但是当我调试时它显示 obj.Name 为 null :

public partial class MainPage : MasterDetailPage
    {
        public MainPage()
        {
            InitializeComponent();
            masterPage.ListView.ItemSelected += OnItemSelected;

            CityClick.Clicked += async (sender, e) =>
            {
                await Detail.Navigation.PushAsync(new Cities());
            };
            City obj = new City();
            Cities obj2 = new Cities();
            var myobject = Application.Current.Properties[CityClick.Text];
            myobject = obj.Name;
            if (obj.Name == null)
            {
                if (Application.Current.Properties.ContainsKey(CityClick.Text))
                {
                    var id = Application.Current.Properties[CityClick.Text] as string;
                    CityClick.Text = obj.Name;
                }
            }
        }}

这是 MainPage xaml:

<?xml version="1.0" encoding="UTF-8"?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
                  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                  xmlns:local="clr-namespace:LearningProject;assembly=LearningProject"
                  x:Class="LearningProject.MainPage"
          MasterBehavior="Popover">
  <ContentPage.ToolbarItems>
    <ToolbarItem x:Name="CityClick"
                 Text="City:"
                 Order="Primary">
    </ToolbarItem>
  </ContentPage.ToolbarItems>
  <MasterDetailPage.Master>
    <local:MasterPage x:Name="masterPage" />
  </MasterDetailPage.Master>
  <MasterDetailPage.Detail>
    <NavigationPage>
      <x:Arguments>
        <local:UnpaidVehicle />
      </x:Arguments>
    </NavigationPage>
  </MasterDetailPage.Detail>
</MasterDetailPage>

所以当我调试时,它表明 ItemSelected 持有一些字符串名称。但是当我调试 MainPage.xaml.cs 时,它显示它为空。为什么 obj.Name 没有达到 Cities 类的值?

【问题讨论】:

  • 我在您的问题中看到了几个问题:当您订阅 MasterPage 类上的事件时,我在您的示例代码中没有看到该页面的定义。 CitiesPage 中ListView 的事件处理程序仅弹出导航堆栈;它不会冒泡任何东西或设置可以从包含元素访问的属性。
  • @AndyHopper 嗨,您的意思是订阅 CityClick.Clicked += ?为什么我必须在这里定义一些东西?这只是一个简单的导航到另一个页面。是的,城市页面中的 listView 与任何东西都没有真正的关系,我会修复它。
  • @AndyHopper 实际上那个事件处理程序是需要的,我试图在没有的情况下执行它,但我遇到了一堆错误。
  • 不,CityClick 只是一个工具栏项。我指的是您的Cities ContentPage 对ListItem 选择没有做任何事情以将其发布给外部观察者/消费者。我看到Cities 页面的过载,该页面采用Action;我假设您打算将其用作发布机制。我建议你不要这样做。相反,在Cities 上定义一个公开当前选择的City 的公共属性,发布“选择更改”事件,设置属性并触发更改事件。
  • 我还建议不要弹出Cities中的导航堆栈;让 change 事件的订阅者处理该问题,以便将该问题与 Cities 分开。

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

好的,所以我以这样的方式解决了值传递问题:我将 private Action&lt;object, SelectedItemChangedEventArgs&gt; onItemSelected; 更改为 public event EventHandler&lt;SelectedItemChangedEventArgs&gt; OnSelectedCity; 这个事件处理程序将用于 MainPage 类。它会检测何时选择了项目。然后在 listView.ItemSelected else 部分中,我添加了对所选对象的解析:OnSelectedCity((City)e.SelectedItem, null); 完整的 else 部分如下所示:

                else
                {
                    ((ListView)sender).SelectedItem = null;
                    if (OnSelectedCity != null)
                    {
                        OnSelectedCity((City)e.SelectedItem, null);
                    }
                    await Navigation.PopAsync();
                }

然后在 CityClick.Clicked 事件的 MainPage 类中,我像这样初始化 Cities 类:

CityClick.Clicked += async (sender, e) =>
            {
                Cities page = new Cities();
                page.OnSelectedCity += Page_OnSelectedCity;
                await Detail.Navigation.PushAsync(page);
            };

我在这里调用 Cities 类的事件。我自动创建Page_OnSelectedCity 方法。在那里我传递类的对象值并将其分配给我的 ToolBarItem 的 CityClick.Text 标签,在那里我看到了来自 City 类的值!

void Page_OnSelectedCity(object sender, SelectedItemChangedEventArgs e)
        {
            var city = (City)(sender);
            CityClick.Text = city.Name;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 2021-11-20
    相关资源
    最近更新 更多