UserControl抛出System.ArgumentException

时间:2013-10-27 17:07:03

标签: c# silverlight windows-phone-7 windows-phone-8 windows-phone

您好我正在使用userControl它工作正常但是当我第二次尝试使用它时会抛出下一个异常

System.Windows.dll中发生未处理的“System.ArgumentException”类型异常 附加信息:参数不正确。

这是我的代码

XAML

    <Grid x:Name="LayoutRoot">
    <TextBlock x:Name="tb1" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=me}" TextWrapping="Wrap" />
    <TextBlock x:Name="tb2" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap"  />
    <TextBlock x:Name="tb3" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap" />
    <TextBlock x:Name="tb4" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap" />
    <TextBlock x:Name="tb5" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap" />
    <TextBlock x:Name="tb6" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap" />
    <TextBlock x:Name="tb7" FontWeight="ExtraBold" Foreground="Black" Opacity="0.7" Margin="1,3,0,0" Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap" />
    <TextBlock x:Name="tb8" FontWeight="ExtraBold" Foreground="White"  Text="{Binding Text,ElementName=tb1}" TextWrapping="Wrap" />
  </Grid>

C#

  public partial class DropShadowTextBlock : UserControl
  {
    public DropShadowTextBlock()
    {
        InitializeComponent();
    }

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public SolidColorBrush Foreground
    {
        get { return (SolidColorBrush)GetValue(ForegroundProperty); }
        set { SetValue(ForegroundProperty,value);}
    }


    public static void ForegroundPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        DropShadowTextBlock control = sender as DropShadowTextBlock;
        if (control != null)
        {
            control.tb8.Foreground = e.NewValue as SolidColorBrush;
        }

    }
   public static void TextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
   {
            var control = sender as DropShadowTextBlock;
            if (control.tb1 != null)
            control.tb1.Text = e.NewValue.ToString();
   }


    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(DropShadowTextBlock), new PropertyMetadata(new PropertyChangedCallback(TextPropertyChanged)));
    public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register("Foreground", typeof(SolidColorBrush), typeof(DropShadowTextBlock), new PropertyMetadata(new PropertyChangedCallback(ForegroundPropertyChanged)));

  }
}

最后这是我如何使用userControl

        public void dibujarTexto(String text)
        {
          DropShadowTextBlock textblockTop = new DropShadowTextBlock();
          textblockTop.Text = text;
          textblockTop.Foreground = new SolidColorBrush(Colors.White);
          textblockTop.Name = Guid.NewGuid().ToString();
          migrid.Children.Add(textblockTop); //this is a grid control
        }

我一直在分享,我发现控件名称必须是唯一的,所以我使用Guid类来获取随机名称但不起作用

请有人帮助我。

非常感谢。

2 个答案:

答案 0 :(得分:0)

因此,控件的名称是唯一的,但是您是否指定了用户控件的id(在顶部的xaml代码中)?如果是,请删除此行。

答案 1 :(得分:0)

我已经解决了这个问题。我只是为我的网格添加一个新名称,现在它可以正常工作:D

 public void dibujarTexto(String text)
    {
      DropShadowTextBlock textblockTop = new DropShadowTextBlock();
      textblockTop.Text = text;
      textblockTop.Foreground = new SolidColorBrush(Colors.White);
      textblockTop.Name = Guid.NewGuid().ToString();
      migrid.Name= Guid.NewGuid().ToString();
      migrid.Children.Add(textblockTop); //this is a grid control
    }