如何根据mvvm数据移动窗口?

时间:2019-06-04 21:32:58

标签: c# wpf mvvm

我有一个分布式应用程序,可以运行同一应用程序的许多实例。每个实例的启动位置由进程的开始时间设置,以定位在窗口网格中。每个实例都接受来自数据库队列的处理请求,它们由数据库存储过程管理。使用的第三方库是内存和计算密集型的,每隔几百个请求就会使应用程序随机崩溃。有崩溃恢复功能,当检测到崩溃时,将崩溃进程的请求重新排队到仍在运行的应用程序实例。崩溃恢复的工作原理是将每个实例定期写入数据库日志文件。如果未在定义的时间范围内将应用程序的实例写入日志,则会检测到崩溃并启动恢复。随着时间的流逝,新的应用程序实例开始替换崩溃的实例,并且按开始时间在开始位置可以将它们放置在现有实例窗口的顶部。目标是根据正在运行的实例的开始时间,定期将实例重新定位到新的开始时间顺序。数据库日志条目将更新为文本框,并且通过MVVM模式进行绑定。我需要根据转到“文本框”的值来重新定位窗口,以便它们按仍在运行的实例的新的开始时间顺序移动位置。示例基于GUI事件而不是数据绑定。我不知道该怎么做。

我无法找到实现此目的的方法

使用此代码设置初始位置

public MainWindow()
{
    InitializeComponent();

    this.Closing += Window_Closing;


    WindowStartupLocation = WindowStartupLocation.Manual;

    // get window hieght and width
    _windowHeight = Convert.ToInt32(ConfigurationManager.AppSettings["AppHeight"]);
    _windowWidth = Convert.ToInt32(ConfigurationManager.AppSettings["AppWidth"]);

    // get count of apps to fit on screen
    _downAppCount = (int)Math.Floor(System.Windows.SystemParameters.PrimaryScreenHeight / ( _windowHeight + 1 ));
    _acrossAppCount = (int)Math.Floor(System.Windows.SystemParameters.PrimaryScreenWidth / ( _windowWidth + 1 ));

    // get the start order of this process relative to all running to determine screen position
    _currentProcess = Process.GetCurrentProcess();
    int i = 0;
    foreach ( Process proc in Process.GetProcessesByName(_currentProcess.ProcessName).OrderBy(x => x.StartTime).ToArray() )
    {
        i++;
        if ( proc.Id == _currentProcess.Id )
        {
            _processOrder = i;
        }
    }

    // set postion on screen
    this.Top = Math.Floor(( (double)_processOrder - 1 ) / _acrossAppCount ) * ( _windowHeight + 1 );
    this.Left =          (( (double)_processOrder - 1 ) % _acrossAppCount ) * ( _windowWidth  + 1 );

}

我希望每当应用程序使请求出队且消息发送到TextBox时,都会更改每个实例的位置。

0 个答案:

没有答案
相关问题