页面引用资源控件如何赋值

fxhome fxhome 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
比如MainPage.xaml页引用了 有一个资源

<UserControl.Resources>
<local:UserControl1 x:Key="UserControl1DataSource" d:IsDataSource="True"/>
</UserControl.Resources>

//UserControl1 是一个用户控件

MainPage.xaml 还有一个内容控件

<ContentControl x:Name="contentControl" ></ContentControl>




然后我在MainPage.xaml.cs 的构造函数里写

UserControl1 obj = this.Resources["UserControl1DataSource"] as UserControl1;
contentControl.Content = obj;



为什么会显示 值不在预期的范围内?
注示:我就是不想写 UserControl1 obj=new UserControl1() 我想知道怎么把资源控件赋值 谢谢大家

...全文
评论 1
收藏 已收藏
分享
返回顶部
fxhome
WPF/Silverlight 8,757 成员 12,031 内容