简体   繁体   English

在C#中使用反射访问和修改xaml元素

[英]Accessing and Modifying xaml elements with Reflection in C#

I have a model that has a number of attributes that may or may not be null, depending on if the data is available for those specific attributes on a remote server. 我有一个模型,该模型具有许多可能为null或不为null的属性,具体取决于数据是否可用于远程服务器上的那些特定属性。

I'm building a simple windows phone application that gives more easily accessible versions of this information on a mobile phone. 我正在构建一个简单的Windows Phone应用程序,该应用程序可在手机上提供此信息的更易于访问的版本。 I found that in the case that the properties of the model above are not set, the value doesn't show up (shows a blank), but the label still shows up. 我发现在未设置上述模型的属性的情况下,该值未显示(显示为空白),但标签仍显示。

After considering a number of different alternatives for this (including massive amounts of if statements, ugh), I decided that if I set the name for specific attributes in the xaml to match the properties of the data model that I was looking at, I then in real time have the system analyze the properties of the data model, and elements of my xaml. 在考虑了许多不同的选择(包括大量的if语句,ugh)之后,我决定,如果我在xaml中设置特定属性的名称以匹配我所查看的数据模型的属性,那么实时让系统分析数据模型的属性以及我的xaml的元素。 If a property in my xaml matched the name of the property in my model, and the model was null, I could turn the visibility to collapsed. 如果xaml中的属性与模型中的属性名称匹配,并且该模型为null,则可以将可见性折叠。 If the model was not null, make it visible. 如果模型不为空,则使其可见。 Thus I could have a clean, dynamic solution that would show only the data that is actually available. 因此,我可以得到一个干净,动态的解决方案,该解决方案将仅显示实际可用的数据。

Here's the code 这是代码

PropertyInfo[] properties = data.GetType().GetProperties();

foreach (PropertyInfo property in properties)
{

    FieldInfo view = this.GetType().GetField(property.Name);

    if (view != null)
    {
        if (property.GetValue(data, null) == null)
        {
            object aView = view.GetValue(this);
            aView.GetType().GetProperty("Visibility").SetValue(aView, "Collapsed", null);
        }
        else
        {
            object aView = view.GetValue(this);
            aView.GetType().GetProperty("Visibility").SetValue(aView, "Visible", null);
        }
    }
}

Unfortunately, I hit a snag. 不幸的是,我遇到了障碍。 I can't figure out how to access the xaml elements. 我不知道如何访问xaml元素。 I've tried to use 我尝试使用

this.GetType().GetProperties()
this.GetType().GetFields()
this.GetType().GetMembers()

To find the elements that I'm looking for, but they don't show up in any of those. 查找我要寻找的元素,但这些元素均未显示。 Is there something I'm missing. 有什么我想念的吗?

Is there a better way to do this that is more beautiful? 有没有更好的方法可以做到这一点呢?

Thanks in advance for your help. 在此先感谢您的帮助。

If you have named elements in your XAML as follows: 如果您在XAML中具有如下命名元素:

<Grid>
  <TextBlock x:Name="txt" />
</Grid>

You can find them via the FindName method: 您可以通过FindName方法找到它们:

TextBlock txt = this.FindName("txt") as TextBlock;

No reflection is required! 不需要反射!

You could create a custom converter which is used in a Visibility binding expression for each element. 您可以创建一个自定义转换器,用于每个元素的可见性绑定表达式。 This converter can check the property passed in, and if null, then return Collapsed as the value, otherwise Visible. 此转换器可以检查传入的属性,如果为null,则返回Collapsed作为值,否则返回Visible。

Similar to what you're doing, but all declarative, and much faster than using reflection. 与您正在执行的操作类似,但是都是声明性的,并且比使用反射要快得多。

Here is an example - http://digitalmoto.net/blog/2011/02/28/null-object-to-visibility-converter/ 这是一个示例-http://digitalmoto.net/blog/2011/02/28/null-object-to-visibility-converter/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM