UserControl抛出System.ArgumentException [英] UserControl throws System.ArgumentException

查看:60
本文介绍了UserControl抛出System.ArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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

Hi i'm using an userControl it works fine but when i try to use it for second time it throws the next exception


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


这是我的代码

Here's my code


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类来随机名字但不起作用

I've been sharing and i found that the control names must be unique so i'm using the Guid class to get random names but doesn't work


如果有人可以请帮助我。

Please if somebody could help me.


非常感谢。

Thanks best regards.

推荐答案

代码中的哪一行会抛出此异常?
What line in your code throws this exception?


这篇关于UserControl抛出System.ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆