【问题标题】:Dependency Property register names依赖属性寄存器名称
【发布时间】:2015-12-12 15:14:57
【问题描述】:
public string City 
{  
    get { return GetValue(CityProperty).ToString(); }  
    set { SetValue(CityProperty, value); } 
}

public static readonly DependencyProperty CityProperty = 
  DependencyProperty.Register("City", typeof(string), typeof(Object1));

如果我犯了错误,请在此处: ...Register("城市"), typeof...

例如我忘记将“city”或“vity”大写

依赖属性子系统是否完全被吹走了?字符串 City 属性还在吗?我可以用各种方式设置它,静态的 CityProperty thingamabob 仍然存在,我可以用它在某个地方做点什么。我可以像 Object1.CityProperty 之类的那样做,但是故障/链接到底在哪里?如果 register 方法中的“City”文字和 City 属性不匹配,那么字符串 City 属性就不是依赖属性?

我想我的意思也是,如果字符串 City 属性调用 GetValue,那么有什么区别?子系统是否会“找到”所有内容并能够支持使用字符串 City 属性作为:a) 绑定目标 b) 动画 c) 样式

编辑

在一个有点令人不安的发展中,以下 'works' 。当您从 XAML 设置 City 属性时,它显然将其存储在某处。 a) 的哪些部分用作绑定目标、b) 样式、c) 动画对我来说是个谜。

namespace util
{
    public class foo : FrameworkElement
    {
        public String City
        {
            get { return (String)GetValue(CityProperty); }
            set { SetValue(CityProperty, value); }
        }

        // Using a DependencyProperty as the backing store for City.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CityProperty =
        DependencyProperty.Register("city", typeof(String), typeof(foo), null);
    }
}

namespace screwing_up_dependencies
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var aFoo = this.FindName("bar");
            if (aFoo is util.foo)
            {
                util.foo theFoo = (util.foo)aFoo;
                ((Button)sender).Content = theFoo.City;
            }
        }
    }
}


  <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"
            xmlns:util="clr-namespace:util"
              >
            <util:foo x:Name="bar" City="blah"></util:foo>
            <Button Content="Button"  Grid.Row="1" Click="Button_Click" Height="105"/>
    </Grid>

编辑 2 此外,您也可以这样做。注册命令的“名称”参数显然没有任何区别。而且 OwnerType 参数是什么似乎也无关紧要。

<util:foo x:Name="bar2" City="{Binding ElementName=ContentPanel, Path=Width}"></util:foo>

public static readonly DependencyProperty CityProperty =
        DependencyProperty.Register(Guid.NewGuid().ToString(), typeof(String), typeof(object), null);

var aFoo = this.FindName("bar2");
            if (aFoo is util.foo)
            {
                util.foo theFoo = (util.foo)aFoo;
                ((Button)sender).Content = theFoo.City;
            }

【问题讨论】:

  • 您是否尝试过任何方法来了解发生了什么?尝试在 XAML 中设置属性。
  • @Clemens 查看我的编辑。

标签: wpf xaml dependency-properties


【解决方案1】:

子系统是否会“找到”所有内容并能够支持将字符串 City 属性用作:a) 绑定目标 b) 动画 c) 样式

没有。

其他系统使用反射来预测有关实例的信息。当属性city 不存在时,不会发生任何操作。因为在 C# 中,可以有重载的属性名称(即Citycity),在编译器和运行时进程的眼中,它们是两个独立的实体,反映了未知实例。


是否有任何可用的 Lint 工具可以自动检测这种事情

为了避免在创建依赖属性时出现错误,我使用了 Jeff Wilcox 的 (Helpful Silverlight Snippets - Jeff Wilcox)。通过使用针对 6 种依赖属性的 sn-ps,它可以避免错误。请注意,即使它是 Silverlight,我也无需更改 WPF 即可使用它们。

【讨论】:

  • 您确定会发生“无操作”吗?我的意思是,如果我做一个 Object1.SetValue(Object1.CityProperty, "hello world") 然后我做一个 Object1.Getvalue(Object1.CityProperty) 我会得到这个值吗? AND 大写城市仍然是 Object1 的属性,所以我可以这样 那么,Text={Binding City} 是否会从 City 属性中获取字符串,该属性将调用 GetValue( Object1.CityProperty),这又将是??从“城市”内部获取价值?
  • 我猜是什么乞求,IS,是否有任何可用的 Lint 工具可以自动检测这种事情?对于不安全、无类型的字符串文字来说,这似乎是一个简单的地方,只需将像“城市”这样的变量扔到数据洞中,而一切都可以编译并且它似乎可以工作,直到您尝试使用样式继承或一些东西,你不知道发生了什么。
  • @AndyzSmith 我说的是一般情况,这取决于特定的操作。由于您指定了 City 并且存在 City 属性,因此仍然可能发生绑定。但另一方面,需要两者同步的设计时操作可能会失败。
  • 那么,现有的代码库没有 Lint 吗?您必须从第一天开始使用 sn-ps 才能获得好处?
猜你喜欢
  • 2013-08-01
  • 2016-01-19
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
相关资源
最近更新 更多