Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 代表:“;系统字符串“;无法转换为类型";System.Windows.DependencyProperty“;!为什么不呢?_C#_Delegates_Dependencies - Fatal编程技术网

C# 代表:“;系统字符串“;无法转换为类型";System.Windows.DependencyProperty“;!为什么不呢?

C# 代表:“;系统字符串“;无法转换为类型";System.Windows.DependencyProperty“;!为什么不呢?,c#,delegates,dependencies,C#,Delegates,Dependencies,我第一次尝试使用代理。因此,我使用了一个progressbar示例,它在循环中更新(works)。 之后,我尝试使用相同的代码,但将progressbar与应该显示计数器的文本框交换。不幸的是,我得到了以下错误: “System.String”类型的对象无法转换为“System.Windows.DependencyProperty”类型 有人知道问题发生的原因以及我如何解决它吗?提前谢谢 public MainWindow() { InitializeCompon

我第一次尝试使用代理。因此,我使用了一个progressbar示例,它在循环中更新(works)。 之后,我尝试使用相同的代码,但将progressbar与应该显示计数器的文本框交换。不幸的是,我得到了以下错误:

“System.String”类型的对象无法转换为“System.Windows.DependencyProperty”类型

有人知道问题发生的原因以及我如何解决它吗?提前谢谢

    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Process();
    }

    //Create a Delegate that matches the Signature of the ProgressBar's SetValue method
    private delegate void UpdateSylvacDelegate(System.Windows.DependencyProperty dp, Object value);



    private void Process()
    {
        //Create a new instance of our ProgressBar Delegate that points
        //  to the ProgressBar's SetValue method.
        var updateTextBoxDelegate = new UpdateSylvacDelegate(myTextBox.SetValue);

        double value = 0;

        //Loop
        do
        {
            string value += Convert.ToString(value);

            /*Update the Value of the ProgressBar:
              1)  Pass the "UpdateSylvacDelegate" delegate that points to the myTextBox.SetValue method
              2)  Set the DispatcherPriority to "Background"
              3)  Pass an Object() Array containing the property to update (ProgressBar.ValueProperty) and the new value */

            Dispatcher.Invoke(updateTextBoxDelegate,
                System.Windows.Threading.DispatcherPriority.Background,
                new object[] { TextBox.TextProperty, value });                                
        }
        while (!button2.IsPressed);        
    }



    private void button2_Click(object sender, RoutedEventArgs e)
    {

    }

带有2个按钮和1个文本框的我的XML代码:
由于WPF的态度是传递错误,您能否显示
UpdateSylvacDelegate
的代码。错误发生在
updateTextBoxDelegate
调用中的某个地方

另外,请粘贴包含自定义依赖属性的代码,这些属性可能会受到委托以及文本框所在的xaml的影响


然后,我将相应地编辑此答案,希望能找到解决方案。

我终于找到了问题。虽然看起来很容易,但还是花了一段时间才看到:)


“value”(double)和文本框属性string之间存在一个简单的转换问题。

这是完整的代码。UpdateSylvacDelegate目前只是一个名称。如果按下开始按钮,文本框应向上计数,直到按下停止按钮。
<Window x:Class="test2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Prime Numbers" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="210" Width="472">
    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Name="stackPanel1" Width="363" Height="35">
        <Button Content="START" Height="21" Name="button1" Width="69" Click="button1_Click" />
        <TextBox Height="20" Name="myTextBox" Width="114" />
        <Button Content="STOP" Height="22" Name="button2" Width="63" Click="button2_Click" />
        <ProgressBar Height="23" Name="progressBar1" Width="111" />
    </StackPanel>
</Window>