【问题标题】:Xamarin C# - Set a TextView from a different LayoutXamarin C# - 从不同的布局设置 TextView
【发布时间】:2019-01-04 10:25:25
【问题描述】:

我有问题。

这是我使用的简化代码:

public void LoadOrderPage()
{
    Android.Support.V4.View.ViewPager SummaryWalletSwitcher =
          FindViewById<Android.Support.V4.View.ViewPager>(Resource.Id.SummaryWalletSwitcher);

    List<View> viewlist = new List<View>();
    viewlist.Add(LayoutInflater.Inflate(Resource.Layout.AgentSummary, null, false));
    viewlist.Add(LayoutInflater.Inflate(Resource.Layout.AgentWallet, null, false));
    SummaryWalletAdapter ViewSwitchAdapter = new SummaryWalletAdapter(viewlist);
    SummaryWalletSwitcher.Adapter = ViewSwitchAdapter;

    LoadAgentInfo(null, null);

    Timer AgentInfo_Timer = new Timer();
    AgentInfo_Timer.Interval = 1000;
    AgentInfo_Timer.Elapsed += LoadAgentInfo;
    AgentInfo_Timer.Enabled = true;
}

public void LoadAgentInfo(object sender, ElapsedEventArgs e)
{
    TextView TextView1 = FindViewById<TextView>(Resource.Id.txtPortfolioValue);      
    TextView1.Text = "This is TextView 1";
}

TextView 在Resource.Layout.AgentSummary 内。 计时器每秒运行良好!

但是当我调用LoadAgentInfo(null, null); 时,它说该函数内的 TextView 是一个空引用。原因是我使用 ViewPager 在一个页面中使用了 2 个布局。

我已经尝试过像这样从 id 的来源扩展布局:

var InflatedAgentSummary = LayoutInflater.Inflate(Resource.Layout.AgentSummary, null);
TextView TextView1 = 
     InflatedAgentSummary.FindViewById<TextView>(Resource.Id.txtPortfolioValue);

但是 TextView 永远不会改变!

我做错了什么?

【问题讨论】:

  • 确保 ID 为 txtPortfolioValue 的 textview 是该活动的布局集的一部分。该活动的代码在 OnCreate 方法上的外观如何?
  • 好吧,TextView 所在的布局在 Android.Support.V4.View.ViewPager 内部
  • 我会将代码编辑为我拥有的更相似的版本
  • 我已经编辑了问题和代码
  • 如果 TextView 在另一个视图中,在本例中为 ViewPager,那么您将无法从活动中找到它。这就是您拥有空引用的原因。 FindViewById 仅在您使用 SetContentLayout 方法设置的布局文件中查找视图的 Id。在这里,您可能需要找到一种从该视图公开方法的方法。

标签: c# android xamarin methods elapsedtime


【解决方案1】:

您必须缓存对膨胀的AgentSummary 视图的引用并使用它来访问您的TextView

private View _agentSummary;

public void LoadOrderPage()
{
    Android.Support.V4.View.ViewPager SummaryWalletSwitcher = FindViewById<Android.Support.V4.View.ViewPager>(Resource.Id.SummaryWalletSwitcher);

    List<View> viewlist = new List<View>();
    _agentSummary = LayoutInflater.Inflate(Resource.Layout.AgentSummary, null, false);
    viewlist.Add(_agentSummary);
    viewlist.Add(LayoutInflater.Inflate(Resource.Layout.AgentWallet, null, false));
    SummaryWalletAdapter ViewSwitchAdapter = new SummaryWalletAdapter(viewlist);
    SummaryWalletSwitcher.Adapter = ViewSwitchAdapter;

    LoadAgentInfo(null, null);

    Timer AgentInfo_Timer = new Timer();
    AgentInfo_Timer.Interval = 1000;
    AgentInfo_Timer.Elapsed += LoadAgentInfo;
    AgentInfo_Timer.Enabled = true;
}

public void LoadAgentInfo(object sender, ElapsedEventArgs e)
{
    TextView TextView1 = _agentSummary.FindViewById<TextView>(Resource.Id.txtPortfolioValue);      
    TextView1.Text = "This is TextView 1";
}

文本没有在你的尝试中改变的原因是你夸大了AgentSummary的一个新实例,所以你实际上改变了这个新InflatedAgentSummary实例上的文本,它在@987654326之后立即被丢弃@结束。

【讨论】:

  • 非常感谢!这解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
相关资源
最近更新 更多