question

ApptacularApps-8305 avatar image
0 Votes"
ApptacularApps-8305 asked FayWang-MSFT commented

How to safely resuse XAML layout from other classes

I need to reuse some controls in my main page from other pages, but I need someone to confirm if the following is possible or not:

  • Is it possible to access items in Page 1 from Page 2?

  • Is it safe to delete the Page 2 XAML file?

Page1.xaml

 <Page [...]>
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Height="Auto" />
             <RowDefinition Height="*" />
         </Grid.RowDefinitions>
         <StackPanel Grid.Row="0">
             <TextBlock x:Uid="TitleText" />
         </StackPanel>
         <controls:TabView  Grid.Row="1" x:Name="MyTabs">
    
         </controls:TabView>
     </Grid>
 </Page>

Page1.cs

     public sealed partial class Page1 : Page
     {
         public Page1()
         {
             this.InitializeComponent();
         }
     }


Page2.xaml

     <Page [...]>
         <Grid>
        
         </Grid>
     </Page>


Page2.cs

     public sealed partial class Page2 : Page
     {
         public PageStation()
         {
             this.InitializeComponent();
         }
     }


Update


PageTest.xaml

     <Page [...]>
         <Grid>
             <Grid.RowDefinitions>
                 <RowDefinition Height="Auto"/>
                 <RowDefinition Height="*"/>
             </Grid.RowDefinitions>
             <StackPanel Grid.Row="0">
                 <TextBlock x:Name="txtTitle" x:FieldModifier="public" Style="{StaticResource HeaderTextBlockStyle}" />
                
             <controls:TabView x:FieldModifier="public" Grid.Row="1" x:Name="MyTabs"/>
         </Grid>
     </Page>

PageTest.xaml.cs

     public sealed partial class PageTest : Page
     {
         public PageTest()
         {
             this.InitializeComponent();
    
             Current = this;
         }
    
         public static PageTest Current;
     }


PageSunflower.xaml

 <Page [...]>
    
     <Grid>
    
     </Grid>
 </Page>

PageSunflower.xaml.cs

     public sealed partial class PageSunflower : Page
     {
         public PageSunflower()
         {
             this.InitializeComponent();
    
             TabView tabview = PageTest.Current.MyTabs;
    
             TextBlock txtTitle = PageTest.Current.txtPageTitle;
             txtTitle.Text = "hello";
         }
     }

MainList.xaml.cs

     public sealed partial class MainList : Page
     {
         public List<ListItem> listItemMains;
    
         public MainList ()
         {
             this.InitializeComponent();
    
             listItemMains = ItemManagerMains.GetListItems();
         }
    
         private void ListMain_ItemClick(object sender, ItemClickEventArgs e)
         {
             ListItemMain item = (ListItemMain)e.ClickedItem;
    
             if (item.FlowerName == "Sunflower")
             {
                 Frame.Navigate(typeof(PageSunflower));
    
             }
             else
             {
                 Frame.Navigate(typeof(PageDaffodil));
             }
         }
     }


windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

FayWang-MSFT avatar image
0 Votes"
FayWang-MSFT answered FayWang-MSFT commented

Hello,

Welcome to Microsoft Q&A!



Is it possible to access items in Page 1 from Page 2?

If you want to access controls from other pages, you could try to set the x:FieldModifier of the control as public. In this case, it will be public and you can access the control by its x:name from other pages. For example:

Page1.xaml:

 <controls:TabView x:FieldModifier="public"  Grid.Row="1" x:Name="MyTabs">
    
 </controls:TabView>

Page1.xaml.cs:

You need to define a public static Page1 instance to let other pages access your control through this instance.


 public Page1()
 {
     this.InitializeComponent();
    
     Current = this;
 }
    
 public static Page1 Current;


Page2.xaml.cs:

 TabView tabview = Page1.Current.MyTabs;


Is it safe to delete the Page 2 XAML file?

A .xaml file has a .xaml.cs code-behind file which contains the logic. Together, the XAML and code-behind make a complete class. The .xaml.cs associates itself with the corresponding XAML by calling the generated InitializeComponent method in its constructor. If you delete the XAML file, it can't find the XAML and will throw exceptions.







· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

After applying your suggested code to Page2.xaml.cs I got this error:


Object reference not set to an instance of an object

0 Votes 0 ·

When I tested the above code, I didn't receive such exception, can you provide a simple sample that can be reproduced for us to test?

0 Votes 0 ·

That's strange. Could you also confirm if anything needs to be done to Page2.xaml?

0 Votes 0 ·
Show more comments