如何刷新当前状态VisualStateManager [英] How to refresh the current state with VisualStateManager

查看:143
本文介绍了如何刷新当前状态VisualStateManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Silverlight 4应用程序,我在XAML定义了一些状态,并使用 VisualStateManager.GoToState(这一点,Statename的,FALSE)这如预期的工作。

但问题是,一些国家的属性被绑定到用户界面,使用户可以自定义的东西,如颜色。这是可能做到这一点时,在该状态(可以说状态A)。该变化不会反映,直到我换到另一种状态,然后回到状态答:问题不在于从用户界面到基础的属性绑定(它们具有预期值),而国家需要被刷新 - 或至少这是我的结论。

起初,我只是想 VisualStateManager.GoToState(这一点,StateA,FALSE),但发现,<一个href="http://msdn.microsoft.com/en-us/library/system.windows.visualstatemanager.gotostate%28v=vs.95%29.aspx"相对=nofollow>如果控制已经在Statename的状态,GoToState不执行任何操作返回true,。

我又试图没有成功,虽然它真的不是一个很好的解决方案:

  VisualStateManager.GoToState(这一点,StateB,假);
VisualStateManager.GoToState(这一点,StateA,假);
 

有谁知道我怎么能强制刷新,或者一种解决方法?

更新: 我设法得到它与下面的答案,使用下面的code当依赖项属性修改相结合的工作。该 Storyboard.Stop() Storyboard.Begin()似乎考虑到设置的属性的新值从UI

 如果(VisualStateManager.GoToState(这一点,StateA,FALSE))
{
    的VisualState stateA =(的VisualState)VisualStateGroup.States [0];
    stateA.Storyboard.Stop();
    stateA.Storyboard.Begin();
}
 

解决方案

这code应该工作:

  VisualStateManager.GoToState(这一点,StateB,假);
VisualStateManager.GoToState(这一点,StateA,假);
 

这是一个典型的Silverlight / WPF的方式来强制刷新。虽然看起来哈克,我已经看到了微软的Silverlight工具包code此完全相同的方法。

现在,我的猜测是,该控件没有加载或者您不调用状态改变时的属性改变。

您需要做两件事情:

  1. 在OnApplyTemplate或控件的加载情况下,把你的控制到正常状态。 DependencyProperties经常更新前的控制实际上完成加载的模板。在这种情况下,即使你叫GoToState,没有模板,所以它不会做任何事情,因为没有模板。

  2. 确认,在属性更改处理程序依赖项属性,您呼叫的状态变化。

有关您参考,您注册的PropertyChanged处理程序是这样的:

 公共静态只读的DependencyProperty MinValueProperty =
        DependencyProperty.Register(MINVALUE的typeof(双),typeof运算(ScaleValueConverter),新PropertyMetadata(0.0D,OnMinValuePropertyChanged));
 

I have a Silverlight 4 application where I have some states defined in XAML, and use VisualStateManager.GoToState(this, "stateName", false) which works as expected.

The problem however is that some of the state's properties are bound to the UI so the user can customize things such as the colour. It is possible to do this when in that state (lets say state A). The changes do not reflect until I change to another state, and then back to state A. The problem is not the binding from the UI to the underlying properties (they have the expected values), but rather the state needs to be refreshed - or at least that is my conclusion.

At first I just tried VisualStateManager.GoToState(this, "StateA", false) but found out that "if the control is already in the stateName state, GoToState takes no action returns true".

I then tried with no success, although it is really not a nice solution:

VisualStateManager.GoToState(this, "StateB", false);
VisualStateManager.GoToState(this, "StateA", false);

Does anyone know how I can force a refresh, or a workaround?

UPDATE: I managed to get it to work with a combination of the answer below and using the following code when the dependency property changed. The Storyboard.Stop() and Storyboard.Begin() seemed to take into account the new value of the properties set from the UI.

if (VisualStateManager.GoToState(this, "StateA", false))
{
    VisualState stateA = (VisualState)VisualStateGroup.States[0];
    stateA.Storyboard.Stop();
    stateA.Storyboard.Begin();
}

解决方案

This code should work:

VisualStateManager.GoToState(this, "StateB", false);
VisualStateManager.GoToState(this, "StateA", false);

That is a typical Silverlight/WPF way to force a refresh. While it seems hacky, I've seen this exact same approach in Microsoft and Silverlight toolkit code.

Now, my guess is that the control hasn't loaded yet or you aren't invoking the state change when the properties change.

You need to do two things:

  1. In OnApplyTemplate or the control's loaded event, put your control into the proper state. DependencyProperties are often updated before the control actually finishes loading the templates. In this scenario, even if you called GoToState, there is no template so it won't do anything because there is no template.

  2. Make sure that in the Property Change handler for the dependency property, you are calling the state changes.

For you reference, you register a PropertyChanged handler like this:

 public static readonly DependencyProperty MinValueProperty =
        DependencyProperty.Register("MinValue", typeof(double), typeof(ScaleValueConverter), new PropertyMetadata(0.0d,OnMinValuePropertyChanged));

这篇关于如何刷新当前状态VisualStateManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆