在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,跳轉結束。