简体   繁体   中英

Unable to access and reuse controls from other page

I'm trying to use a page to access some controls in a different page but for some reason, it's not working despite setting the necessary references to the other page? Any ideas on why this is not working and how this can be fixed?

Expected result:

Open app > MainList > click list item > go to PageSunflower > PageSunflower should show controls of the PageTest.xaml file

Current result:

Error - Object reference not set to an instance of an object

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));
         }
     }
 }

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";
     }
 }

PageTest.xaml

 <Page [...]>
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Height="Auto"/>
             <RowDefinition Height="*"/>
         </Grid.RowDefinitions>
         <StackPanel Grid.Row="0">
             <TextBlock x:Name="txtTitle" x:FieldModifier="public"/>

         <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;
 }

If you want to get the control of other page through static variables, you need two conditions:

  1. This page has already been loaded.
  2. The page is cached.

Since the controls on the page are called, the controls on the page will be instantiated only after the page is loaded.

In addition, when the page is not the content of the current Frame , the page may be unloaded, and the reference will be released at this time, the control cannot be obtained, so the page should be cached.

So if you want to get the controls in a page, please make sure that the page has been loaded (that is, once navigated to the page), and then cache the page, like this:

public PageTest()
{
    this.InitializeComponent();
    NavigationCacheMode = NavigationCacheMode.Enabled;
    Current = this;
}

Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM