如何追踪窗口的位置?

问题描述 投票:0回答:1

帮助实施。我为窗口的大小编写了跟踪代码。然后我决定添加窗口位置的跟踪。我尝试实现它,但没有成功。

    public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainWindowViewModel();
        
        this.WhenAnyValue(window => window.Bounds)
            .Subscribe(bounds =>
            {
                var vm = (MainWindowViewModel)this.DataContext;
                vm.WindowResized(bounds);
                vm.WindowInfo = $"Position: {bounds.Position.X}, {bounds.Position.Y} | Size: {bounds.Width} x {bounds.Height}";
            });
    }

public class MainWindowViewModel : ReactiveObject
{
    private double _width;
    private double _height;
    private double _x;
    private double _y;

    public double Width
    {
        set => this.RaiseAndSetIfChanged(ref _width, value);
    }

    public double Height
    {
        set => this.RaiseAndSetIfChanged(ref _height, value);
    }
    public double X
    {
        set => this.RaiseAndSetIfChanged(ref _x, value);
    }

    public double Y
    {
        set => this.RaiseAndSetIfChanged(ref _y, value);
    }
    public void WindowResized(Rect bounds)
    {
        Width = bounds.Width;
        Height = bounds.Height;
        X = bounds.Position.X;
        Y = bounds.Position.Y;
    }

    
    private string _windowInfo;

    public string WindowInfo
    {
        set => this.RaiseAndSetIfChanged(ref _windowInfo, value);
    }
    
}

我需要在移动窗口时更新位置。

c# mvvm reactiveui avaloniaui
1个回答
0
投票

我不太熟悉 AvaloniaUI 事件来命令选项,但一般来说你的窗口有 Position 属性和 PositionChanged 事件。您可以订阅此事件来获取更新的窗口位置

© www.soinside.com 2019 - 2024. All rights reserved.