C# 在安卓C中,如何将变量从(主活动到另一个)活动传递到自定义视图?
我是编程新手,在将变量从活动传递到自定义视图时遇到问题 在我的主要活动中,我使用以下方法将字符串添加到新的活动分析中:C# 在安卓C中,如何将变量从(主活动到另一个)活动传递到自定义视图?,c#,android,xamarin,C#,Android,Xamarin,我是编程新手,在将变量从活动传递到自定义视图时遇到问题 在我的主要活动中,我使用以下方法将字符串添加到新的活动分析中: Intent a = new Intent (this, typeof(Analysis)); a.PutExtra("speedvalues", mapclass.Speed()); StartActivity (a); 在分析活动中,我使用以下方法检索它: string s = Intent.GetStringExtra("speedvalues"); 这一切都很好。但
Intent a = new Intent (this, typeof(Analysis));
a.PutExtra("speedvalues", mapclass.Speed());
StartActivity (a);
在分析活动中,我使用以下方法检索它:
string s = Intent.GetStringExtra("speedvalues");
这一切都很好。但是,我需要将这个字符串传递给另一个类,即我在打开新活动时调用的自定义视图类。因为我需要这个字符串来使用自定义视图类绘制图形/线
有人能告诉我怎么做吗?我尝试将字符串s声明为静态字符串,但这导致了异常错误
编辑:
以下是我所请求的自定义视图类的一部分:
class Graph : View
{
List<int> speedvalues = new List<int>(StringToListInt(VARIABLE FROM ACTIVITY));
List<PointF> graphpoints = new List<PointF>();
int padding = 100;
public Graph(Context c) : base(c)
{
this.SetBackgroundColor(Color.White);
}
public float MaxSpeed()
{ [...] }
public static List<int> StringToListInt(string x)
{ [...] }
public void GraphPoints()
{ [...] }
protected override void OnDraw(Canvas cv)
{ [...] }
当然,在Graph类中,我使用以下方法检索值:
string speedvalues = Analysis.s5;
到目前为止,这一个有效。这个版本更好还是更差?1。改变
4.到
6.到
完整代码
您正在分配值,并且已调用ondraw。 因此,为了防止ondraw,请查找下面修改的代码
class Graph : View
{
List<int> speedvalues;
List<PointF> graphpoints = new List<PointF>();
int padding = 100;
bool isReady = false;
public Graph(Context c) : base(c)
{
this.SetBackgroundColor(Color.White);
}
public float MaxSpeed()
{ [...] }
public static List<int> StringToListInt(string x)
{ [...] }
public void GraphPoints()
{ [...] }
public void ReadyToDraw(bool ready)
{
isReady=ready;
}
public static List<int> StringToListInt(string x)
{
speedvalues=new List<int>(StringToListInt(x));
}
protected override void OnDraw(Canvas cv)
{
base.OnDraw(canvas);
if(ReadyToDraw)
{
//Your drawing code
}
}
}
除了其他答案,您还有另一个选项,称为共享首选项 您可以将变量存储在主活动中,并在任何类中获取它 例如,通过谷歌可以找到很多教程
发布您的定制课程嘿,谢谢您的回复。我添加了自定义视图类的一部分。很高兴知道您找到了解决方案。但是使用static不是一个好的实践。静电是邪恶的,谢谢!这是一个非常好的解决方案。我自己也通过使用新的静态字符串和静态方法找到了解决方案。我在问题部分添加了我的解决方案。谢谢!这在将来非常方便。在这种情况下,共享首选项不可取。谢谢您的回答。但是,我的问题是如何将字符串SpeedValue从Activity类获取到View类。欢迎,您也可以这样做。我更新了答案,向您展示了如何通过第6步的检查
List<int> speedvalues = new List<int>(StringToListInt(VARIABLE FROM ACTIVITY));
List<int> speedvalues ;
public Graph(Context c) : base(c)
{
this.SetBackgroundColor(Color.White);
}
public Graph(Context c , string speedvalues ) : base(c)
{
this.peedvalues = new List<int>(StringToListInt(speedvalues));
this.SetBackgroundColor(Color.White);
}
string s4 = Intent.GetStringExtra("speedvalues");
s5 = StringToG (s4);
graph = new Graph(this);
string s4 = Intent.GetStringExtra("speedvalues");
graph = new Graph(this , StringToG(s4));
class Graph : View
{
List<int> speedvalues ;
public Graph(Context c , string speedvalues ) : base(c)
{
this.peedvalues = new List<int>(StringToListInt(speedvalues));
this.SetBackgroundColor(Color.White);
}
}
class Graph : View
{
List<int> speedvalues;
List<PointF> graphpoints = new List<PointF>();
int padding = 100;
bool isReady = false;
public Graph(Context c) : base(c)
{
this.SetBackgroundColor(Color.White);
}
public float MaxSpeed()
{ [...] }
public static List<int> StringToListInt(string x)
{ [...] }
public void GraphPoints()
{ [...] }
public void ReadyToDraw(bool ready)
{
isReady=ready;
}
public static List<int> StringToListInt(string x)
{
speedvalues=new List<int>(StringToListInt(x));
}
protected override void OnDraw(Canvas cv)
{
base.OnDraw(canvas);
if(ReadyToDraw)
{
//Your drawing code
}
}
}
string s = Intent.GetStringExtra("speedvalues");
Graph g=new Graph(this);
g.StringToListInt(s);//Assign values
g.ReadyToDraw(true);//Set it is ready to draw
g.invalidate(); // Draw