Open a new frame window from MainPage in the Windows 10 Universal App?

How to open a new frame window that contains another xaml page when I click a button from the main window in a universal application?

+4
source share
1 answer

You can check the sample on how to do this from Offical Microsoft Samples on GitHub, as can be found here , but I will summarize quickly here. Another simpler implementation can be found on Mike Taut's Blog .

Since you did not specify a development language, I will assume C # and XAML.

Create your own button in XAML that will be pressed to create a new window:

<Button  
Content="Create"  
HorizontalAlignment="Center"  
VerticalAlignment="Center"  
Click="OnCreate" />

In the code behind, add the OnCreate click handler:

async void OnCreate(object sender, RoutedEventArgs e)  
{  
  CoreApplicationView newCoreView = CoreApplication.CreateNewView();  

  ApplicationView newAppView = null;  
  int mainViewId = ApplicationView.GetApplicationViewIdForWindow(  
    CoreApplication.MainView.CoreWindow);  

  await newCoreView.Dispatcher.RunAsync(  
    CoreDispatcherPriority.Normal,  
    () =>  
    {  
      newAppView = ApplicationView.GetForCurrentView();  
      Window.Current.Content = new SubWindowUserControl();  
      Window.Current.Activate();  
    });  

  await ApplicationViewSwitcher.TryShowAsStandaloneAsync(  
    newAppView.Id,  
    ViewSizePreference.UseHalf,  
    mainViewId,  
    ViewSizePreference.UseHalf);  
}

. ( ), , , SubWindowUserControl, ( ), .

API ApplicationViewSwitcher.

XAML, async, OnCreate, :

  await newCoreView.Dispatcher.RunAsync(  
    CoreDispatcherPriority.Normal,  
    () =>  
    {  
      newAppView = ApplicationView.GetForCurrentView();  
      Window.Current.Content = new Frame();
      (Window.Current.Content as Frame).Navigate(typeof(<your_page>));
      Window.Current.Activate();  
    });  

, XAML , Frame, XAML.

Hello Second Window!

, !

+5

Source: https://habr.com/ru/post/1608301/


All Articles