2

トゥームストーンの後で、ページの状態をアプリの標準ページに復元する方法があるかどうか疑問に思っています。たとえば、ユーザーがページ 2 にいるときにアプリが廃棄された場合、復元されたページは常にページ 1 になります。アプリでページ 2 からデータを復元する方法に問題があり、この方法を代替手段として使用することを考えています。問題。

public FeedPage()
{
    InitializeComponent();
    this.Loaded += new RoutedEventHandler(FeedPage_Loaded);
    performanceProgressBar.Visibility = System.Windows.Visibility.Collapsed;
    LoadFeed();
}

private void LoadFeed()
{
    FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
    var currentFeed = root.DataContext as FeedViewModel;
    WebClient client = new WebClient();
    performanceProgressBar.IsIndeterminate = true;
    performanceProgressBar.Visibility = System.Windows.Visibility.Visible;
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
    client.DownloadStringAsync(new Uri(currentFeed.FeedUrl)); 
    this.DataContext = currentFeed;
    ApplicationBar.IsVisible = false;    
}

...

private void FeedPage_Loaded(object sender, RoutedEventArgs e)
{
    if (!App.ViewModel.IsDataLoaded)
    {
        App.ViewModel.LoadData();
    }
}

私の問題は、tobstone 中に currentFeed を保存する方法がわからないことです。アプリがアクティブ化されるたびに、currentFeed は null になります。私は多くの解決策を試しましたが、結果はありませんでした。

public partial class App : Application
{
    private static MainViewModel viewModel = null;

    /// <summary>
    /// A static ViewModel used by the views to bind against.
    /// </summary>
    /// <returns>The MainViewModel object.</returns>
    public static MainViewModel ViewModel
    {
       get
        {
            // Delay creation of the view model until necessary
            if (viewModel == null)
                viewModel = new MainViewModel();

            return viewModel;
        }

    }


    /// <summary>
    /// Provides easy access to the root frame of the Phone Application.
    /// </summary>
    /// <returns>The root frame of the Phone Application.</returns>
    public PhoneApplicationFrame RootFrame { get; private set; }

    /// <summary>
    /// Constructor for the Application object.
    /// </summary>
    public App()
    {
        // Global handler for uncaught exceptions. 
        UnhandledException += Application_UnhandledException;

        // Show graphics profiling information while debugging.
        if (System.Diagnostics.Debugger.IsAttached)
        {
            // Display the current frame rate counters.
            Application.Current.Host.Settings.EnableFrameRateCounter = true;

            // Show the areas of the app that are being redrawn in each frame.
            //Application.Current.Host.Settings.EnableRedrawRegions = true;

            // Enable non-production analysis visualization mode, 
            // which shows areas of a page that are being GPU accelerated with a colored overlay.
            //Application.Current.Host.Settings.EnableCacheVisualization = true;
        }

        // Standard Silverlight initialization
        InitializeComponent();

        // Phone-specific initialization
        InitializePhoneApplication();
    }

    // Code to execute when the application is launching (eg, from Start)
    // This code will not execute when the application is reactivated
    private void Application_Launching(object sender, LaunchingEventArgs e)
    {
    }

    // Code to execute when the application is activated (brought to foreground)
    // This code will not execute when the application is first launched
    private void Application_Activated(object sender, ActivatedEventArgs e)
    {
    }

    // Code to execute when the application is deactivated (sent to background)
    // This code will not execute when the application is closing
    private void Application_Deactivated(object sender, DeactivatedEventArgs e)
    {
    }

    // Code to execute when the application is closing (eg, user hit Back)
    // This code will not execute when the application is deactivated
    private void Application_Closing(object sender, ClosingEventArgs e)
    {

    }


    // Code to execute if a navigation fails
    private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
    {
        if (System.Diagnostics.Debugger.IsAttached)
        {
            // A navigation has failed; break into the debugger
            System.Diagnostics.Debugger.Break();
        }
    }

    // Code to execute on Unhandled Exceptions
    private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
    {
        if (System.Diagnostics.Debugger.IsAttached)
        {
            // An unhandled exception has occurred; break into the debugger
            System.Diagnostics.Debugger.Break();
        }
    }

    #region Phone application initialization

    // Avoid double-initialization
    private bool phoneApplicationInitialized = false;

    // Do not add any additional code to this method
    private void InitializePhoneApplication()
    {
        if (phoneApplicationInitialized)
            return;

        // Create the frame but don't set it as RootVisual yet; this allows the splash
        // screen to remain active until the application is ready to render.
        RootFrame = new PhoneApplicationFrame();
        RootFrame.Navigated += CompleteInitializePhoneApplication;

        // Handle navigation failures
        RootFrame.NavigationFailed += RootFrame_NavigationFailed;

        // Ensure we don't initialize again
        phoneApplicationInitialized = true;
    }

    // Do not add any additional code to this method
    private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
    {
        // Set the root visual to allow the application to render
        if (RootVisual != RootFrame)
            RootVisual = RootFrame;

        // Remove this handler since it is no longer needed
        RootFrame.Navigated -= CompleteInitializePhoneApplication;
    }

    #endregion
}

}

これは私の app.xaml.cs であり、変更はありません。このコードでは、トゥームストーンはメインページと詳細ページでは正常に機能しますが、(もちろん) フィードページでは機能しません。

4

2 に答える 2

0

アプリの2ページからデータを復元する方法に問題があり、この問題を解決するための代替手段としてこの方法を考えています。

あなたの本当の問題、これを解決してみませんか?あなたが求めているものがマーケットプレイス認定に合格する可能性は低く、一般的にユーザーエクスペリエンスが悪いと見なされます。

データの復元は非常に簡単です。したがって、それを行わない理由はありません。

アップデート

まあ、これはかなり疑わしいようです

FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
currentFeed = root.DataContext as FeedViewModel;

確かに、RootVisualは、表示しようとしているページ自体になりますか?それ以外の場合は、まったく読み込まれていない別のページです。

解決策は、からのDataContextを使用して回避することApplication.Current.RootVisualです。あなたが私に尋ねるならば、それは一般的に悪い習慣です。

したがって、ViewModelをStateプロパティに手動で永続化するか、別の方法で解決する必要があります(後者をお勧めします)。

しかし、基本的には、プラットフォーム用に開発する方法についてもっと読む必要があります。動作することが知られているいくつかの例/チュートリアルを見てください。

于 2011-11-01T11:54:42.943 に答える
0

App.xaml.cs の次のメソッドを見てください。

private void Application_Deactivated(object sender, DeactivatedEventArgs e)

private void Application_Activated(object sender, ActivatedEventArgs e)

ここは、分離ストレージとの間で状態データを保存および読み込む必要がある場所です。

これは、アプリを強制的に別のページに移動させる場所でもありますが、そうしないことを強くお勧めします。ユーザーはこの動作を予期していません。おそらく、マーケットプレイスで公開されることさえありません。これらのメソッドで状態を読み込んで保存するだけです。

これまでに何をしようとしましたか?問題はどこにあるのでしょうか?

于 2011-11-01T12:18:54.913 に答える