Xamarin C# - 从不同的布局设置 TextView
Xamarin C# - Set a TextView from a different Layout
我有问题。
这是我使用的简化代码:
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 永远不会改变!
我做错了什么?
您必须缓存对膨胀的 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
实例上的文本,这是LoadAgentInfo
结束后立即丢弃。
我有问题。
这是我使用的简化代码:
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 永远不会改变!
我做错了什么?
您必须缓存对膨胀的 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
实例上的文本,这是LoadAgentInfo
结束后立即丢弃。