C# 如何在Silverlight中使用图像名称(字符串)查找图像
我已经创建了一个包含几个控件的页面,在这个页面中,我必须得到一个页面中的图像。我将图像名称作为字符串值。我已经做了一个for循环来查找图像并返回,但是如果页面中的所有控件都是for循环的话,那么它会很乏味,而且时间也很长 //传递字符串并作为图像查找C# 如何在Silverlight中使用图像名称(字符串)查找图像,c#,silverlight,.net-4.0,silverlight-4.0,C#,Silverlight,.net 4.0,Silverlight 4.0,我已经创建了一个包含几个控件的页面,在这个页面中,我必须得到一个页面中的图像。我将图像名称作为字符串值。我已经做了一个for循环来查找图像并返回,但是如果页面中的所有控件都是for循环的话,那么它会很乏味,而且时间也很长 //传递字符串并作为图像查找 Image imgBack = FindControl<Image>((UIElement)Layout, typeof(Image), strSelectedimg); Image-imgBack=FindControl((UIEl
Image imgBack = FindControl<Image>((UIElement)Layout, typeof(Image), strSelectedimg);
Image-imgBack=FindControl((UIElement)布局、类型(Image)、strSelectedimg);
//查找图像的函数
public T FindControl<T>(UIElement parent, Type targetType, string ControlName) where T : FrameworkElement
{
if (parent == null) return null;
if (parent.GetType() == targetType && ((T)parent).Name == ControlName)
{
return (T)parent;
}
T result = null;
int count = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < count; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
if (FindControl<T>(child, targetType, ControlName) != null)
{
result = FindControl<T>(child, targetType, ControlName);
break;
}
}
return result;
}
public T FindControl(UIElement父级,类型targetType,字符串ControlName),其中T:FrameworkElement
{
if(parent==null)返回null;
if(parent.GetType()==targetType&((T)parent.Name==ControlName)
{
返回(T)父级;
}
T结果=null;
int count=VisualTreeHelper.GetChildrenCount(父级);
for(int i=0;i
是否有其他简单的方法可以使用字符串值在页面中查找图像?也许您可以在添加图像的同时构建查找。如果您在运行时发布添加图像的代码,我可以给您一个更准确的答案;但我的想法是这样的:
private Dictionary<string, Image> _imageLookup;
private class ImageInfo
{
public string Name { get; set; }
public string Uri { get; set; }
}
private void AddImages(ImageInfo[] imageInfos)
{
this._imageLookup = new Dictionary<string, Image>();
foreach (var info in imageInfos)
{
var img = CreateImage(info.Name, info.Uri);
if (!this._imageLookup.ContainsKey(info.Name))
{
this._imageLookup.Add(info.Name, img);
}
AddImageToUI(img);
}
}
private Image CreateImage(string name, string uri)
{
// TODO: Implement
throw new NotImplementedException();
}
private void AddImageToUI(Image img)
{
// TODO: Implement
throw new NotImplementedException();
}
如果您需要查找的不仅仅是图像,您可以使用
字典
或字典
。如果您使用Silverlight Toolkit,那么您不需要自己维护这个帮助器方法,因为它附带了一个类似的方法作为扩展方法
using System.Linq;
using System.Windows.Controls.Primitives;
// ...
private void DoStuff()
{
var myImage = this.MyRootLayoutPanel.GetVisualDescendants().OfType<Image>()
.Where(img => img.Name == "MyImageName").FirstOrDefault();
}
这些图像是如何进入页面的?它们是在运行时添加的,还是在静态XAML代码中定义的?@Andrew:它们是在运行时定义的。如何从一个位置多次添加一个图像。通过这样做,它向我抛出了一个错误:“已经添加了一个具有相同键的项”。如果您添加了三个具有相同键K的图像,那么当您将K传递给FindControl时,您希望FindControl返回什么?如果三个图像中的任何一个都可以接受,那么在添加到查找之前,您可以在(!this.\u imageLookup.ContainsKey(uri))中放置
If
。或者,您可能应该使用不同的键。在绑定来自同一位置的图像时,该键包含不同的名称。我的答案现在使用控件的名称作为键,而不是URI。
using System.Linq;
using System.Windows.Controls.Primitives;
// ...
private void DoStuff()
{
var myImage = this.MyRootLayoutPanel.GetVisualDescendants().OfType<Image>()
.Where(img => img.Name == "MyImageName").FirstOrDefault();
}
public class MyFancyControl : Control
{
public MyFancyControl()
{
this.DefaultStyleKey = typeof(MyFancyControl);
}
// ...
public override void OnApplyTemplate()
{
var myImage = this.GetTemplateChild("MyImageName") as Image;
}
}