获取弹出页面的公共变量 [英] Getting public variable of popped page

查看:87
本文介绍了获取弹出页面的公共变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实例化另一个页面,并为它的一个公共属性("SomeValue")分配一个值,如下所示:

I'm instancing another page, and I assign a value to one of its public properties ("SomeValue") like this:

        _btnGotoOtherPage.Clicked += async (sender, e) =>
        {
            OtherPage _otherpage = new OtherPage;
            _otherpage.SomeValue = 1033;
            await Navigation.PushAsync(_otherpage);
            return;
        };

在此"_otherpage"中,用户可以修改此值.

Within this "_otherpage", the user can modified this value.

弹出"_otherpage"时,我想看看"SomeValue"变量并对其进行处理.

When "_otherpage" is popped, I would like to have a look at the "SomeValue" variable and do something with it.

我不需要MessagingSystem,因为我不想在值更改时收到通知.我只想知道当弹出"_otherpage"时该值是什么.

MessagingSystem wouldn't be what I need because I don't want to be notified on the value change. I only want to know what this value is when "_otherpage" is popped.

我也不想使用Binding(如果可能的话!),因为在处理许多此类变量时,我觉得很难进行组织.

I would also like to not use Binding (if possible!) because I feel it's hard to organize when I'm dealing with many of such variables.

也许可以通过一个事件来做到这一点吗?

Is it possible to do this with an event perhaps?

我梦dream以求的解决方案是(伪代码):

My dream solution would be (pseudo code):

private void OnPagePopped()
{
    int iNewValue = PoppedPage.SomeValue;
}

谢谢.

推荐答案

这是我对弹出式窗口的处理方式,但是可以以相同的方式与页面样式一起使用.

This is how I do it with my popups, but it can be used with the page style in the same way.

这里是我的小例子,期望MainPage = new NavigationPage(new Page1());

Heres my little example, expecting MainPage = new NavigationPage(new Page1());

基本上,这是关于在公共财产所在的页面上放置Task.此任务可以返回公共属性的值.该任务将在OnDisappearing替代中完成,并返回公共属性.

It is basically about having a Task on the Page where the public property sits. This task can return the value of the public property. The task will be completed in the OnDisappearing override and return the public property.

要取回值,请推动页面并等待任务页面.PagePoppedTask

To get the value back, you push the page and await the Task page.PagePoppedTask

using System;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace MVVMTests
{
    /// <summary>
    /// A base implementation for a page, that holds a task, 
    /// which can be completed and returns a value of type of the generic T
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ResultContentPage<T> : ContentPage
    {
            public Task<T> PagePoppedTask { get { return tcs.Task; } }
            private TaskCompletionSource<T> tcs;

            public ResultContentPage()
            {
                tcs = new TaskCompletionSource<T>();
            }

            /// <summary>
            /// Completes the task and sets it result
            /// </summary>
            /// <param name="result"></param>
            protected /* virtual */ void SetPopupResult(T result)
            {
                if (PagePoppedTask.IsCompleted == false)
                    tcs.SetResult(result);
            }
    }

    /// <summary>
    /// Page1 exists of one button, that creates the Page2, assigns a value to a prop on the Page2
    /// and then awaits the PagePoppedTask of Page2
    /// </summary>
    public class Page1 : ContentPage
    {
        public Page1()
        {
            var button = new Button()
            {
                Text = "Go to page 2"
            };
            button.Clicked += Button_Clicked;

            Content = button;
        }

        private async void Button_Clicked(object sender, EventArgs e)
        {
            //Push the page
            var newPage = new Page2() { CoolInt = 123 };
            await App.Current.MainPage.Navigation.PushAsync(newPage);

            //Await the result
            int result = await newPage.PagePoppedTask;
            System.Diagnostics.Debug.WriteLine("Page result: " + result.ToString());
        }
    }

    /// <summary>
    /// Inherits from the ResultContentPage and sets the PagePoppedTask as soon as its Disappearing
    /// </summary>
    public class Page2 : ResultContentPage<int>
    {
        public int CoolInt { get; set; }    //Your property on the page

        public Page2()
        {
            var button = new Button()
            {
                Text = "Go back to page 1"
            };
            button.Clicked += Button_Clicked;

            Content = button;
        }

        private async void Button_Clicked(object sender, EventArgs e)
        {
            CoolInt = 321;                                      //assign dummy value to CoolInt prop and pop the page
            await App.Current.MainPage.Navigation.PopAsync();   //pop the page
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();
            SetPopupResult(CoolInt);    //set the result of the task (in ResultContentPage<T>)
        }
    }
}

这篇关于获取弹出页面的公共变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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