等待导致PropertyChanged过早开火

时间:2016-12-08 13:56:30

标签: c# uwp

我有以下课程:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImportDefinitieGenerator.LinkTypeDetail.Detail
{

    abstract class LinkTypeDetailBase : INotifyPropertyChanged
    {
        public string Property { get; set; }
        private string _Value;

        public event PropertyChangedEventHandler PropertyChanged;

        public string Value { get { return _Value; }
            set
            {
                _Value = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
            }
        }
        public LinkTypeDetailBase(string Property)
        {
            this.Property = Property;
            this.Value = "";
        }

        internal abstract void Click();
    }
}

美好而简单。当Value被更改PropertyChanged时,此类正常工作。但是现在我试图从对话框中设置Value,所以我使用:

Value = await StringDialog.getString(Property, Value);

反过来调用

public async static Task<string> getString(string title, string defaultText = "")
{
    StringDialog stringDialog = new StringDialog(title, defaultText);
    await stringDialog.ShowAsync();
    return stringDialog.TextBox.Text;
}

现在我的问题出现了。除了PropertyChanged事件在StringDialog出现的那一刻被激发,而不是在等待和返回之后,一切都运行良好和花花公子。结果,该值在发生时尚未改变。

这是预期的行为吗?如果是这样,那么处理这个问题的正确方法是什么?如果没有,我将如何妥善解决这个问题? (我确定我可以破解它,所以我特别询问如何正确地做到这一点。)

编辑:

好的,在尝试创建一个Minimal,Complete和Verifyable示例后,我似乎错了。我将附上代码以供将来参考。这个错误不是我预期的错误,因此这已经成为我的代码中的一个错误,因为这个问题偏离了主题。当我找到原因时,我会发布它会对将来的参考有用。

using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace MCV
{
    sealed partial class App : Application
    {
        private static VerifyMyBug verifyMyBug;

        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {
            Frame rootFrame = Window.Current.Content as Frame;
            if (rootFrame == null)
            {
                rootFrame = new Frame();
                rootFrame.NavigationFailed += OnNavigationFailed;
                Window.Current.Content = rootFrame;
            }
            if (e.PrelaunchActivated == false)
            {
                if (rootFrame.Content == null)
                {
                    rootFrame.Navigate(typeof(MainPage), e.Arguments);
                }
                Window.Current.Activate();
            }

            verifyMyBug = new VerifyMyBug();
            verifyMyBug.PropertyChanged += (o, e1) =>
            {
                throw new NotImplementedException("Property changed");
            };
            verifyMyBug.DelayedSet();
        }
        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }
        private void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            deferral.Complete();
        }
    }

    class VerifyMyBug : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _Value;
        public string Value
        {
            get { return _Value; }
            set
            {
                _Value = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
            }
        }

        public async void DelayedSet()
        {
            Value = await GetStringDelayed();
            throw new NotImplementedException("Done setting variable");
        }

        public async Task<string> GetStringDelayed()
        {
            await Task.Delay(TimeSpan.FromSeconds(5));
            return "Hello";
        }
    }
}

编辑2: 好的,事件确实发生了,它一路上就丢失了。我为浪费你的时间而道歉。

0 个答案:

没有答案