我尝试将ItemSelected值传递给MainPage ToolbarItem x:name="CityClick"
很多小时。这是我的城市类和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; }
};
这是我的MainPage类,我初始化City()类,但是当我调试时,它显示为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时,它显示它为null。为什么obj.Name没有达到Cities类的值?
答案 0 :(得分:0)
好的,所以我用这样的方式解决了值传递问题:我将private Action<object, SelectedItemChangedEventArgs> onItemSelected;
更改为public event EventHandler<SelectedItemChangedEventArgs> 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类调用event。我自动创建Page_OnSelectedCity
方法。在那里我传递类的对象值并将其分配给ToolBarItem的CityClick.Text标签,在那里我看到来自City
类的值!
void Page_OnSelectedCity(object sender, SelectedItemChangedEventArgs e)
{
var city = (City)(sender);
CityClick.Text = city.Name;
}