Hai. I have a XAML page and a c # control code. There is a block in this page, its contents may change depending on the events, that is, it is necessary to insert XAML code into this block (it can be written both in a separate file (the best option) and in string), which must also be hung managed C # code. Tell me how to implement it. Thank.

eg

namespace App5{ public sealed partial class Content : Page { public Content() { this.InitializeComponent(); } private void Click(object sender, RoutedEventArgs e) { //do } }} 

Xaml:

 <Page x:Class="App5.Content" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App5" 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}"> <Button Content="BUTTON" Click="Click"/> </Grid> </Page> 

I need to insert this page into the block. StackPanel Name = "DataPage"

  <Page x:Class="App5.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App5" 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}"> <StackPanel Name="DataPage"/> </Grid> </Page> 

For any event that occurred on MainPage, everything that contains the content page must be inserted into the <StackPanel Name="DataPage"/> block. In this example, a GRID with a button is inserted. It is necessary not only to insert the content, but also to save the click event hung on the button.

If someone is familiar, it is like dynamic loading of pages on ajax (web), when there are static elements on the page (menu, header, footer), but there is a part that is constantly updated (usually this is the page content itself) when clicking on a link or other events.

  • You can dynamically generate objects from XAML code using the XamlReader . Then it is enough just to set the DataContext and banding for the resulting object and insert it into the tree in the code. - icebat
  • 3
    If not difficult, describe the original task. because what you want to do .... in general you shouldn't want it. And if you have been given a task like this, then do not mess with them) - iRumba
  • Most likely, you just need a UserControl . But yes, better describe your particular case. Now your question is too general. - VladD
  • Completed the question - Sanych Goilo
  • Incomprehensible again. You have a home page, and a Content page. This second page is shown separately, right? And there is a button on it, by pressing which the contents of this second page should go to the main page? - VladD

1 answer 1

He also encountered a similar situation and decided it in the simplest way. I put a static bool member in a separate class and in the MainPage button that changes the value of this member. When true opens one page, and when false another (or you can configure in the code of the page on c # what will be displayed for this or that value). and in MainPage, the content on the button is also depending on the value of true / false.