How to safely resuse XAML layout from other classes

Apptacular Apps 386 Reputation points
2020-06-15T16:26:05.653+00:00

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));
            }
        }
    }
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Fay Wang - MSFT 5,196 Reputation points
    2020-06-16T02:52:53.91+00:00

    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.