Xamarin C#-从其他布局设置文本视图
我有个问题 这是我使用的简化代码:Xamarin C#-从其他布局设置文本视图,c#,android,xamarin,methods,elapsedtime,C#,Android,Xamarin,Methods,Elapsedtime,我有个问题 这是我使用的简化代码: public void LoadOrderPage() { Android.Support.V4.View.ViewPager SummaryWalletSwitcher = FindViewById<Android.Support.V4.View.ViewPager>(Resource.Id.SummaryWalletSwitcher); List<View> viewlist = new Lis
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";
}
但是文本视图永远不会改变
我做错了什么?您必须缓存对已膨胀的
代理摘要
视图的引用,并使用它访问您的文本视图
:
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";
}
private View\u-agentSummary;
公共void LoadOrderPage()
{
Android.Support.V4.View.ViewPager SummaryWalletSwitcher=FindViewById(Resource.Id.SummaryWalletSwitcher);
列表视图列表=新列表();
_agentSummary=LayoutFlater.Inflate(Resource.Layout.agentSummary,null,false);
添加(_agentSummary);
添加(LayoutInflater.Inflate(Resource.Layout.AgentWallet,null,false));
SummaryWalletAdapter ViewSwitchAdapter=新的SummaryWalletAdapter(视图列表);
SummaryWalletSwitcher.Adapter=ViewSwitchAdapter;
LoadAgentInfo(null,null);
Timer AgentInfo_Timer=新定时器();
AgentInfo_Timer.Interval=1000;
AgentInfo_Timer.appeased+=LoadAgentInfo;
AgentInfo_Timer.Enabled=true;
}
public void LoadAgentInfo(对象发送方,ElapsedEventArgs e)
{
TextView TextView1=_agentSummary.FindViewById(Resource.Id.txtPortfolioValue);
TextView1.Text=“这是TextView 1”;
}
您尝试此操作时文本没有更改的原因是您夸大了一个新的
AgentSummary
实例,因此您实际上更改了这个新的InflatedAgentSummary
实例上的文本,在LoadAgentInfo
结束后,它立即被丢弃。您必须缓存对已膨胀的AgentCommand
视图的引用,并使用它访问您的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";
}
private View\u-agentSummary;
公共void LoadOrderPage()
{
Android.Support.V4.View.ViewPager SummaryWalletSwitcher=FindViewById(Resource.Id.SummaryWalletSwitcher);
列表视图列表=新列表();
_agentSummary=LayoutFlater.Inflate(Resource.Layout.agentSummary,null,false);
添加(_agentSummary);
添加(LayoutInflater.Inflate(Resource.Layout.AgentWallet,null,false));
SummaryWalletAdapter ViewSwitchAdapter=新的SummaryWalletAdapter(视图列表);
SummaryWalletSwitcher.Adapter=ViewSwitchAdapter;
LoadAgentInfo(null,null);
Timer AgentInfo_Timer=新定时器();
AgentInfo_Timer.Interval=1000;
AgentInfo_Timer.appeased+=LoadAgentInfo;
AgentInfo_Timer.Enabled=true;
}
public void LoadAgentInfo(对象发送方,ElapsedEventArgs e)
{
TextView TextView1=_agentSummary.FindViewById(Resource.Id.txtPortfolioValue);
TextView1.Text=“这是TextView 1”;
}
您尝试此操作时文本没有更改的原因是您夸大了一个新的
AgentSummary
实例,因此您实际上更改了这个新的InflatedAgentSummary
实例上的文本,在LoadAgentInfo
结束后立即将其丢弃。请确保Id为txtPortfolioValue
的文本视图是该活动布局集的一部分。在OnCreate
方法中,该活动的代码看起来如何?文本视图的布局位于Android.Support.V4.View.ViewPagerI中。我会将代码编辑为更相似的版本我已经编辑了问题和代码如果文本视图位于另一个视图中,在本例中为ViewPager
,那么您将无法从活动中找到它。这就是您使用空引用的原因FindViewById
仅在使用SetContentLayout
方法设置的布局文件中查找视图ID。在这里,您可能需要找到从该视图公开方法的方法。请确保Id为txtPortfolioValue
的textview是该活动布局集的一部分。在OnCreate
方法中,该活动的代码看起来如何?文本视图的布局位于Android.Support.V4.View.ViewPagerI中。我会将代码编辑为更相似的版本我已经编辑了问题和代码如果文本视图位于另一个视图中,在本例中为ViewPager
,那么您将无法从活动中找到它。这就是您使用空引用的原因FindViewById
仅在使用SetContentLayout
方法设置的布局文件中查找视图ID。在这里,您可能需要找到一种从该视图公开方法的方法。非常感谢!这解决了我的问题。非常感谢!这解决了我的问题。