简体   繁体   English

如何从Xamarin表单的主从页面中传递字符串数据

[英]How to pass string data from Master-Detail Page in Xamarin Forms

I am trying to make a Master-Detail Navigation using help of this github example. 我正在尝试使用此github示例的帮助进行主从导航。 The relevant code sample from my project is - 我项目中的相关代码示例为-

MasterPageItem.cs 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 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 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 - 但是,我需要在页面WebPage传递一个字符串值url ,但是我无法弄清楚如何在下一行中传递字符串值或任何数据-

Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType));

For eg, following is the code sample for the page WebPage - 例如,以下是网页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? 在这里我无法弄清楚,如何从主从导航中传递url

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. 在这里,您可以序列化c#对象并将JSON字符串传递到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. 您的页面在您定义的页面的参数化构造函数中接收到此信息,然后可以在其中反序列化,因此您可以将复杂对象(如JSON)以及简单字符串传递给页面。

If you want to add parameterized constructor in only one DetailPage then : 如果只想在一个DetailPage中添加参数化的构造函数,则

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

Yes you can Activator.CreateInstance Method as many overloads 是的,您可以将Activator.CreateInstance方法作为许多重载

Detail = new NavigationPage((Page)Activator.CreateInstance(item.TargetType, url));

All the overloads can be found here 所有重载都可以在这里找到

Activator.CreateInstance Method Activator.CreateInstance方法

The one you want is here 你想要的一个在这里

Activator.CreateInstance Method (Type, Object[]) Activator.CreateInstance方法(类型,对象[])

Parameters 参量

type Type: System.Type 类型类型:System.Type

The type of object to create 要创建的对象类型

args Type: System.Object[] args类型: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 如果args为空数组或null,则调用不带参数的构造函数(默认构造函数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM