Xamarin 获取弹出页面的公共变量
我正在实例化另一个页面,并为其公共属性(“SomeValue”)赋值,如下所示:Xamarin 获取弹出页面的公共变量,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我正在实例化另一个页面,并为其公共属性(“SomeValue”)赋值,如下所示: _btnGotoOtherPage.Clicked += async (sender, e) => { OtherPage _otherpage = new OtherPage; _otherpage.SomeValue = 1033; await Navigation.PushAsync(_otherpa
_btnGotoOtherPage.Clicked += async (sender, e) =>
{
OtherPage _otherpage = new OtherPage;
_otherpage.SomeValue = 1033;
await Navigation.PushAsync(_otherpage);
return;
};
await CoreMethods.PushPageModel<BPageModel>(myParameter, true);
在此“\u otherpage”中,用户可以修改此值
当弹出“_otherpage”时,我想看看“SomeValue”变量并对其进行处理
MessagingSystem不是我所需要的,因为我不想收到有关值更改的通知。我只想知道当弹出“\u otherpage”时该值是多少
我也不想使用绑定(如果可能的话!),因为我觉得在处理许多这样的变量时很难组织起来
有可能在某个活动中做到这一点吗
我的理想解决方案是(伪代码):
谢谢。在A页:
MessagingCenter.Subscribe<string>(this, "SomeMessage", (s) => HandleMessage(s));
MessagingCenter.Subscribe(这是“SomeMessage”,s)=>HandleMessage);
B页:
MessagingCenter.Send<string>("SenderOrAnythingElse", "SomeMessage");
MessagingCenter.Send(“senderoranythinghelse”、“SomeMessage”);
在PageB中有一个公共变量:
public int SomeValue { get; set; }
现在我们展示PageB:
PageB nPageB = new PageB;
await Navigation.PushAsync(nPageB);
在nPageB中,用户现在可以更改PageB的公共变量
//we track the "Disappearing" event of the page.
//When it occurs, we get the value
nPageB.Disappearing += async (sender1, e1) =>
{
Debug.WriteLine("New value is: " + nPage.SomeValue.ToString());
};
这是我使用弹出窗口的方式,但是它可以以同样的方式与页面样式一起使用 下面是我的小例子,期望
MainPage=new-NavigationPage(new-Page1())代码>
它基本上是关于在公共属性所在的页面上有一个任务。此任务可以返回公共属性的值。任务将在OnDisappearing override中完成,并返回公共属性
要返回值,请按下页面并等待任务页面。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>)
}
}
}
使用系统;
使用System.Threading.Tasks;
使用Xamarin.Forms;
名称空间MVVMTests
{
///
///包含任务的页面的基本实现,
///它可以被完成并返回一个泛型T类型的值
///
///
公共类结果内容页:内容页
{
公共任务页面PoppedTask{get{return tcs.Task;}
私有任务完成源tcs;
公共结果内容页()
{
tcs=新任务完成源();
}
///
///完成任务并设置其结果
///
///
受保护的/*虚拟*/void SetPopupResult(T结果)
{
如果(PagePoppedTask.IsCompleted==false)
tcs.SetResult(结果);
}
}
///
///Page1存在一个按钮,用于创建Page2,为Page2上的道具赋值
///然后等待第2页的PagePoppedTask
///
公共类第1页:内容页
{
公共页1()
{
var按钮=新按钮()
{
Text=“转到第2页”
};
按钮。点击+=按钮\点击;
内容=按钮;
}
已单击专用异步无效按钮(对象发送方,事件参数e)
{
//翻页
var newPage=new Page2(){CoolInt=123};
等待App.Current.MainPage.Navigation.PushAsync(newPage);
//等待结果
int result=wait newPage.PagePoppedTask;
System.Diagnostics.Debug.WriteLine(“页面结果:+result.ToString());
}
}
///
///继承ResultContentPage并在其消失时设置PagePoppedTask
///
公共类第2页:结果内容页
{
public int CoolInt{get;set;}//页面上的属性
公共页2()
{
var按钮=新按钮()
{
Text=“返回第1页”
};
按钮。点击+=按钮\点击;
内容=按钮;
}
已单击专用异步无效按钮(对象发送方,事件参数e)
{
CoolInt=321;//将虚拟值分配给CoolInt prop并弹出页面
等待App.Current.MainPage.Navigation.popsync();//弹出页面
}
受保护的覆盖无效OnDisappearing()
{
base.OnDisappearing();
SetPopupResult(CoolInt);//设置任务的结果(在ResultContentPage中)
}
}
}
如果您正在寻找理想的解决方案,我建议您遵循MVVM模式,将大量代码从页面移到视图模型
我使用一个名为FreshMvvm的MVVM框架。这允许我执行视图模型到视图模型导航,并在它们之间传递参数,如下所示:
_btnGotoOtherPage.Clicked += async (sender, e) =>
{
OtherPage _otherpage = new OtherPage;
_otherpage.SomeValue = 1033;
await Navigation.PushAsync(_otherpage);
return;
};
await CoreMethods.PushPageModel<BPageModel>(myParameter, true);
我可以通过APageViewModel的ReverseInit方法访问它
大多数MVVM框架都有类似的功能
您没有理由不能使用MessagingCenter。或者你昨天刚刚问了一个关于定制活动的问题,你可以这样做。我不会把我的回答标记为“正确”答案,因为这种方式感觉太难看了。欢迎有其他选择。我只是想展示一些有用的东西。@sushingover你的建议/评论将非常感谢。这很有用,但设计相当糟糕-这两个页面紧密结合在一起,这通常是个坏主意