c# - 框架中的 WPF 页面导航,如何从我导航到的页面访问我的原始框架?

标签 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 的 Content 设置为 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 的 Content 添加到 MainPage。但是在我的 Einstellungen.xaml.cs 文件中,我当然无法访问我的 _mainFrame!我该怎么做?

最佳答案

您可以通过 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 你应该详细说明你的问题

关于c# - 框架中的 WPF 页面导航,如何从我导航到的页面访问我的原始框架?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34255109/

相关文章:

java - 无法验证 ANT 1.9.7 设置

Docker 未在 Windows 10 上运行错误 : Hardware assisted virtualization and data execution protection must be enabled in the BIOS

c# - x :Bind StackOverflow when trying to x:Bind ListView. 使用 TwoWay 模式的 SelectedItem

c# - 深色/浅色主题 Assets 限定符

c# - 检查 Get-NetTCPConnection 对象是否存在

visual-studio-2015 - 发行版中的 UWP 编译失败

c# - 在Application_Start上访问当前域名

c# - 将值数组传递给 .net 紧凑框架中的 SQL 存储过程

c# - C# 中 ToString() 的需要是什么?

c# - PDF 将零件编号转换为链接