如何在 Wpf 应用程序中保持两个 window 之间的会话?
How to maintain session in Wpf Application between two window?
我有 WPF
个申请,其中有一个注册 window。
当我单击注册选项卡时,它会打开注册 window。
我的要求是,如果在单击注册选项卡后没有 activity,它应该超时并移动到不同的 window。
您可以将值放入 Application.Current.Properties
以在应用程序中使用它。
Application.Current.Resources["ResourceName"] = "SomeData";
The general strategy for storing such data is to have public classes
with public properties/fields and access them from anywhere in the
application. However, with Windows Presentation Foundation (WPF), the
framework itself provides an application-wide “storage bag”,
Application.Properties, that could be used for the very same purpose.
This bag is an app-domain specific thread-safe key-value based
IDictionary instance.
我认为您需要检查 window
上的空闲时间
关于 windows 加载事件
private static DispatcherTimer idleTimer;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
idleTimer = new DispatcherTimer();
idleTimer.Interval = TimeSpan.FromSeconds(5);
idleTimer.Tick += this.OnTimerTick;
idleTimer.Start();
}
计时器滴答事件
private void OnTimerTick(object sender, EventArgs e)
{
uint idleTime = this.GetIdleTime();
if (idleTime > 5000)
{
this.Close();
}
}
空闲时间助手方法
[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO dummy);
private uint GetIdleTime()
{
LASTINPUTINFO lastUserAction = new LASTINPUTINFO();
lastUserAction.CbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastUserAction);
GetLastInputInfo(ref lastUserAction);
return (uint)Environment.TickCount - lastUserAction.DwTime;
}
internal struct LASTINPUTINFO
{
public uint CbSize;
public uint DwTime;
}
我有 WPF
个申请,其中有一个注册 window。
当我单击注册选项卡时,它会打开注册 window。
我的要求是,如果在单击注册选项卡后没有 activity,它应该超时并移动到不同的 window。
您可以将值放入 Application.Current.Properties
以在应用程序中使用它。
Application.Current.Resources["ResourceName"] = "SomeData";
The general strategy for storing such data is to have public classes with public properties/fields and access them from anywhere in the application. However, with Windows Presentation Foundation (WPF), the framework itself provides an application-wide “storage bag”, Application.Properties, that could be used for the very same purpose. This bag is an app-domain specific thread-safe key-value based IDictionary instance.
我认为您需要检查 window
上的空闲时间关于 windows 加载事件
private static DispatcherTimer idleTimer;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
idleTimer = new DispatcherTimer();
idleTimer.Interval = TimeSpan.FromSeconds(5);
idleTimer.Tick += this.OnTimerTick;
idleTimer.Start();
}
计时器滴答事件
private void OnTimerTick(object sender, EventArgs e)
{
uint idleTime = this.GetIdleTime();
if (idleTime > 5000)
{
this.Close();
}
}
空闲时间助手方法
[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO dummy);
private uint GetIdleTime()
{
LASTINPUTINFO lastUserAction = new LASTINPUTINFO();
lastUserAction.CbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastUserAction);
GetLastInputInfo(ref lastUserAction);
return (uint)Environment.TickCount - lastUserAction.DwTime;
}
internal struct LASTINPUTINFO
{
public uint CbSize;
public uint DwTime;
}