简体   繁体   English

如果C#中的窗口大小发生变化,如何调用方法

[英]How can I invoke a method if the Window size changes in C#

While I know a bit about event handlers, but not really enough about how to impement them. 虽然我对事件处理程序有所了解,但对如何强制使用事件处理程序还不够了解。

So the question is: How can I call my method 所以问题是:如何调用我的方法

redrawWidgets();

without manually checking everytime if the main window changed size? 无需每次都手动检查主窗口是否已更改大小?

Note that I did not use XAML and instead coded the GUI by hand, since that how I stupidally started this project. 请注意,我没有使用XAML,而是手动编写了GUI,因为我是如何愚蠢地启动这个项目的。 The next one I will definetly do with XAML. 下一个我将明确使用XAML。

My mainWindow looks as follows: 我的mainWindow如下所示:

using AlgorithmComparer.src.View.Main;
using System.Windows;
using AlgorithmComparer.src.Model;
using System.Collections.Generic;
using System;
using System.Windows.Threading;

namespace AlgorithmComparer.src.View
{
    public class MainWindow : Window, IView
    {
        ControllerClass _controller;

        private AlgorithmRuntimeView _leftAlgorithmRuntimeView;
        private AlgorithmRuntimeView _rightAlgorithmRuntimeView;
        public MainWindow(ControllerClass controller, AlgorithmRuntimeView leftAlgorithmRuntimeView, AlgorithmRuntimeView rightAlgorithmRuntimeView)
        {
            this._leftAlgorithmRuntimeView = leftAlgorithmRuntimeView;
            this._rightAlgorithmRuntimeView = rightAlgorithmRuntimeView;
            this._controller = controller;
            Title = "Algorithm Comparer";
            Height = 530;
            Width = 800;
            InitLayout();
        }

        public void InitLayout()
        {
            Content = new MainView(_controller, _leftAlgorithmRuntimeView, _rightAlgorithmRuntimeView);
        }

        public int[] getWindowSize()
        {
            return (new int[2] { (int)this.ActualHeight, (int)this.ActualWidth });
        }
    }
}

You dont need to invoce this method yourself. 您不需要自己调用此方法。 The Windowclass already offers you this functionallity with the SizeChanged event. Window类已经通过SizeChanged事件为您提供了此功能。 You can use this bit of code to make use of it: 您可以使用以下代码来利用它:

    YourWindow.SizeChanged += functionName;
    //If you are in your code behind use:
    //this.SizeChanged += functionName;

    private void functionName(object sender, SizeChangedEventArgs e)
    {
        //do what you want to do if size changed 
    }

In your window's constructor, you can hook up to the SizeChanged event: 在窗口的构造函数中,您可以连接到SizeChanged事件:

SizeChanged += window_SizeChanged;

You'll get a handler method that you can make your call in. Be warned: this will fire very rapidly whenever the size changes, as it is changing, and for any reason it changes. 您将获得一个可以调用的处理程序方法。请注意:每当大小更改,更改或出于任何原因更改时,此方法都会非常迅速地触发。 It'll fire at launch as the window is first being sized, for instance. 例如,它会在启动时触发,因为首先要调整窗口的大小。 On minimize, maximize, edge drag...everything. 在最小化,最大化,边缘拖动...所有方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM