C# 按名称设置并查找控件
我所拥有的是这个,它运行良好:C# 按名称设置并查找控件,c#,wpf,visual-studio,C#,Wpf,Visual Studio,我所拥有的是这个,它运行良好: if (direction.Equals("UR")) { UR_Image.Source = new BitmapImage(new Uri(String.Format("file:///{0}/../Family/" + name + "/Image/"+direction+".png", Directory.GetCurrentDirectory()))); } else if (direction.Equals("UL")) { UL_Im
if (direction.Equals("UR"))
{
UR_Image.Source = new BitmapImage(new Uri(String.Format("file:///{0}/../Family/" + name + "/Image/"+direction+".png", Directory.GetCurrentDirectory())));
}
else if (direction.Equals("UL"))
{
UL_Image.Source = new BitmapImage(new Uri(String.Format("file:///{0}/../Family/" + name + "/Image/"+direction+".png", Directory.GetCurrentDirectory())));
}
我想做的事情如下,以伪代码的形式编写:
direction + _Image.Source = new BitmapImage(new Uri(
String.Format("file:///{0}/../Family/" + name + "/Image/"+direction+".png",
Directory.GetCurrentDirectory())));
如何实现方向+\u图像
方向是一个
字符串,UL\u Image和UR\u Image是图像视图。尝试以下方法
YourWindow.xaml
<!-- named controls -->
<Image x:Name="ImageOne" />
<Image x:Name="ImageTheOther" />
其中方向
为枚举
public enum Direction
{
One,
TheOther
}
简单地用整数来识别你的方向怎么样,例如,“U”
是0
,UR
是1
,R
是2
,等等?试着把你的图像存储到字典中,方向将是关键,与之对应的是一些图像
,所以它可能是yourDict[direction]。Source=new-BitmapImage…
,当然我们应该首先检查密钥是否存在。也可以考虑使用MVVM模式。代码>图像
只是一个控件,我们不应该像保存数据那样保存它。Mario,视图不同,这就是问题所在。
public enum Direction
{
One,
TheOther
}