Xamarin 传递列表项值,从一个类到 MainPage 类 ToolbarItem [英] Xamarin passing list item value, from one class to MainPage class ToolbarItem

查看:19
本文介绍了Xamarin 传递列表项值,从一个类到 MainPage 类 ToolbarItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Many hours I am trying to pass ItemSelected value to my MainPage ToolbarItem x:name="CityClick". This is my code of Cities class and City class behind of Cities class:

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 :

This is my MainPage class where I initialize City() class, but when I am debugging it shows null for obj.Name :

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:

And this is 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 类的值?

So when I am debuging, it is shows that ItemSelected hold some string name. But when I am debuging MainPage.xaml.cs it shows that it is null. Why obj.Name do not reach value from Cities class?

推荐答案

Ok ,所以我以这样的方式解决了值传递问题:我改变了 private ActiononItemSelected; 公共事件 EventHandlerOnSelectedCity; 此事件处理程序将在 MainPage 类中使用.它会检测何时选择了项目.然后在 listView.ItemSelected else 部分中,我添加了对所选对象的解析: OnSelectedCity((City)e.SelectedItem, null); 完整的 else 部分如下所示:

Ok , so I solved value passing issue in such a way: I changed private Action<object, SelectedItemChangedEventArgs> onItemSelected; to public event EventHandler<SelectedItemChangedEventArgs> OnSelectedCity; This event handler will be used in MainPage class. And It will detects when item is selected. Then in listView.ItemSelected else section I added parsing of my selected object : OnSelectedCity((City)e.SelectedItem, null); Full else section looks like this:

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

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

Then in MainPage class of CityClick.Clicked event I initialize Cities class like this:

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 类的值!

Here I call event from Cities class. And I create automaticly Page_OnSelectedCity method. There I pass object value of class and assign it to my CityClick.Text label of ToolBarItem, there I see value from my City class!

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

这篇关于Xamarin 传递列表项值,从一个类到 MainPage 类 ToolbarItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆