-1

I created a WinUI 3 project. I have a timepicker field and it has the value selectedtime="11:11". I can't figure out how to get its value in code

Tried like this MainWindows.xaml

<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
    <TimePicker x:Name="Control2" ClockIdentifier="24HourClock" Header="24 hour clock" SelectedTime="11:11" />
    <Button  x:Name="myButton" Content="Click" Click="myButton_Click"></Button>
</StackPanel>

MainWindows.xaml.cpp

void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
    {
        auto selectedTime = Control2().SelectedTime();
        auto nameTimePicker = Control2().Name(); // get property value x:Name = Control2
        myButton().Content(box_value(selectedTime));
        // This closes the program
        // selectedTime.GetString().c_str()
    }

Button text Windows.Foundation.IReference<Windows.Foundation.TimeSpan>

Can you please tell me how I can also get the text time SelectedTime = "11:11"? as I received x:Name="Control2".

IInspectable
  • 46,945
  • 8
  • 85
  • 181
Zend
  • 33
  • 4
  • See here: https://learn.microsoft.com/en-us/uwp/api/windows.foundation.timespan.duration *TimeSpan is a specialization of std::chrono::duration* and this: https://stackoverflow.com/questions/42866524/convert-chronoduration-to-string-or-c-string for example – Simon Mourier Feb 07 '23 at 08:01
  • This example shows how to set, and I need to get the value of the timepicker, namely its fields selectedtime="11:11" – Zend Feb 07 '23 at 08:06
  • TimePicker has SelectedTime or Time, with C++/WinRT they are bound as `std::chrono::duration`. There's nothing more with this language. – Simon Mourier Feb 07 '23 at 08:34
  • So the actual question here is how to convert a `std::chrono::duration` into a textual representation? – IInspectable Feb 07 '23 at 09:34
  • @IInspectable not how to convert, but how to get the time 11:11, into the text and insert it into the button (the name of the button will be 11:11), from the SelectedTime = "11:11" field. Just like I got the TimePicker's name (Control2().Name()) – Zend Feb 07 '23 at 11:10
  • So, that's a *"yes"*, even though you believe the problem to be something else. `Control2().Name()` returns a (value compatible with) `HSTRING`. That can be used as the source to set a `Button`'s `Content` property. The same does not apply for a `std::chrono::duration` (or `TimeSpan`). You'll have to convert that to a textual representation first. That's going to involve a concept called "locale", which could be inferred in more than one way. You'll have to be explicit about that conversion, meaning that you have to write code. – IInspectable Feb 07 '23 at 11:39
  • @IInspectable Could you show an example of how you can convert Control2().SelectedTime to text? – Zend Feb 07 '23 at 11:44

1 Answers1

0

Use std::format with std::formatter<std::chrono::duration>.

auto selectedTime = Control2().SelectedTime().Value();
auto nameTimePicker = Control2().Name(); // get property value x:Name = Control2
std::string s = std::format("{:%R}", selectedTime);//"C" locale
myButton().Content(box_value(winrt::to_hstring(s)));

enter image description here

Other information: winrt::hstring functions and operators.

YangXiaoPo-MSFT
  • 1,589
  • 1
  • 4
  • 22