You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

跨页面访问控件报空引用错误,求故障原因及修复方案

问题分析与修复方案

哎,这个空引用异常的问题我太熟了!咱们一步步来拆解:

故障根本原因

你遇到的Object reference not set to an instance of an object异常,核心问题出在**PageTest.Current是null**:

  • PageTest.Current这个静态字段,只有在PageTest页面的构造函数执行时才会被赋值为this;
  • 从你的导航流程(打开应用→进入MainList→跳转PageSunflower)来看,整个过程中PageTest页面根本没被实例化过!自然Current字段还是默认的null,你去访问它的子控件MyTabs、txtPageTitle肯定会报错。

另外还有个隐藏问题:直接跨页面访问控件的设计本身就不合理,会让页面之间的耦合度极高,后续需求变更时维护成本会非常大。


修复方案

方案1:先确保PageTest已实例化(临时应急,不推荐长期用)

如果PageTest是你的应用主页面/入口页面,调整导航流程,确保先加载PageTest,再从PageTest导航到MainList,最后再跳转PageSunflower。这样PageTest.Current就会被正确赋值,不会再为null。

但还是要强调:这种直接操作其他页面控件的方式,后续很容易出问题,不推荐作为长期方案。

方案2:解耦页面,用合理的方式传递数据(推荐)

方式A:通过导航参数传递数据

把需要展示的内容通过导航参数传递,再让PageTest自己处理控件更新,避免直接跨页操作控件:

  1. 修改MainList的跳转代码,传递参数:
// MainList.xaml.cs
private void ListMain_ItemClick(object sender, ItemClickEventArgs e) {
    ListItemMain item = (ListItemMain)e.ClickedItem;
    if (item.FlowerName == "Sunflower") {
        // 传递需要设置的标题文本
        Frame.Navigate(typeof(PageSunflower), "hello");
    } else {
        Frame.Navigate(typeof(PageDaffodil));
    }
}
  1. 在PageSunflower中接收参数,调用PageTest的公开方法更新控件(不要直接操作控件):
// PageSunflower.xaml.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    if (e.Parameter is string title && PageTest.Current != null)
    {
        // 调用PageTest的公开方法更新标题
        PageTest.Current.UpdatePageTitle(title);
    }
}
  1. 在PageTest中添加公开方法,内部处理控件更新:
// PageTest.xaml.cs
public void UpdatePageTitle(string newTitle)
{
    txtTitle.Text = newTitle;
}

方式B:使用MVVM模式(最优解,彻底解耦)

定义一个共享的ViewModel,让PageTest和PageSunflower都绑定到ViewModel的属性上,数据变化时页面自动更新,完全不需要直接操作控件:

  1. 创建共享ViewModel:
public class MainViewModel : INotifyPropertyChanged
{
    private string _pageTitle;
    public string PageTitle
    {
        get => _pageTitle;
        set
        {
            _pageTitle = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
  1. 在App.xaml中注册ViewModel为全局资源(或者用依赖注入确保单例):
<Application.Resources>
    <local:MainViewModel x:Key="MainViewModel"/>
</Application.Resources>
  1. 在PageTest的XAML中绑定属性:
<TextBlock x:Name="txtTitle" Text="{Binding PageTitle, Source={StaticResource MainViewModel}}"/>
  1. 在PageSunflower中直接更新ViewModel的属性:
// PageSunflower.xaml.cs
public PageSunflower() {
    this.InitializeComponent();
    var viewModel = (MainViewModel)Application.Current.Resources["MainViewModel"];
    viewModel.PageTitle = "hello";
}

内容的提问来源于stack exchange,提问作者wbk727

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 08:27:53