windows phone 7中简单使用Usercontrol

在UserControl页面:

xaml中加入了一个Button,有一个click事件Button_Click

 <Grid x:Name="LayoutRoot">
<Button Click="Button_Click" Style="{StaticResource ButtonStyle1}">
<Button.Background>
<ImageBrush ImageSource="ApplicationIcon.png"></ImageBrush>
</Button.Background>
</Button>
</Grid>

cs代码:

public event EventHandler<RoutedEventArgs> goClick;
private void Button_Click(object sender, RoutedEventArgs e)
{
var h = goClick;
if (h != null)
{
h(this, e);
}
}

在调用UserControl的Page页中的xaml:

引入命名空间:

xmlns:my="clr-namespace:YourUserControlSolutionNameHere"

添加UserControl:

<my:YourUserControlClassName Name="userControl" Width="100" Height="100"></my:YourUserControlClassName >

cs代码定义并处理点击事件:

userControl.goClick += new EventHandler<RoutedEventArgs>(userControl_goClick);
void userControl_goClick(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/PivotPage1.xaml", UriKind.Relative));
}

OK,跳转结束。


你可能感兴趣的:(windows,phone)