6

私はWPFが初めてで、WPFのナビゲーションアプリケーションを開発しています.

<NavigationWindow x:Class="KioskTraffic.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="768" Width="1024" Source="Home.xaml"
    WindowState="Maximized" ResizeMode="NoResize" ShowsNavigationUI="False" WindowStyle="None" Cursor="Arrow" Closing="NavigationWindow_Closing"></NavigationWindow>

そして、このnavigarionwindowに表示されるページがいくつかあります

<Page x:Class="KioskTraffic.Page1"
      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="768" Width="1024"
      Title="Page1">

NavigationWindow.xaml.cs ファイルで現在実行されているページを確認するにはどうすればよいですか?

このナビゲーション ウィンドウには、現在のページが home.xaml であるかどうかを確認するタイマーがあり、そのタイマーを開始したくありません。

4

6 に答える 6

8

ナビゲーション ウィンドウの CurrentSource プロパティを使用して、コード ビハインドで現在実行中のページを取得できます。要件に従って、以下のように NavigationService.Navigate() メソッドを使用して実行します。

NavWindow.xaml :

<NavigationWindow x:Class="WPFTest.MyNavWindow" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="768" Width="1024" Source="ShopList.xaml" Grid.Row="1" 
        WindowState="Maximized" ResizeMode="NoResize" ShowsNavigationUI="True" WindowStyle="SingleBorderWindow" Cursor="Arrow" Navigated="NavigationWindow_Navigated">
</NavigationWindow>

NavWindow.xaml.cs :

namespace WPFTest
{
    public partial class MyNavWindow : NavigationWindow
    {
        public MyNavWindow()
        {
            InitializeComponent();
        }

        private void NavigationWindow_Navigated(object sender, NavigationEventArgs e)
        {
            MessageBox.Show(((NavigationWindow)this).CurrentSource.ToString());
        }
    }
}

ShopList.xaml :

<Page x:Class="WPFTest.ShopList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ShopList">
<Grid>
    <Label Height="28" Margin="81,12,99,0" Name="label1" VerticalAlignment="Top" FontSize="16" FontWeight="Bold">Shop List</Label>
    <Button Name="btnNext" Content="Go to Product list" Width="150" Height="30" Margin="0,50,0,0" Click="btnNext_Click"></Button>
</Grid>

ShopList.xaml.cs :

namespace WPFTest
{
    public partial class ShopList : Page
    {
        public ShopList()
        {
            InitializeComponent();
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new System.Uri("ProductList.xaml", UriKind.Relative));
        }
    }
}

ProductList.xaml :

<Page x:Class="WPFTest.ProductList"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ProductList">
    <Grid>
        <Label Height="28" Margin="81,12,99,0" Name="label1" VerticalAlignment="Top" FontSize="16" FontWeight="Bold">Product List</Label>
    </Grid>
</Page>

それは私にとってはうまくいっています。これで問題が解決することを願っています。解決しない場合はお気軽にご質問ください。

アップデート :

Uri の代わりにクラス名を使用してページをナビゲートする場合は、次のような現在のソースを取得できます。

MessageBox.Show(((NavigationWindow)this).NavigationService.Content.GetType().Name.ToString() + ".xaml");
于 2011-10-04T06:23:39.977 に答える
1

フレーム内に表示されるフルパスで現在のページを知りたい場合は、それを使用できます。

string currentpage = Myframe.CurrentSource.OriginalString.ToString().Replace("yoursolutionname;component/", "");
于 2015-02-04T16:56:00.997 に答える
1

コンテナー ウィンドウの NavigationService の Content プロパティを見て、現在のページを見つけました。

于 2013-09-05T16:03:35.390 に答える
0

には、最後にナビゲートされたページの URINavigationWindowと呼ばれるプロパティがあります。CurrentSource

于 2011-09-30T14:17:44.417 に答える