如何点击页面的一个按钮来改变Windows框架的来源?[英] How to click a button of the page to change the source of the frame of Windows?

本文是小编为大家收集整理的关于如何点击页面的一个按钮来改变Windows框架的来源?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个WPF项目,我在窗口中添加了一个框架,框架的来源是页面.我想在页面中单击一个按钮以更改框架的页面.

<Window x:Class="MagicArm.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MagicArm"
    Title="MainWindow">
   <Frame Name="FrameContent"Source="PageStart.xaml"></Frame>
</Window>

pagestart:

<Page x:Class="MagicArm.PageStart"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:Ignorable="d" 
  Height="452" Width="800"
  Title="PageStart">
<Canvas>
 <button name=""> </button>
</Canvas>

推荐答案

编辑:功能解决方案看起来像:

mainwindow xaml:

<StackPanel>
    <Frame Name="frmMainContent" Height="260"
         DataContext="MyPageInformation"
         Source="{Binding}"
         NavigationUIVisibility="Hidden">           
    </Frame>

</StackPanel>

MainWindow CS:

 frmMainContent.Source = new Uri("test1.xaml", UriKind.Relative); // initialize frame with the "test1" view

test1 xaml:

<Grid>
    <Button Click="ButtonBase_OnClick" Background="Red" Height="30" Width="100">Go to page 2</Button>
</Grid>

test1 cs:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        NavigationService ns = NavigationService.GetNavigationService(this);
        ns.Navigate(new Uri("test2.xaml", UriKind.Relative));
    }

test2 xaml:

  <Grid>
        <Button Click="ButtonBase_OnClick" Width="100" Height="30" Background="RoyalBlue"> Go to page 1</Button>
    </Grid>

test2 cs:

 NavigationService ns = NavigationService.GetNavigationService(this);
            ns.Navigate(new Uri("test1.xaml", UriKind.Relative));

这是使用navigationservice的工作解决方案.

本文地址:https://www.itbaoku.cn/post/2091132.html

问题描述

I have a WPF project, I add a frame in the Windows,the source of the frame is the page. I want to achieve clicking a button in the page to change the page of the frame.

<Window x:Class="MagicArm.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MagicArm"
    Title="MainWindow">
   <Frame Name="FrameContent"Source="PageStart.xaml"></Frame>
</Window>

PageStart:

<Page x:Class="MagicArm.PageStart"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  mc:Ignorable="d" 
  Height="452" Width="800"
  Title="PageStart">
<Canvas>
 <button name=""> </button>
</Canvas>

推荐答案

EDIT: A functional solution can look like:

MainWindow XAML:

<StackPanel>
    <Frame Name="frmMainContent" Height="260"
         DataContext="MyPageInformation"
         Source="{Binding}"
         NavigationUIVisibility="Hidden">           
    </Frame>

</StackPanel>

MainWindow cs:

 frmMainContent.Source = new Uri("test1.xaml", UriKind.Relative); // initialize frame with the "test1" view

test1 XAML:

<Grid>
    <Button Click="ButtonBase_OnClick" Background="Red" Height="30" Width="100">Go to page 2</Button>
</Grid>

test1 cs:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        NavigationService ns = NavigationService.GetNavigationService(this);
        ns.Navigate(new Uri("test2.xaml", UriKind.Relative));
    }

test2 XAML:

  <Grid>
        <Button Click="ButtonBase_OnClick" Width="100" Height="30" Background="RoyalBlue"> Go to page 1</Button>
    </Grid>

test2 cs:

 NavigationService ns = NavigationService.GetNavigationService(this);
            ns.Navigate(new Uri("test1.xaml", UriKind.Relative));

This is a working solution using NavigationService.