【问题标题】:Refresh Silverlight UserControl via XAML通过 XAML 刷新 Silverlight UserControl
【发布时间】:2009-05-26 14:05:08
【问题描述】:

我在 Visual Studio 2008 中使用最新版本的 Silverlight 2.0。我有一个简单的 Silverlight UserControl,代码如下:

public partial class SilverlightControl1 : UserControl
{
    public SilverlightControl1()
    {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(SilverlightControl1_Loaded);
        Composite = new Composite();
    }

    void SilverlightControl1_Loaded(object sender, RoutedEventArgs e)
    {
        Composite.Width = this.Width / 2.0;
        Composite.Height = this.Height / 2.0;
        if (!this.LayoutRoot.Children.Contains(Composite)) 
            this.LayoutRoot.Children.Add(Composite);
    }

    public Composite Composite
    {
        get;
        set;
    }
}

public class Composite : ContentControl
{
    private Grid grid;
    private Canvas canvas;

    public Composite()
    {
        if (grid == null) grid = new Grid();
        if (canvas == null) canvas = new Canvas();
        if (!grid.Children.Contains(canvas)) 
            grid.Children.Add(canvas);
        Content = grid;
        this.Loaded += new RoutedEventHandler(Composite_Loaded);
    }

    private Rectangle rectangle;

    void Composite_Loaded(object sender, RoutedEventArgs e)
    {
        if (rectangle == null) rectangle = new Rectangle();
        Canvas.SetTop(rectangle, 0);
        Canvas.SetLeft(rectangle, 0);
        rectangle.Fill = new SolidColorBrush(Color);
        rectangle.Width = Width;
        rectangle.Height = Height;
        if (!canvas.Children.Contains(rectangle)) 
            canvas.Children.Add(rectangle);
    }

    public Color Color
    {
        get;
        set;
    }
}

然后我在 Silverlight 应用程序中使用此 UserControl,页面的 XAML 如下所示:

<UserControl x:Class="SilverlightApplication1.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:test="clr-namespace:SilverlightClassLibrary1;assembly=SilverlightClassLibrary1"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="Green">
    <test:SilverlightControl1 Name="uControl1">
      <test:SilverlightControl1.Composite>
        <test:Composite Color="Yellow"/>
      </test:SilverlightControl1.Composite>
    </test:SilverlightControl1>
    </Grid>
</UserControl>

我的问题是:我必须在上面添加什么代码,以便通过将“复合颜色”更改为黄色以外的其他颜色并点击返回按钮,UserControl 会自动刷新?正如代码所示,刷新 UserControl 的唯一方法是在 VS2008 IDE 中移动滑块栏,这会更改 Silverlight 页面的缩放百分比。一个附带的问题,虽然对上述问题不太重要,但是:使用上面的代码,为什么我不能更改 LayoutRoot 的“背景”颜色?如果我删除我的 UserControl,它会按预期工作。

【问题讨论】:

    标签: silverlight xaml refresh silverlight-2.0


    【解决方案1】:

    解决方案有两个。首先是在 LayoutUpdated 事件而不是 Loaded 事件中进行更改,其次是订阅 PropertyMetadata 的 PropertyChangedCallback。这是完整的工作代码:

      public partial class SilverlightControl1 : UserControl
      {
        public SilverlightControl1()
        {
          InitializeComponent();
          this.LayoutUpdated += new EventHandler(SilverlightControl1_LayoutUpdated);
          Composite = new Composite();
        }
    
        void SilverlightControl1_LayoutUpdated(object sender, EventArgs e)
        {
          Composite.Width = this.Width / 2.0;
          Composite.Height = this.Height / 2.0;
          if (!this.LayoutRoot.Children.Contains(Composite)) this.LayoutRoot.Children.Add(Composite);
        }
    
        public Composite Composite
        {
          get;
          set;
        }
      }
    
      public class Composite : ContentControl
      {
        private Grid grid;
        private Canvas canvas;
    
        public Composite()
        {
          if (grid == null) grid = new Grid();
          if (canvas == null) canvas = new Canvas();
          if (!grid.Children.Contains(canvas)) grid.Children.Add(canvas);
          Content = grid;
          this.LayoutUpdated += new EventHandler(Composite_LayoutUpdated);
        }
    
        void Composite_LayoutUpdated(object sender, EventArgs e)
        {
          if (rectangle == null) rectangle = new Rectangle();
          Canvas.SetTop(rectangle, 0);
          Canvas.SetLeft(rectangle, 0);
          rectangle.Fill = new SolidColorBrush(Color);
    
          rectangle.Width = Width;
          rectangle.Height = Height;
          if (!canvas.Children.Contains(rectangle)) canvas.Children.Add(rectangle);
        }
    
        public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(Composite), new PropertyMetadata(Colors.Red, new PropertyChangedCallback(OnColorPropertyChanged)));
    
        private static void OnColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
          Composite comp = (Composite)d;
          comp.InvalidateArrange();
        }
    
        private Rectangle rectangle;
    
        public Color Color
        {
          get { return (Color)GetValue(ColorProperty); }
          set { SetValue(ColorProperty, value); }
        }
      }
    

    【讨论】:

      【解决方案2】:

      我认为你需要将 Composite 变成一个依赖属性。可能会希望对 Color on Composite 执行相同的操作,以便您能够绑定它。

      【讨论】:

      • 感谢您的回答,Correl!正如您所建议的,我已将 Composite 和 Color 属性实现为 Dependency 属性,但不幸的是,这对问题没有任何影响!
      • 查看代码,我认为这里的问题是您在 OnLoaded 事件中设置颜色。当您更改颜色时,您不会强制 VS 重新加载控件,因此不会再次触发该事件。不要将 Rectangle 的 Fill 设置为 Color 的值,而是尝试绑定它。这样,当 Color 发生变化时,Dependency 属性就会触发并更新它。
      • 感谢 Correl!我使用 DependencyObject.GetValue 和 SetValue 将 Color 属性实现为 Dependency 属性。当我设置 Rectangle 的填充时,Color 属性因此已经绑定到依赖属性,不是吗?
      • 不完全。要实际绑定它,您需要创建一个绑定。尝试以下代码,而不是使用 rectangle.Fill = new SolidColorBrush(Color): Binding binding = new Binding(); binding.Source = this; binding.Path = new PropertyPath("颜色"); rectangle.SetBinding(Rectangle.FillProperty, binding);
      猜你喜欢
      • 2010-10-29
      • 1970-01-01
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      相关资源
      最近更新 更多