在后面的代码中访问XAML对象(WPF)

时间:2010-09-13 18:51:21

标签: wpf

我正在像这样在XAML中创建对象

<Window.Resources>
        <local:DataReceiver x:Key="request">
        </local:DataReceiver>
    </Window.Resources>

如何从代码隐藏中调用此对象方法?

2 个答案:

答案 0 :(得分:1)

DataReceiver request = this.TryFindResource("request") as DataReceiver;
if (request != null)
{
   // your code here
}

答案 1 :(得分:1)

var dataReceiver = (DataReceiver)FindResource("request");
...