【问题标题】:INotifyPropertyChanged, no UI updatesINotifyPropertyChanged,没有 UI 更新
【发布时间】:2011-09-30 15:09:26
【问题描述】:

我在网上四处寻找答案,但似乎无法让它发挥作用。这是我所拥有的:

public class UIValues : INotifyPropertyChanged
    {
        private double zoomValue = 1;

        private static readonly UIValues instance = new UIValues();

        public event PropertyChangedEventHandler PropertyChanged;

        internal static UIValues Instance { get { return instance; } }

        internal double ZoomValue
        {
            get { return zoomValue; }
            set
            {
                if (this.zoomValue == value)
                    return;

                this.zoomValue = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("ZoomValue"));
            }
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, e);
        }
    }

然后我有这个:

<UserControl>
    <UserControl.DataContext>
            <local:UIValues x:Name="uiValues"/>
        </UserControl.DataContext>

.
.
.

                <Viewbox x:Name="vbViewBox" RenderTransformOrigin="0.5,0.5">
                    <local:ImageControl x:Name="imgControl" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    <Viewbox.RenderTransform>
                            <TransformGroup>
                                    <CompositeTransform x:Name="trCompositeTransform" ScaleX="{Binding ZoomValue}" ScaleY="{Binding ZoomValue}" Rotation="0" SkewX="0" SkewY="0"/>
                            </TransformGroup>
                    </Viewbox.RenderTransform>
                </Viewbox>
</UserControl>

所以基本上,每当我从代码隐藏的 UIValues 类中更改 ZoomValue 时,UI 都不会更新。

有人知道为什么吗?

谢谢!

【问题讨论】:

    标签: silverlight inotifypropertychanged


    【解决方案1】:

    看看贴出的代码,我猜你有这样的东西来改变缩放值。

      UIValues.Instance.ZoomValue = x;
    

    问题是这个 xaml:-

      <local:UIValues x:Name="uiValues"/> 
    

    构造一个独立的 UIValues 实例,该实例与静态 Instance 属性返回的实例不同。因此,您将更改一个对象上没有任何东西在听的值。

    编辑

    ZoomLevel 也是 internal,因为它必须是公共的绑定。

    解决办法是使用IApplicationService,通过App.xaml来做事。

    将你的班级改为:

    public class UIValues : INotifyPropertyChanged,  IApplicationService
    {
        private double zoomValue = 1;
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        internal static UIValues Instance { get; private set; }
    
        public double ZoomValue
        {
            get { return zoomValue; }
            set
            {
                if (zoomValue == value)
                    return;
    
                zoomValue = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("ZoomValue"));
            }
        }
    
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, e);
        }
    
        void IApplicationService.StartService(ApplicationServiceContext context)
        {
            Instance = this;
            Application.Current.Resources.Add("UIValues", Instance);
        }
    
        void IApplicationService.StopService() { }
    }
    

    UIValues的实例添加到App.Xaml:-

    <Application.ApplicationLifetimeObjects>
        <local:UIValues />
    </Application.ApplicationLifetimeObjects>
    

    然后像这样设置用户控件DataContext:-

    <UserControl ... DataContext="{StaticResource UIValues}"> 
    

    也就是说,这是对您可能需要绑定到真实数据的DataContext 的浪费。您可以直接在绑定上指定Source:-

     <CompositeTransform ScaleX="{Binding ZoomValue, Source={StaticResource UIValues}}" ScaleY="{Binding ZoomValue, Source={StaticResource UIValues}}" Rotation="0" SkewX="0" SkewY="0"/>
    

    【讨论】:

      【解决方案2】:

      ZoomValue 属性的访问修饰符更改为public,一切都会正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-12
        相关资源
        最近更新 更多