在一段时间内以编程方式调整网格控件的大小

发布于 2024-10-08 09:46:59 字数 1188 浏览 0 评论 0原文

天哪,

我正在尝试使用滑动选项卡模拟旧的 Xbox 360 GUI(记住,您会按向左或向右,内容会根据选项卡滑入?)无论如何,目前,我正在使用此功能好吧,但是我无法让“动画”工作。

当用户按下向左箭头或向右箭头时,将调用我的 OpenWindow(int iIndex) 方法,其中 iIndex 是要滑入的下一个或上一个“窗口”的索引。(不是窗口......每个“窗口”是一个结构体,其父网格控件包含一个按钮和一个较小的网格控件,其中包含窗口内容。)

现在,我的问题在于调整父网格控件的大小。当它滑入时,通过调用 mygrid.Width += 1 来调整它的大小;这可行,但我没有看到它在确定的时间段内发生,它只是滞后一点,然后调整大小到所需的宽度。而如果我以相同的方法调用 this.Width += 1 (这是主程序窗口),窗口将按照我希望网格控件调整大小的方式调整大小。我尝试过 UpdateLayout() 但无济于事。这告诉我我的时机没问题。

如果有人可以提供帮助,我们将不胜感激。

这是我的 OpenWindow 方法...

public void OpenWindow(int iIndex)
    {
        int iInterval = 1;
        for (int i = (int)myDict[iIndex].Shell.Width; i < (int)stack_outter.Width; i += iInterval)
        {                
            myDict[iIndex].Shell.Width += 1;
            myDict[iIndex].Shell.UpdateLayout();
            System.Threading.Thread.Sleep(1);                

        }

        myDict[iIndex].Shell.Width = stack_outter.Width - (BUTTON_WIDTH * (myDict.Count - 1));
    }

myDict 是字典,Shell 是我在调整大小时尝试设置动画的网格。抱歉,代码很乱,当我试图让东西工作时,我的代码总是被黑客攻击:)

谢谢,

Ash

Neried网络解决方案

G'day,

I am attempting to simulate the old XBox 360 GUI with the sliding tabs (Remember, you'd press left or right and the content would slide in depending on the tab?) Anyways, at the moment, I have this working well, however I cannot get the "animation" working.

When the user presses left arrow or right arrow, my OpenWindow(int iIndex) method will be called, where iIndex is the index to the next or previous "window" to be slid in. (Not a window... each "Window" is a struct with a parent grid control containing a button and a smaller grid control that contains the windows contents.)

Now, my problem lies with resizing the parent grid control. When it is slid in, it is resized by calling mygrid.Width += 1; That works, but I don't see it happen over a determined period of time, it just lags a bit and then resizes to the required width. Whereas if I call this.Width += 1 in the same method, (this being the main program window) the window resizes how I want the grid control to resize. I've tried UpdateLayout() but to no avail. This tells me my timing is okay.

If anyone could be of assistance, it would be greatly appreciated.

Here is my OpenWindow method...

public void OpenWindow(int iIndex)
    {
        int iInterval = 1;
        for (int i = (int)myDict[iIndex].Shell.Width; i < (int)stack_outter.Width; i += iInterval)
        {                
            myDict[iIndex].Shell.Width += 1;
            myDict[iIndex].Shell.UpdateLayout();
            System.Threading.Thread.Sleep(1);                

        }

        myDict[iIndex].Shell.Width = stack_outter.Width - (BUTTON_WIDTH * (myDict.Count - 1));
    }

myDict is a Dictionary, Shell is the grid that I am attempting to animate when resizing. Sorry about the code, it's messy, my code is always hacked when I am trying to get stuff working :)

Thanks,

Ash

Neried Web Solutions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云不回头 2024-10-15 09:46:59

您的 OpenWindow 方法发生在 Dispatcher 线程上。这也是负责渲染的线程,因此只要您的 OpenWindow 方法不返回,就不会渲染任何内容。

解决此问题的正确方法是为 Width 属性设置动画。我没有任何从代码启动动画的经验(过去我只将它们用于诸如鼠标悬停时淡入按钮突出显示之类的事情,这在 WPF 中更容易完成),但我快速阅读了一遍-通过此页面,MSDN 上的动画概述,我认为您'会想要这样的东西:

        DoubleAnimation myDoubleAnimation = new DoubleAnimation();
        myDoubleAnimation.From = myDict[iIndex].Shell.Width;
        myDoubleAnimation.To = stack_outter.Width;
        myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));
        myDoubleAnimation.AutoReverse = false;
        myDoubleAnimation.RepeatBehavior = new RepeatBehavior(1.0);

        myStoryboard = new Storyboard();
        myStoryboard.Children.Add(myDoubleAnimation);
        Storyboard.SetTarget(myDoubleAnimation, myDict[iIndex].Shell);
        Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(FrameworkElement.WidthProperty));
        myStoryboard.Begin(myDict[iIndex].Shell);

Your OpenWindow method is happening on the Dispatcher thread. That's also the thread responsible for rendering, so as long as your OpenWindow method doesn't return, nothing gets rendered.

The proper way to fix this would be to animate the Width property. I don't have any experience in starting animations from code (I've only used them in the past for things like a fade-in button highlight on mouse over, which is more easily done from WPF), but I took a quick read-through this page, Animation Overview on MSDN, and I think you'll want something like this:

        DoubleAnimation myDoubleAnimation = new DoubleAnimation();
        myDoubleAnimation.From = myDict[iIndex].Shell.Width;
        myDoubleAnimation.To = stack_outter.Width;
        myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));
        myDoubleAnimation.AutoReverse = false;
        myDoubleAnimation.RepeatBehavior = new RepeatBehavior(1.0);

        myStoryboard = new Storyboard();
        myStoryboard.Children.Add(myDoubleAnimation);
        Storyboard.SetTarget(myDoubleAnimation, myDict[iIndex].Shell);
        Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(FrameworkElement.WidthProperty));
        myStoryboard.Begin(myDict[iIndex].Shell);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文