- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个使用 NavigationPage 进行正常屏幕导航的 Xamarin.Forms 应用程序。从其中一个屏幕(Stay Detail),我需要显示一系列 4 个顺序模式页面(如向导),这些页面收集数据以完成与 Stay Detail 相关的过程。流程中的每个页面都有一个“取消”按钮,允许用户取消向导并返回 Stay Detail。这是一般流程:
modal1 -> modal2 -> modal3 -> modal4
/ \
StayDetail StayDetail
从 StayDetail 执行 PushModalAsync 以启动 modal1,然后 PushModalAsync/PopModalAsync 在各个模式页面之间切换非常容易。但是,我想不出一种从第二个模态或更高版本退出模态堆栈的干净方法。最好的方法是什么?
最佳答案
对于您的情况,此解决方案可能看起来过于复杂,但这是我用来在模态操作之间进行通信以随机解决依赖项(没有帐户,ID 丢失,同时删除数据等)而不会失去类型安全性的方法.
考虑向导/模态系列的一种方法是,下一页取决于上一页,并且大多数情况下您希望使用子模态的结果。
希望对您有所帮助:
public static class ModalManager
{
public static bool TryRespondModal<TData>(this IModalResponse<TData> source, TData data, bool autoPop = true, bool animate = false)
{
if (Application.Current.MainPage.Navigation.ModalStack.Count <= 0)
return false;
source.ModalRequestComplete.SetResult(data);
if (autoPop)
Application.Current.MainPage.Navigation.PopModalAsync(animate);
return true;
}
public static Task<TData> GetResponseAsync<TData>(this IModalResponse<TData> source)
{
source.ModalRequestComplete = new TaskCompletionSource<TData>();
return source.ModalRequestComplete.Task;
}
}
public enum WizardState
{
Indeterminate = 0,
Complete = 1,
Canceled = 2
}
public class WizardModel
{
public WizardState State { get; set; }
public List<string> Page1Values { get; set; } = new List<string>();
public List<string> Page2Values { get; set; } = new List<string>();
public List<string> Page3Values { get; set; } = new List<string>();
}
public abstract class WizardPage : IModalResponse<WizardModel>
{
public WizardModel Model { get; set; }
public ICommand NextCommand { get; set; }
public ICommand PreviousCommand { get; set; }
public ICommand CancelCommand { get; set; }
protected WizardPage(WizardModel model)
{
Model = model;
NextCommand = new Command(NextButtonPressed);
PreviousCommand = new Command(PreviousButtonPressed);
CancelCommand = new Command(PreviousButtonPressed);
}
protected abstract IModalResponse<WizardModel> GetNextPage();
protected virtual void CancelButtonPressed()
{
Model.State = WizardState.Canceled;
this.TryRespondModal(Model);
}
protected virtual void PreviousButtonPressed()
{
// you're dropping down on the level of dependent windows here so you can tell your previous modal window the result of the current response
this.TryRespondModal(Model);
}
protected virtual async void NextButtonPressed()
{
var np = GetNextPage();
if (Model.State == WizardState.Complete || np == null || (await np?.GetResponseAsync()).State == WizardState.Complete)
PersistWizardPage();
// all following modal windows must have run through - so you persist whatever your page has done, unless you do that on the last page anyway. and then tell the previous
// modal window that you're done
this.TryRespondModal(Model);
}
protected virtual void PersistWizardPage()
{
// virtual because i'm lazy
throw new NotImplementedException();
}
public TaskCompletionSource<WizardModel> ModalRequestComplete { get; set; }
}
public class Page1 : WizardPage
{
public Page1(WizardModel model) : base(model)
{
}
protected override IModalResponse<WizardModel> GetNextPage()
{
return new Page2(Model);
}
}
public class Page2 : WizardPage
{
public Page2(WizardModel model) : base(model)
{
}
protected override IModalResponse<WizardModel> GetNextPage()
{
return new Page3(Model);
}
}
public class Page3 : WizardPage
{
public Page3(WizardModel model) : base(model)
{
}
protected override IModalResponse<WizardModel> GetNextPage()
{
return null;
}
protected override void NextButtonPressed()
{
this.Model.State = WizardState.Complete;
base.NextButtonPressed();
}
}
public interface IModalResponse<TResponseType>
{
TaskCompletionSource<TResponseType> ModalRequestComplete { get; set; }
}
关于c# - 清除 Xamarin Forms 模态堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36892044/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!