2017-05-05 103 views
0

我想要的是在要禁用的特定日期之前設置日期(不在今天之前)。例如:如何在DatePickerDialog中使用特定日期而不是今天(XAMARIN)設置最短日期

今天是2017年5月5日 具體目標日期:僅限5月1日至5月5日。

所有比這一天越大使用此代碼

禁用,但我不能從5月1日之前禁用它。

我現在有這個代碼。

public class DatePickerFragment : DialogFragment, 
           DatePickerDialog.IOnDateSetListener 
{ 
    // TAG can be any string of your choice. 
    public static readonly string TAG = "X:" + typeof(DatePickerFragment).Name.ToUpper(); 

    // Initialize this value to prevent NullReferenceExceptions. 
    Action<DateTime> _dateSelectedHandler = delegate { }; 

    public static DatePickerFragment NewInstance(Action<DateTime> onDateSelected) 
    { 
     DatePickerFragment frag = new DatePickerFragment(); 
     frag._dateSelectedHandler = onDateSelected; 
     return frag; 
    } 

    public override Dialog OnCreateDialog(Bundle savedInstanceState) 
    { 
     DateTime currently = DateTime.Now; 
     DatePickerDialog dialog = new DatePickerDialog(Activity, 
                 this, 
                 currently.Year, 
                 currently.Month-1, 
                 currently.Day); 

     //****************this is my problem*****************// 
     dialog.DatePicker.MinDate = CurrentUser.lastReplenish.Millisecond; 
     dialog.DatePicker.MaxDate = Java.Lang.JavaSystem.CurrentTimeMillis(); 
     //***************************************************// 
     return dialog; 
    } 

    public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
    { 
     // Note: monthOfYear is a value between 0 and 11, not 1 and 12! 
     DateTime selectedDate = new DateTime(year, monthOfYear + 1, dayOfMonth); 
     Log.Debug(TAG, selectedDate.ToLongDateString()); 
     _dateSelectedHandler(selectedDate); 
    } 
} 

回答

1

我猜 「CurrentUser.lastReplenish」 是一個.NET DateTime對象? 的Android總是需要從毫秒1970年1月1日(時期)開始,所以你需要計算了一下:

dialog.DatePicker.MinDate = (long)CurrentUser.lastReplenish.ToUniversalTime() 
    .Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds; 
+0

我只是編碼它現在和它完美的作品!非常感謝! :) – jace

相關問題