gpt4 book ai didi

c# - 清除 Xamarin Forms 模态堆栈

转载 作者:太空狗 更新时间:2023-10-29 23:49:57 24 4
gpt4 key购买 nike

我有一个使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com