简体   繁体   English

无法从其他页面访问和重用控件

[英]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打开应用程序 > MainList > 单击列表项 > go 到 PageSunflower > PageSunflower 应该显示 PageTest.xaml 文件的控件

Current result:当前结果:

Error - Object reference not set to an instance of an object错误 - Object 引用未设置为 object 的实例

MainList.xaml.cs 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页向日葵.xaml

 <Page [...]>
     <Grid>

     </Grid>
 </Page>

PageSunflower.xaml.cs页向日葵.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 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 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:如果想通过static变量来获得其他页面的控制权,需要两个条件:

  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.另外,当页面不是当前Frame的内容时,页面可能会被卸载,此时会释放引用,无法获取控件,所以应该缓存页面。

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.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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