Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
972 views
in Technique[技术] by (71.8m points)

xamarin - Parse an actual time to TimePicker

I'm new here so bare a bit with me. And yes i tried to google my question but really wasn't shure about the answers i found either. So here's my problem: I want to build an App where the User can select a Times from clicking different Buttons, and then calculate the Timedifference between. The ButtonClick opens the TimePicker Dialog and the Examplecode i found on Microsoft Docs uses always the actual time. What i want is to use the last valid time from parsing the Buttontext. But i have no idea how to pass the ID of the senderbutton to the TimePicker class.

Here's the Eventhandler from the Button:

    void TimeSelectOnClick (object sender, EventArgs eventArgs)
    {
        // Instantiate a TimePickerFragment (defined below) 
        TimePickerFragment frag = TimePickerFragment.NewInstance (

            // Create and pass in a delegate that updates the Activity time display 
            // with the passed-in time value:
            delegate (DateTime time)
            {
                timeSelectButton.Text = time.ToString("HH:mm");
            });

        // Launch the TimePicker dialog fragment (defined below):
        frag.Show(FragmentManager, TimePickerFragment.TAG);
    }

and here's the TimePicker dialog fragment:

// TimePicker dialog fragment
public class TimePickerFragment : DialogFragment, TimePickerDialog.IOnTimeSetListener
{
    // TAG used for logging
    public static readonly string TAG = "MyTimePickerFragment";

    // Initialize handler to an empty delegate to prevent null reference exceptions:
    Action<DateTime> timeSelectedHandler = delegate { };

    // Factory method used to create a new TimePickerFragment:
    public static TimePickerFragment NewInstance(Action<DateTime> onTimeSelected)
    {
        // Instantiate a new TimePickerFragment:
        TimePickerFragment frag = new TimePickerFragment();

        // Set its event handler to the passed-in delegate:
        frag.timeSelectedHandler = onTimeSelected;

        // Return the new TimePickerFragment:
        return frag;
    }

    // Create and return a TimePickerDemo:
    public override Dialog OnCreateDialog (Bundle savedInstanceState)
    {
        //Set the TimePicker default time 
        //Here i Want to parse the time from the button something like DateTime.Parse(buttonID.Text);
        //Either with current time or parsed time... how to pass values from the sender button i have no idea
        DateTime currentTime = DateTime.Parse("06:00");

        // force 24-hour time format:
        bool is24HourFormat = true;

        // Instantiate a new TimePickerDemo, passing in the handler, the current 
        // time to display, and whether or not to use 24 hour format:
        TimePickerDialog dialog = new TimePickerDialog
            (Activity, this, currentTime.Hour, currentTime.Minute, is24HourFormat);
   
        // Return the created TimePickerDemo:
        return dialog;
    }

    // Called when the user sets the time in the TimePicker: 
    public void OnTimeSet(TimePicker view, int hourOfDay, int minute)
    {
        // Get the current time:
        DateTime currentTime = DateTime.Now;

        // Create a DateTime that contains today's date and the time selected by the user:
        DateTime selectedTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, hourOfDay, minute, 0);

        // Log the date and selected time:
        Log.Debug(TAG, selectedTime.ToLongTimeString());

        // Invoke the handler to update the Activity's time display to the selected time:
        timeSelectedHandler (selectedTime);
    }

}

thanks in advance to anybody here on Stackoverflow! You guys really do a great Job!

Cheers


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could add a property in TimePickerFragment

static string TimeString;
 public static TimePickerFragment NewInstance(Action<DateTime> onTimeSelected,string time)
    {
        // Instantiate a new TimePickerFragment:
        TimePickerFragment frag = new TimePickerFragment();

        // Set its event handler to the passed-in delegate:
        frag.timeSelectedHandler = onTimeSelected;

        TimeString = time;

        // Return the new TimePickerFragment:
        return frag;
    }

in Activity

TimePickerFragment frag = TimePickerFragment.NewInstance (

            // Create and pass in a delegate that updates the Activity time display 
            // with the passed-in time value:
            delegate (DateTime time)
            {
                timeSelectButton.Text = time.ToString("HH:mm");
            },buttonID.Text);

And you can modify it in any place

TimePickerFragment.TimeString = "xxx";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...