如何从Xamarin表单的主从页面中传递字符串数据 [英] How to pass string data from Master-Detail Page in Xamarin Forms

查看:152
本文介绍了如何从Xamarin表单的主从页面中传递字符串数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此github 进行主从导航.例子.我项目中的相关代码示例为-

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

如果不需要任何数据即可通过详细页面,则此方法有效.但是,我需要在页面WebPage中传递一个字符串值url,但是我无法弄清楚如何在下面的行中传递字符串值或任何数据-

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

例如,以下是页面WebPage-

public WebPage (string URL)
{
    InitializeComponent ();
    Browser.Source = URL;
}

在这里我无法弄清楚,如何从主从导航中通过url?

Here I am unable to figure out, how should I pass url from Master-Detail Navigation?

推荐答案

通用方式:

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

在这里,您可以序列化c#对象并将JSON字符串传递到myStringParam中.您的网页会在您定义的网页的参数化构造函数中接收到此内容,然后可以在其中进行反序列化,从而可以将复杂对象(如JSON)以及简单字符串传递给网页.

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.

如果只想在一个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;
}

这篇关于如何从Xamarin表单的主从页面中传递字符串数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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