如何浏览用户控件通用Windows应用程序中的图像框架?

时间:2015-12-18 07:32:19

标签: c# win-universal-app uwp windows-10-universal

我有一个关于点击它的图像我正在导航到另一个页面

<Image Source="{Binding ImagePath}" Tapped="Image_Tapped_1"/>
  

C#

 private void Image_Tapped_1(object sender, TappedRoutedEventArgs e)
    {
        var gameData = (sender as Image).DataContext as DataClass;
        this.Frame.Navigate(typeof(GamePage), gameData);
    }

现在我已经为自适应UI创建了一个用户控件,我的代码现在看起来像这样

 <Grid>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisualStateGroup">
            <VisualState x:Name="VisualStateMin600">
                <VisualState.Setters>
                    <Setter Target="image.(FrameworkElement.Width)" Value="NaN"/>
                </VisualState.Setters>
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="600"/>
                </VisualState.StateTriggers>
            </VisualState>
            <VisualState x:Name="VisualStateMin0">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="1"/>
                </VisualState.StateTriggers>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Image x:Name="image" Source="{Binding ImagePath}" Tapped="image_Tapped_1" Width="250" />

</Grid>

现在的问题是,我将如何导航到Tap Control属性在UserControl中无法使用的其他页面?

2 个答案:

答案 0 :(得分:2)

试试((Frame)Window.Current.Content).NavigateTo 或者,如果需要,将公共属性设置为App.Xaml.cs上的rootframe,这样您就可以随时访问它。

答案 1 :(得分:1)

在用户控件的代码隐藏中(让我们称之为ImageControl),你必须像这样管理一个事件处理程序:

public sealed partial class ImageControl : UserControl
{
    public event EventHandler ImageTapped;

    public void image_Tapped_1(object sender, TappedRoutedEventArgs e)
    {
        if (ImageTapped != null)
        {
            ImageTapped(this, EventArgs.Empty);
        }
    }
}

从XAML声明你的ImageControl:

<local:ImageControl ImageTapped="ImageControl_ImageTapped" />

最后来自C#代码:

private void ImageControl_ImageTapped(object sender, EventArgs e)
{
   this.Frame.Navigate(typeof(YourOtherPage));
}