依赖属性在加载时没有值

发布于 2024-09-04 00:23:18 字数 2278 浏览 2 评论 0原文

我的问题是这样的,我有一个名为 profile 的 UC,其中包含另一个名为 FollowImageControl 的 UC。

在我的 Profile.xaml 中,我声明性地将 FollowImageControl 的一个名为 FollowerId 的属性绑定到 Profile.xaml.cs 中的 CurrentUserId。问题是我的 CurrentUserId 是在 Profile.xaml.cs 中分配的; Profile.xaml 代码隐藏。

这意味着我最初并没有获得 FollowerId。我在 FollowImageControl.xaml.cs 中有这些方法:

    public static readonly DependencyProperty _followUserId =
                    DependencyProperty.Register("FollowUserId", typeof(Guid), typeof(FollowImageControl), null);
    public Guid FollowUserId
    {
        get { return (Guid)GetValue(_followUserId); }
        set { SetValue(_followUserId, value); }
    }
    public FollowImageControl()
    {
        // Required to initialize variables
        InitializeComponent();
        LoggedInUserId = WebContext.Current.User.UserId;
        var ctx = new NotesDomainContext();
        if (ctx.IsFollowingUser(LoggedInUserId, FollowUserId).Value) SwitchToDelete.Begin();
    }


    private void AddImg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (LoggedInUserId != FollowUserId)
        {
            var ctx = new NotesDomainContext();
            ctx.FollowUser(FollowUserId, LoggedInUserId);
            ctx.SubmitChanges();
        }
    }

奇怪的事情,当我插入断点时,FollowImageControl() 中的 FollowerUserId 为 0,但它的值是AddImg_MouseLeftButtonDown,并且没有中间逻辑来设置它的值。这是怎么回事???

这是更多代码信息:

这是我来自 profile.xaml 的绑定,

<internalCtrl:FollowImageControl FollowUserId="{Binding ElementName=ProfileCtrl, Path=CurrentUserId}" />

这是我在 profile.xaml.cs 中的构造函数,其中设置了 CurrentUserId public static readonly DependencyProperty _CurrentUserId =

DependencyProperty.Register("CurrentUserId", typeof(Guid), typeof(Profile), null);
        public Guid CurrentUserId 
        {
            get { return (Guid)GetValue(_CurrentUserId); }
            set { SetValue(_CurrentUserId, value); }
        }

public Profile(Guid UserId) {
            CurrentUserId = UserId;
            InitializeComponent();
            Loaded += new RoutedEventHandler(Profile_Loaded);
        }

我非常惊讶,前一分钟 FollowerId 还没有值,下一分钟它就持有正确的值,而我却没有更改代码隐藏中的值。

My problem is this, I have a UC called profile that contains another UC called FollowImageControl.

In my Profile.xaml i declaretively bind a property of FollowImageControl called FollowerId to a CurrentUserId from Profile.xaml.cs. Problem is that I CurrentUserId is assigned in Profile.xaml.cs; the Profile.xaml code-behind.

This means that I do not initially get the FollowerId. I have these methods in the FollowImageControl.xaml.cs:

    public static readonly DependencyProperty _followUserId =
                    DependencyProperty.Register("FollowUserId", typeof(Guid), typeof(FollowImageControl), null);
    public Guid FollowUserId
    {
        get { return (Guid)GetValue(_followUserId); }
        set { SetValue(_followUserId, value); }
    }
    public FollowImageControl()
    {
        // Required to initialize variables
        InitializeComponent();
        LoggedInUserId = WebContext.Current.User.UserId;
        var ctx = new NotesDomainContext();
        if (ctx.IsFollowingUser(LoggedInUserId, FollowUserId).Value) SwitchToDelete.Begin();
    }


    private void AddImg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (LoggedInUserId != FollowUserId)
        {
            var ctx = new NotesDomainContext();
            ctx.FollowUser(FollowUserId, LoggedInUserId);
            ctx.SubmitChanges();
        }
    }

THE WEIRD THING IS that when i insert breakpoints the FollowerUserId in FollowImageControl() is 0, but it has a value in AddImg_MouseLeftButtonDown, and there is no inbetween logic that sets the value of it. How is this???

Here's a little more code info:

This is my binding from profile.xaml

<internalCtrl:FollowImageControl FollowUserId="{Binding ElementName=ProfileCtrl, Path=CurrentUserId}" />

this is my constructor in profile.xaml.cs wherein the CurrentUserId is set
public static readonly DependencyProperty _CurrentUserId =

DependencyProperty.Register("CurrentUserId", typeof(Guid), typeof(Profile), null);
        public Guid CurrentUserId 
        {
            get { return (Guid)GetValue(_CurrentUserId); }
            set { SetValue(_CurrentUserId, value); }
        }

public Profile(Guid UserId) {
            CurrentUserId = UserId;
            InitializeComponent();
            Loaded += new RoutedEventHandler(Profile_Loaded);
        }

I'm seriously dumbfound that one minute the FollowerId has no value, and the next it holds the right, without me having changed the value in the code-behind.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生路 2024-09-11 00:23:18

您在 FollowImageControl 构造函数中的何处设置断点?如果您在调用 InitializeComponent() 之前设置了它,这是正常行为,因为 InitializeComponent() 会执行所有 XAML。这意味着,您的绑定也是在此方法中创建的,因此在调用 InitializeComponent() 之后应该有一个值,而不是之前。当然,AddImg_MouseLeftButtonDown 在构造函数完成后被调用,因此 InitializeComponent() 也被调用。

Where did you set the breakpoint in the FollowImageControl constructor? If you had set it before the call to InitializeComponent(), this is normal behavior because InitializeComponent() executes all the XAML. This means, also your binding is created in this method, so that there should be a value after InitializeComponent() has been called, but not before. Of course, AddImg_MouseLeftButtonDown is called after your constructor has completed, and thus InitializeComponent() has also been called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文