简体   繁体   English

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

[英]Resizing a grid control programatically over a period of time

G'day, 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. 我正在尝试使用滑动选项卡模拟旧的XBox 360 GUI(还记得,您按向左或向右,内容是否会根据选项卡滑动?)无论如何,目前,我的工作情况很好,但是我无法使“动画”正常工作。

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

Now, my problem lies with resizing the parent grid control. 现在,我的问题在于调整父网格控件的大小。 When it is slid in, it is resized by calling mygrid.Width += 1; 滑入时,通过调用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. 而如果我在同一方法中调用this.Width + = 1(这是主程序窗口),则该窗口将调整我希望网格控件调整大小的方式。 I've tried UpdateLayout() but to no avail. 我已经尝试过UpdateLayout()但没有用。 This tells me my timing is okay. 这告诉我我的时间还可以。

If anyone could be of assistance, it would be greatly appreciated. 如果有人可以提供帮助,将不胜感激。

Here is my OpenWindow method... 这是我的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 is a Dictionary, Shell is the grid that I am attempting to animate when resizing. myDict是一个Dictionary,Shell是我在调整大小时尝试设置动画的网格。 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 Neried Web解决方案

Your OpenWindow method is happening on the Dispatcher thread. 您的OpenWindow方法发生在Dispatcher线程上。 That's also the thread responsible for rendering, so as long as your OpenWindow method doesn't return, nothing gets rendered. 这也是负责渲染的线程,因此只要您的OpenWindow方法不返回,就不会渲染任何内容。

The proper way to fix this would be to animate the Width property. 解决此问题的正确方法是设置Width属性的动画。 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: 我没有从代码开始制作动画的经验(过去我只使用它们来完成诸如鼠标悬停时的淡入按钮突出显示等操作,而这在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);

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

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