Xamarin.forms 反应式UI数据持久性

Xamarin.forms 反应式UI数据持久性,xamarin.forms,reactiveui,xamarin.uwp,data-persistence,datapersistance,Xamarin.forms,Reactiveui,Xamarin.uwp,Data Persistence,Datapersistance,有人能帮我介绍一下如何使用UWP和iOS在Xamarin forms应用程序上实现数据持久性吗?我在网上找到了一些例子,但它们都很老了,只引用了iOS和Android Xamarin Native。基本上,我希望采用当前页面的视图模型并持久化所有用户输入的数据 阿卡瓦切苏彭斯河 namespace NameSpace { public class AkavacheSuspensionDriver<TAppState> : ISuspensionDriver where TApp

有人能帮我介绍一下如何使用UWP和iOS在Xamarin forms应用程序上实现数据持久性吗?我在网上找到了一些例子,但它们都很老了,只引用了iOS和Android Xamarin Native。基本上,我希望采用当前页面的视图模型并持久化所有用户输入的数据

阿卡瓦切苏彭斯河

namespace NameSpace    {
public class AkavacheSuspensionDriver<TAppState> : ISuspensionDriver where TAppState : class
{
    private const string appStateKey = "AppBootstrapper";

    public IObservable<Unit> InvalidateState()
    {
        var result = BlobCache.UserAccount.InvalidateObject<TAppState>(appStateKey);
        return result;
    }

    public IObservable<object> LoadState()
    {
        var result = BlobCache.UserAccount.GetObject<TAppState>(appStateKey);
        return result;
    }

    public IObservable<Unit> SaveState(object state)
    {
        var result = BlobCache.UserAccount.InsertObject(appStateKey, (TAppState)state);
        return result;
    }
}
名称空间{
公共类AkavacheSuspensionDriver:IssuspensionDriver,州:类
{
私有常量字符串appStateKey=“AppBootstrapper”;
公共IObservable不动产()
{
var result=BlobCache.UserAccount.InvalidateObject(appStateKey);
返回结果;
}
公共IObservable加载状态()
{
var result=BlobCache.UserAccount.GetObject(appStateKey);
返回结果;
}
公共IObservable存储状态(对象状态)
{
var result=BlobCache.UserAccount.InsertObject(appStateKey,(TAppState)状态);
返回结果;
}
}
App.cs

 RxApp.SuspensionHost.CreateNewAppState = () => new AppBootstrapper();
 RxApp.SuspensionHost.SetupDefaultSuspendResume(new 
 AkavacheSuspensionDriver<AppBootstrapper>());

 MainPage = RxApp.SuspensionHost.GetAppState<AppBootstrapper>().CreateMainPage();
RxApp.suspendionhost.CreateNewAppState=()=>newAppBootstrapper();
RxApp.SuspensionHost.SetupDefaultSuspendResume(新
AkavacheSuspensionDriver());
MainPage=RxApp.SuspensionHost.GetAppState().CreateMainPage();

这里主页将尝试检索“某物”从缓存中,但没有任何东西存在。我需要自己处理反序列化和数据处理吗?我希望首先创建默认的新AppBootstrapper。

有很多不同的解决方案。其中之一是一个名为akavache的反应UI项目。它将允许您保存数据。您可以混合使用它们使用AutoSuspendHelper


请注意,我们关于数据持久性的文档目前正在进行中。我们正在尝试从文档中删除所有聊天对话。

因此,我不确定对此有何感受,但我已经找到了解决问题的方法。我发现SuspensionHost最终会自行创建AppState。然后它将存储数据,并对存储的内容进行重新水合物化

    public App()
    {
        InitializeComponent();
        try
        {
            RxApp.SuspensionHost.CreateNewAppState = () =>
            {
                return new AppBootstrapper();
            };

            RxApp.SuspensionHost.SetupDefaultSuspendResume(new AkavacheSuspensionDriver<AppBootstrapper>());

            RxApp.SuspensionHost.ObserveAppState<AppBootstrapper>().Subscribe<AppBootstrapper>((a) =>
            {
                Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                {
                    var bootstrapper = a;
                    MainPage = bootstrapper.CreateMainPage();
                });
            });

            AppCenterLog.Info("Start", "Application Started");
        }
        catch (Exception ex)
        {
            //Crash reporting not available in appcenter - using event analytics as temp solution
            Analytics.TrackEvent("Bootstrap Runtime Error", new Dictionary<string, string> {
                { "Exception", ex.Message }
            });
        }
    }
这将成功加载AppBootloader,使用集合填充NavStack,以及该集合中的每个项(目前仅为loginViewModel)我在这里的示例提供了一个名称和一个完全填充的LoginViewModel,当它被导航到时,仍然会保留使用的LoginName和密码

ReactiveUI团队中的任何人都对如何实现这一点有任何意见吗?是否有更好的方法来实现这一点

顺便说一句,任何想在xamarin中将其用于UWP的人,我必须将其添加到UWP App.CS类文件中

    private AutoSuspendHelper autoSuspendHelper;

    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        autoSuspendHelper = new AutoSuspendHelper(this);
        this.InitializeComponent();
        this.Suspending += OnSuspending;
    }

    /// <summary>
    /// Invoked when the application is launched normally by the end user.  Other entry points
    /// will be used such as when the application is launched to open a specific file.
    /// </summary>
    /// <param name="args">Details about the launch request and process.</param>
    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        autoSuspendHelper.OnLaunched(args);

        ....
    }
专用AutoSuspendHelper AutoSuspendHelper;
/// 
///初始化singleton应用程序对象。这是编写代码的第一行
///已执行,因此是main()或WinMain()的逻辑等价物。
/// 
公共应用程序()
{
autoSuspendHelper=新的autoSuspendHelper(此);
this.InitializeComponent();
这个.Suspending+=OnSuspending;
}
/// 
///当最终用户正常启动应用程序时调用。其他入口点
///将在启动应用程序以打开特定文件时使用。
/// 
///有关启动请求和过程的详细信息。
仅启动受保护的覆盖无效(启动ActivatedEventArgs args)
{
autoSuspendHelper.OnLaunched(args);
....
}

是的,我已经阅读了手册,并且能够实现一些数据存储和检索。但是我不确定我应该如何处理AkavacheSuspensionDriver。让我在下面发布和示例
public class NavStackPersistence
{
    public string Name { get; set; }
    public string Path { get; set; }
    public LoginViewModel ViewModel { get; set; }
}
    private AutoSuspendHelper autoSuspendHelper;

    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        autoSuspendHelper = new AutoSuspendHelper(this);
        this.InitializeComponent();
        this.Suspending += OnSuspending;
    }

    /// <summary>
    /// Invoked when the application is launched normally by the end user.  Other entry points
    /// will be used such as when the application is launched to open a specific file.
    /// </summary>
    /// <param name="args">Details about the launch request and process.</param>
    protected override void OnLaunched(LaunchActivatedEventArgs args)
    {
        autoSuspendHelper.OnLaunched(args);

        ....
    }