UserControl throws System.ArgumentException

本文关键字:ArgumentException System throws UserControl | 更新日期: 2023-09-27 18:37:05

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

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

这是我的代码

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)));
  }
}

最后是我如何使用用户控件

        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 类来获取随机名称,但不起作用

如果有人可以帮助我,请。

谢谢最好的问候。

UserControl throws System.ArgumentException

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

我已经解决了这个问题。我只是在我的网格中添加一个新名称,现在它可以正常工作: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
    }