user2607110
user2607110

Reputation: 127

Using UWP CalendarDatePicker results in Windows.Foundation.IReference`1 <Windows.Foundation.DateTime>

This may be stupid, as I am just learning UWP and figured I'd try making a tool that uses CalendarDatePicker, however I cannot get it to display the date entered in the box.

XAML

<TextBlock x:Name="dateOutput"/>
<CalendarDatePicker x:Name="OrderDate" DateChanged="ChangeOrderDate"/>

C++

void OrderService::MainPage::ChangeOrderDate(Windows::UI::Xaml::Controls::CalendarDatePicker^ sender, Windows::UI::Xaml::Controls::CalendarDatePickerDateChangedEventArgs^ args)
{

dateOutput->Text = OrderDate->Date->Value.ToString();

}

When I run this code, I can select a date but what displays is the following:

Windows.Foundation.IReference`1 Windows.Foundation.DateTime>

Any help would be appreciated.

Upvotes: 1

Views: 538

Answers (2)

Sunteen Wu
Sunteen Wu

Reputation: 10627

For the DateTime value of the CalendarDatePicker, C++/CX uses the Windows::Foundation::DateTime structure. Details please reference DateTime and Calendar values. Directly invoking ToString() will return the type, for reason you could check @André B's reply.

We may need to get the DateTime.UniversalTime value to convert according to the Remarks of DateTime Structure:

To convert the UniversalTime to SYSTEMTIME, use ULARGE_INTEGER to convert the int64 value to FILETIME, then use FileTimeToSystemTime to get SYSTEMTIME.

But you could just use DateTimeFormatter class in UWP app to format your DataTime for display. Details for how to do please reference this article and this sample. For example:

void CCalendar::MainPage::ChangeOrderDate(Windows::UI::Xaml::Controls::CalendarDatePicker^ sender, Windows::UI::Xaml::Controls::CalendarDatePickerDateChangedEventArgs^ args)
{ 
    DateTime current = OrderDate->Date->Value; 
    Array<DateTimeFormatter^> ^dateFormatters = {
        ref new DateTimeFormatter("shortdate"),
        ref new DateTimeFormatter("longdate"),
        ref new DateTimeFormatter("shorttime"),
        ref new DateTimeFormatter("longtime")
    };
    String^ results = ""; 
    // Generate the results.
    for (auto formatter : dateFormatters)
    {
        // Perform the actual formatting. 
        results = results + formatter->Template + ": " + formatter->Format(current) + "\n";
    }
    dateOutput->Text = results;  
}

Upvotes: 0

Andr&#233; B
Andr&#233; B

Reputation: 1709

What is actually being shown on the Textblock it's the DateTime.Value.ValueType, rather than the actual value of DateTime.Value.

Windows.Foundation.IReference is an hidden interface, which appears as Nullable<T> in .NET and as Platform::IBox in C++/CX, and .Value property that you are trying to access, is part of the implementation of this same interface.

The ValueType::ToString method overrides the Object::ToString method and provides the default implementation of the ToString method for value types. (Value types are types defined by the struct keyword in C#

DateTime is a value type, and that's why ::ToString() is being overriden by ValueType::ToString

Can you try to overload the toString, also including an argument indicating what is the desired format output for your Date value, like "dddd dd MMMM" ?

Upvotes: 1

Related Questions