将MainWindow.cs中的参数传递给CurrentRentWeek.cs,然后在MainWindow.cs中输出结果

时间:2014-09-18 11:13:07

标签: c# xaml

我想将参数传递给存储在CurrentRentWeek类中的方法,然后将CurrentRentWeekTextBlock.Text设置为值(rentWeek)。这可能吗?如果是这样,我该怎么做呢?

MainWindow.cs

    public MainWindow()
    {
        InitializeComponent();
        CurrentDateTimeTextBlock.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
        var datecheckObject = new CurrentRentWeek();
        datecheckObject.DateCheck(CurrentRentWeekTextBlock.Text);
    }

CurrentRentWeek.cs

class CurrentRentWeek
{
    private DateTime cRentWeekStart;
    private DateTime cRentWeekEnd;
    private DateTime today = DateTime.Now;

    public void DateCheck(string rentWeek)
    {
        if (today.DayOfWeek == DayOfWeek.Thursday)
        {
            cRentWeekStart = today.AddDays(-5);
            cRentWeekEnd = today.AddDays(2);
            rentWeek = "Current Rent Week: " + cRentWeekStart.ToString("dd/MM/yyyy") + " - " + cRentWeekEnd.ToString("dd/MM/yyyy");
        }
        else
        {
            rentWeek = "";
        }
    }
}

1 个答案:

答案 0 :(得分:1)

DateCheck返回一个字符串值,并使用该返回值设置该值。试试这个

public MainWindow()
{
    InitializeComponent();
    CurrentDateTimeTextBlock.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
    var datecheckObject = new CurrentRentWeek();
    CurrentRentWeekTextBlock.Text = datecheckObject.DateCheck(CurrentRentWeekTextBlock.Text);
}

CurrentRentWeek.cs

class CurrentRentWeek
{
    private DateTime cRentWeekStart;
    private DateTime cRentWeekEnd;
    private DateTime today = DateTime.Now;

    public string DateCheck(string rentWeek)
    {
        if (today.DayOfWeek == DayOfWeek.Thursday)
        {
            cRentWeekStart = today.AddDays(-5);
            cRentWeekEnd = today.AddDays(2);
            rentWeek = "Current Rent Week: " + cRentWeekStart.ToString("dd/MM/yyyy") + " - " + cRentWeekEnd.ToString("dd/MM/yyyy");
        }
        else
        {
            rentWeek = "";
        }
        return rentWeek;
    }
}