C++/CX CreateTask pass value

浪子不回头ぞ 提交于 2019-12-14 04:10:57

问题


I pass value to property of class in CreateTask, use the property value outside the AsyncTask class, the property value is not change, but the property value change in button click event.

the CreateTask in the AsyncTask class, AsyncTask class have a N property

namespace CreateTask
{
    public ref class AsyncTask sealed : INotifyPropertyChanged
    {
    public:
        AsyncTask();
        void InitTask();
        void ShowN();
        IAsyncOperation<float64>^ GetPrimesAsync(float64 x, float64 y);
    private:
        float64 _n;
        bool _isPropertyChangedObserved;
        event PropertyChangedEventHandler^ _privatePropertyChanged;
    protected:
        void OnPropertyChanged(Platform::String^ propertyName)
        {
            if (_isPropertyChangedObserved)
            {
                PropertyChanged(this, ref new PropertyChangedEventArgs(propertyName));
            }
        }
    public:
        property float64 N
        {
            float64 get()
            {
                return _n;
            }

            void set(float64 value)
            {
                _n = value;
                OnPropertyChanged("N");
            }
        }
        virtual event Windows::UI::Xaml::Data::PropertyChangedEventHandler^ PropertyChanged
        {
            virtual Windows::Foundation::EventRegistrationToken add(Windows::UI::Xaml::Data::PropertyChangedEventHandler^ e)
            {
                _isPropertyChangedObserved = true;
                return _privatePropertyChanged += e;
            }
            virtual void remove(Windows::Foundation::EventRegistrationToken t)
            {
                _privatePropertyChanged -= t;
            }

        protected:
            virtual void raise(Object^ sender, Windows::UI::Xaml::Data::PropertyChangedEventArgs^ e)
            {
                if (_isPropertyChangedObserved)
                {
                    _privatePropertyChanged(sender, e);
                }
            }
        }
    };
}


this code is AsyncTask.Cpp  

AsyncTask::AsyncTask()
{
    InitTask();

}

void AsyncTask::InitTask()
{
    create_task(GetPrimesAsync(111.1, 222.2)).then(
        [this](float64 z)

    {   
        N = z;
    });
}

IAsyncOperation<float64>^ AsyncTask::GetPrimesAsync(float64 x, float64 y)
{
    return create_async([this, x, y]() -> float64
    {
        // Ensure that the input values are in range. 
        float64 z;
        z = x + y;

        return z;
    });
}

void AsyncTask::ShowN()
{
    MessageDialog^ msg = ref new MessageDialog(N.ToString());
    msg->ShowAsync();
}



this MainPage.xaml.h code

  namespace CreateTask
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public ref class MainPage sealed
    {
    public:
        MainPage();
    private:
        AsyncTask^ asyncTask;
    private:
        void Click_Button(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
    };
}



this MainPage.xaml.cpp code

MainPage::MainPage()
{
    InitializeComponent();
    asyncTask = ref new AsyncTask();
    MessageDialog^ msg = ref new MessageDialog(asyncTask->N.ToString());
    msg->ShowAsync();
}



void CreateTask::MainPage::Click_Button(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    MessageDialog^ msg = ref new MessageDialog(asyncTask->N.ToString());
    msg->ShowAsync();
}

来源:https://stackoverflow.com/questions/41155521/c-cx-createtask-pass-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!