加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-phone-7 – 在其保留页面的代码后面处理用户控制事件

发布时间:2020-12-13 20:44:48 所属栏目:Windows 来源:网络整理
导读:我正在寻找以下情况的解决方案. 在我的应用程序中,我有一个页面说page1,我在page1中放置了一个用户控件.我的要求是我需要在page1的代码后面的用户控件中使用按钮的click事件.我如何在windows phone / silverlight中实现相同目标. 1.第一种和正确的方式: (如
我正在寻找以下情况的解决方案.

在我的应用程序中,我有一个页面说page1,我在page1中放置了一个用户控件.我的要求是我需要在page1的代码后面的用户控件中使用按钮的click事件.我如何在windows phone / silverlight中实现相同目标.

1.第一种和正确的方式:

(如果您了解MVVM模式)将控制MyControl,以公开类型为ICommand的DependencyProperty,例如,MyControlButtonClickCommand.

XAML:

<UserControl>
    <Button Command={Binding MyControlButtonClickCommand,Source={RelativeSource Self}} />
</UserControl>

代码隐藏:

public ICommand MyControlButtonClickCommand
{
    get { return (ICommand)GetValue(MyControlButtonClickCommandProperty); }
    set { SetValue(MyControlButtonClickCommandProperty,value); }
}

public static readonly DependencyProperty MyControlButtonClickCommandProperty =
        DependencyProperty.Register("MyControlButtonClickCommand",typeof(ICommand),typeof(MyControl),new PropertyMetadata(null));

您将使用UserControl,如下所示:

<phone:PhoneApplicationPage>

    <namespace:MyControl MyControlButtonClickCommand="{Binding ControlButtonCommand}" />

</phone:PhoneApplicationPage>

ControlButtonCommand是ViewModel(您的自定义对象)的属性,位于Page的DataContext中.

还有一种更简单,更脏的方式,我不鼓励你去:

就像你公开MyControlButtonClickCommand依赖属性而不是暴露它一样,你可以公开一个事件MyControlButtonClick并在页面的xaml中订阅它.在UserControl的代码内部,您应该订阅它的按钮的Click事件并激活它自己的MyControlButtonClick事件.

希望这会帮助你.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读