问题
I am trying to make a Master-Detail Navigation using help of this github example. The relevant code sample from my project is -
MasterPageItem.cs
namespace Demo.MenuItems
{
public class MasterPageItem
{
public string Title { get; set; }
public string IconSource { get; set; }
public Type TargetType { get; set; }
}
}
MainPage.Xaml.cs
public partial class MainPage : MasterDetailPage
{
public MainPage()
{
InitializeComponent();
masterPage.ListView.ItemSelected += OnItemSelected;
if (Device.RuntimePlatform == Device.UWP)
{
MasterBehavior = MasterBehavior.Popover;
}
Detail = new NavigationPage(new HomePage());
}
void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = e.SelectedItem as MasterPageItem;
if (item != null)
{
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
masterPage.ListView.SelectedItem = null;
IsPresented = false;
}
}
}
MasterPage.Xaml.cs
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MasterPage : ContentPage
{
public ListView ListView { get { return listView; } }
public MasterPage()
{
InitializeComponent();
var masterPageItems = new List<MasterPageItem>();
masterPageItems.Add(new MasterPageItem
{
Title = "Help",
IconSource = "icon-1.jpg",
TargetType = typeof(WebPage)
});
listView.ItemsSource = masterPageItems;
}
}
It works if no data is required to pass in detail page. However, I need to pass one string value url
in page WebPage
, but I am unable to figure out how to pass string value or any data in following line -
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
For e.g., following is the code sample for the page WebPage
-
public WebPage (string URL)
{
InitializeComponent ();
Browser.Source = URL;
}
Here I am unable to figure out, how should I pass url
from Master-Detail Navigation?
回答1:
Generalized Way :
//This will create instance of the page using the parameterized constructor you defined in each DetailPages
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType, myStringParam));
//Your Each Detail Page should have parametrized constructor.
public MyPage (string param)
{
InitializeComponent ();
Browser.Source = param;
}
Here, you can serialize the c# object and pass JSON string into myStringParam. Your page receives this in page’s parameterized constructor you defined and there, you can deserialize, thus you can pass complex objects as JSON into a page as well as simple string.
If you want to add parameterized constructor in only one DetailPage then:
if(item.TargetType == typeof(WebPage))
{
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType, myStringParam));
}
else
{
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));
}
//Your Page would be:
public WebPage (string URL)
{
InitializeComponent ();
Browser.Source = URL;
}
回答2:
Yes you can Activator.CreateInstance
Method as many overloads
Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType, url));
All the overloads can be found here
Activator.CreateInstance Method
The one you want is here
Activator.CreateInstance Method (Type, Object[])
Parameters
type Type: System.Type
The type of object to create
args Type: System.Object[]
An array of arguments that match in number, order, and type the parameters of the constructor to invoke. If args is an empty array or null, the constructor that takes no parameters (the default constructor) is invoke
来源:https://stackoverflow.com/questions/48003883/how-to-pass-string-data-from-master-detail-page-in-xamarin-forms