以帧为单位的WPF页面导航,如何从我导航到的页面访问原始帧?

时间:2015-12-13 19:09:24

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

我从MainPage.xaml.cs转到了一个名为Einstellungen的页面。

private void button_Copy_Click(object sender, RoutedEventArgs e)
{
    Einstellungen einstellungen = new Einstellungen();
    _mainFrame.Content = einstellungen;
}

MainPage.xaml有一个名为_mainFrame的框架。 Button Einstellungen将_mainFrame的内容设置为Page Einstellungen

<Page
x:Class="ProjectMoralsKite.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ProjectMoralsKite"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisualStateGroup">
            <VisualState x:Name="_5__Phone">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowHeight="640" MinWindowWidth="360"/>
                </VisualState.StateTriggers>
            </VisualState>
            <VisualState x:Name="_6__Phone">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowHeight="768" MinWindowWidth="432"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="textBlock.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>10,40,0,0</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="textBlock.(FrameworkElement.Width)" Value="412"/>
                    <Setter Target="button.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>10,251,0,0</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="button.(FrameworkElement.Width)" Value="412"/>
                    <Setter Target="button_Copy.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>10,336,0,0</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="button_Copy.(FrameworkElement.Width)" Value="412"/>
                    <Setter Target="button_Copy1.(FrameworkElement.Width)" Value="412"/>
                    <Setter Target="button_Copy1.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>10,421,0,0</Thickness>
                        </Setter.Value>
                    </Setter>
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="_8__Tablet">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowHeight="640" MinWindowWidth="1024"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="button_Copy.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>370,258,362,0</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="button_Copy.(FrameworkElement.HorizontalAlignment)" Value="Stretch"/>
                    <Setter Target="button_Copy.(FrameworkElement.Width)" Value="NaN"/>
                    <Setter Target="textBlock.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>10,40,10,0</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="textBlock.(FrameworkElement.HorizontalAlignment)" Value="Stretch"/>
                    <Setter Target="textBlock.(FrameworkElement.Width)" Value="NaN"/>
                    <Setter Target="button_Copy1.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>0,397,34,0</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="button_Copy1.(FrameworkElement.HorizontalAlignment)" Value="Right"/>
                    <Setter Target="button_Copy1.(FrameworkElement.Width)" Value="292"/>
                    <Setter Target="button.(FrameworkElement.Height)" Value="206"/>
                    <Setter Target="button.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>48,142,0,0</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="button.(FrameworkElement.Width)" Value="292"/>
                    <Setter Target="button_Copy.(FrameworkElement.Height)" Value="206"/>
                    <Setter Target="button_Copy1.(FrameworkElement.Height)" Value="207"/>
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="_12__Tablet">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowHeight="960" MinWindowWidth="1440"/>
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="button_Copy.(FrameworkElement.Height)" Value="NaN"/>
                    <Setter Target="button_Copy.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>563,237,548,64</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="button_Copy.(FrameworkElement.VerticalAlignment)" Value="Stretch"/>
                    <Setter Target="button_Copy.(FrameworkElement.HorizontalAlignment)" Value="Stretch"/>
                    <Setter Target="button_Copy.(FrameworkElement.Width)" Value="NaN"/>
                    <Setter Target="button.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>141,237,0,64</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="button.(FrameworkElement.VerticalAlignment)" Value="Stretch"/>
                    <Setter Target="button.(FrameworkElement.Height)" Value="NaN"/>
                    <Setter Target="button.(FrameworkElement.Width)" Value="329"/>
                    <Setter Target="button_Copy1.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>0,237,123,0</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="button_Copy1.(FrameworkElement.HorizontalAlignment)" Value="Right"/>
                    <Setter Target="button_Copy1.(FrameworkElement.Width)" Value="329"/>
                    <Setter Target="button_Copy1.(FrameworkElement.Height)" Value="659"/>
                    <Setter Target="textBlock.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>10,40,10,0</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="textBlock.(FrameworkElement.HorizontalAlignment)" Value="Stretch"/>
                    <Setter Target="textBlock.(FrameworkElement.Width)" Value="NaN"/>
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Frame x:Name="_mainFrame" Background="#FF0070B7">
        <Grid>
            <Button x:Name="button" Content="&#xD;&#xA;Aufträge&#xD;&#xA;" HorizontalAlignment="Left" Margin="10,251,-275.333,-165" VerticalAlignment="Top" Height="80" Width="340" d:LayoutOverrides="LeftMargin, RightMargin, TopMargin, BottomMargin, TopPosition, BottomPosition"/>
            <TextBlock x:Name="textBlock" TextAlignment="Center" HorizontalAlignment="Left" Margin="10,40,-278.333,-58" TextWrapping="Wrap" Text="ProjectMoralsKite" VerticalAlignment="Top" Height="48" Width="340" FontSize="29.333" d:LayoutOverrides="LeftMargin, RightMargin, TopMargin, BottomMargin, LeftPosition, RightPosition"/>
            <Button x:Name="button_Copy" Content="Einstellungen" HorizontalAlignment="Left" Margin="10,336,-275.333,-250" VerticalAlignment="Top" Height="80" Width="340" Click="button_Copy_Click" d:LayoutOverrides="LeftPosition, RightPosition, TopPosition, BottomPosition"/>
            <Button x:Name="button_Copy1" Content="Über" HorizontalAlignment="Left" Margin="10,421,-275.333,-335" VerticalAlignment="Top" Height="80" Width="340" d:LayoutOverrides="LeftMargin, RightMargin, TopMargin, BottomMargin"/>
        </Grid>
    </Frame>


</Grid>

namespace ProjectMoralsKite
{

// Eine leere Seite, die eigenständig verwendet werden kann oder auf die innerhalb eines Rahmens navigiert werden kann.
// </summary>
public sealed partial class Einstellungen : Page
{

    public Einstellungen()
    {
        this.InitializeComponent();
    }

    public string foregroundProperty { get; set; }

    private void button_Click(object sender, RoutedEventArgs e)
    {


        if(radioButton.IsChecked == true)
        {
            Color black = Color.FromArgb(255, 0, 0, 0);
            SolidColorBrush brush1 = new SolidColorBrush(black);
            textBlock.Foreground = brush1;
            textBlock1.Foreground = brush1;
            radioButton.Foreground = brush1;
            radioButton1.Foreground = brush1;





            //here I want to set the Page of the frame to MainPage.xaml!!!



        }
        else if (radioButton1.IsChecked == true)
        {
            Color white = Color.FromArgb(255, 255, 255, 255);
            SolidColorBrush brush2 = new SolidColorBrush(white);
            textBlock.Foreground = brush2;
            textBlock1.Foreground = brush2;
            radioButton.Foreground = brush2;
            radioButton1.Foreground = brush2;

            //here I want to set the Page of the frame to MainPage.xaml!!!
        }





    }
}

}

现在我在Einstellungen中有一个Button,它应该再次将Frame的内容转换为MainPage。但是在我的Einstellungen.xaml.cs文件中,我当然不能访问我的_mainFrame!我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

您可以Windows.UI.Xaml.Window.Current.Content访问根框架。请注意,它不是您在MainPage中添加到XAML的框架。它的应用程序根框架。但是你可以用它来获取根框架的内容。

var rootFrame = Window.Current.Content as Frame;
var mainPage = rootFrame.Content as MainPage;
var yourFrame = mainPage.Content as Frame; //here I'm not sure because I don't see whole XAML of MainPage

要导航,您应该使用Navigate方法。

_mainFrame.Navigate(typeof(Einstellungen));

最后一件事:WPF != win-universal-app您应该详细说明您的问题

答案 1 :(得分:0)

var rootFrame = Window.Current.Content as Frame;
var mainPage = rootFrame.Content as MainPage;
rootFrame.Navigate(typeof(MainPage));

这种方式有效。 THX