2017-03-28 44 views
0

我的Page1.xaml热几秒钟后,得到UWP从另一个页面内容

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <StackPanel HorizontalAlignment="Left" Height="720" VerticalAlignment="Top" Width="575"> 

     <TextBlock Foreground="White" TextWrapping="Wrap" Margin="28,20,31,0" FontSize="14" Height="145"> 
      <TextBlock.Transitions> 
       <TransitionCollection> 
        <EntranceThemeTransition FromHorizontalOffset="400"/> 
       </TransitionCollection> 
      </TextBlock.Transitions> 
      <Run Text="Text 1"/> 
     </TextBlock> 
    </StackPanel> 
</Grid> 

而且Page2.xaml

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <TextBlock Foreground="White SelectionChanged="TextBlock_SelectionChanged" 
Name="TextBlockOne"> 
     <TextBlock.Transitions> 
      <TransitionCollection> 
       <EntranceThemeTransition FromHorizontalOffset="400"/> 
      </TransitionCollection> 
     </TextBlock.Transitions> 
     <Run Text="Text 2"/> 
    </TextBlock> 
</Grid> 

我想要做的是用第2页的“文本2”在5秒后替换第1页中的“文本1”。

我在Page2.xaml.cs中试过这个:

private void TextBlock_SelectionChanged(object sender, RoutedEventArgs e) 
    { 
     var test = TextBlockOne.Text; 
     Frame.Navigate(typeof(Page1), test); 
    } 

我该如何解决这个问题?

+0

代码什么问题呢? – Archana

+1

保持一个计时器5秒钟,然后导航到页面1的文本值 – Archana

+0

现在的问题是没有任何反应。我甚至无法从第2页获取文本值 – LittleBird

回答

0
public MainPage() 
{ 
    DispatcherTimer t = new DispatcherTimer(); 
    t.Interval = TimeSpan.FromSeconds(5); 
    t.Tick += (s, e) => 
    { 
     frame.Navigate(typeof(Page2)); 
     StopTimer(); 
    }; 
    t.Start(); 
} 

public void StopTimer() 
{ 
    t.Stop(); 
} 

Page2.xaml

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    TextBlock.Text = "My string"; 
} 
0

您可以使用MainPage来浏览它。

首先,MainPage有一个可导航到Apage的框架。

然后,MainPage启动一个可以等待5秒的定时器,以调用MainPage来导航到Bpage。

在XAML代码,我写的MainPage

<Frame x:Name="frame"/> 

在xaml.cs

public MainPage() 
    { 
     this.InitializeComponent(); 
     frame.Navigate(typeof(APage)); 
     DispatcherTimer t = new DispatcherTimer(); 
     t.Interval = new TimeSpan(1000); 
     t.Tick += (s, e) => 
     { 
      NavigatePageB(); 
     }; 
     t.Start(); 
    } 

    private async void NavigatePageB() 
    { 
     await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, 
     () => 
     { 
      frame.Navigate(typeof(PageB)); 
     }); 
    } 
+0

我试过这个解决方案,它只导航到APage,并不会继续PageB – LittleBird

+0

@LittleBird它将在1秒后导航到PageB。 – lindexi

+0

@LittleBird我的疏忽写在NavigatePageB中的帧。请尝试我改变的代码。 – lindexi

相关问题