Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Win8在挂起后重新初始化MainWindow对象_C#_Windows 8_Microsoft Metro - Fatal编程技术网

C# Win8在挂起后重新初始化MainWindow对象

C# Win8在挂起后重新初始化MainWindow对象,c#,windows-8,microsoft-metro,C#,Windows 8,Microsoft Metro,“re init”主窗口对象设置有问题。我认为OnNavigatedTo也会在挂起后调用,我在主窗口中有一些代码,如: 受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e) { object.value=initValue; } 但在暂停后,它没有被调用。那么挂起后如何执行此操作呢?如果使用VS2012附带的默认模板,您将在App.xaml.cs文件中看到以下代码: protected override async void OnLaunched(La

“re init”主窗口对象设置有问题。我认为OnNavigatedTo也会在挂起后调用,我在主窗口中有一些代码,如: 受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e) { object.value=initValue; }


但在暂停后,它没有被调用。那么挂起后如何执行此操作呢?

如果使用VS2012附带的默认模板,您将在App.xaml.cs文件中看到以下代码:

    protected override async void OnLaunched(LaunchActivatedEventArgs args)
    {
        Frame rootFrame = Window.Current.Content as Frame;

        // ... took out some code here

            if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                // Restore the saved session state only when appropriate
                try
                {
                    await SuspensionManager.RestoreAsync();
                }
                catch (SuspensionManagerException)
                {
                    //Something went wrong restoring state.
                    //Assume there is no state and continue
                }
            }

        // ... took out some more code here    

        Window.Current.Activate();
    }
ApplicationExecutionState的可能值为

public enum ApplicationExecutionState
{
    // Summary:
    //     The app is not running.
    NotRunning = 0,
    //
    // Summary:
    //     The app is running.
    Running = 1,
    //
    // Summary:
    //     The app is suspended.
    Suspended = 2,
    //
    // Summary:
    //     The app was terminated after being suspended.
    Terminated = 3,
    //
    // Summary:
    //     The app was closed by the user.
    ClosedByUser = 4,
}
因此,只需为

if (args.PreviousExecutionState == ApplicationExecutionState.Suspended)
执行挂起状态后要执行的代码

要恢复页面本身中以前的状态,请使用在每个页面的LayoutWarePage基类上定义的LoadState和SaveState方法(或实现您自己的状态管理)。VS2012附带的模板(例如网格应用程序)已经使用了所有这些技巧进行状态管理,这是一个很好的入门方法

protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
protected override void SaveState(Dictionary<String, Object> pageState)
protected override void LoadState(对象导航参数,字典页面状态)
受保护的覆盖无效保存状态(字典页面状态)

谢谢,但我如何才能直接访问MainWindow对象?您应该尽量避免从app.xaml.cs文件访问MainWindow对象,并使用示例提供的状态管理(或您自己的状态管理)。因此,我可以在SaveState和LoadState中的init中执行一些停止操作吗?确切地说,通常,您会在那里保存/加载一些数据。请记住,要异步执行此操作,并使用INotifyPropertyChanged事件为加载实现正确的数据绑定。非常奇怪,但当我挂起应用程序时,这些方法没有调用