0

我在 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,它会按预期工作。

4

2 回答 2

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); }
    }
  }
于 2009-05-29T09:10:50.147 回答
0

我认为您需要将 Composite 变成依赖属性。可能会希望对复合颜色上的颜色做同样的事情,以便您能够绑定它。

于 2009-05-26T14:20:59.597 回答