如何制作自动更换照片?

时间:2017-07-06 07:16:25

标签: c# wpf

我是一名真正的初学者"编码员",我有工作,制作一张自动照片 使用textbox(写xy秒,它将是2图像之间的时间)

        string path = @"C:\Teszt\";            
        string[] Files = Directory.GetFiles(path, "*.jpg");
        List<string> fileList = new List<string>();
        foreach (var item in Files)
        {
            fileList.Add(item);
        }
        for (int i = 0; i < fileList.Count; i++)
        {

            string year= "";
            year += DateTime.Now.Year;
            string month = "";
            month += DateTime.Now.Month;
            string day = "";
            day += DateTime.Now.Day;
            //update textbox
            Date.Content = year + "." + month + "." + day + ".";

            string filepath = fileList[i];
            var urii = new Uri(filepath);
            var bitmaap = new BitmapImage(urii);
            image.Source = new BitmapImage(new Uri(fileList[i]));
            Thread.Sleep(TimeSpan.FromSeconds(10));
            if (i == fileList.Count)
                i = 0;
        }
    }
    private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        Thread.Sleep(TimeSpan.FromSeconds(int.Parse(this.TextBox.Text)));
    }

1 个答案:

答案 0 :(得分:2)

  • 您要做的一件事是将代码分解为具有明确,单一责任的方法。这使您的程序更容易理解

  • 日期文本框代码在该循环中没有业务

  • 你需要一种方法来等待直到下一张照片 背景,所以它不会阻止你的UI线程,仍然允许你 在文本框中输入文本。 dispatcherTimer是一个很好的方式。

  • 您需要确保在执行任何操作之前可以将文本框中的文本解析为数字

private List<string> kepek = new List<string>();
private int képnévIndex = 0;
private DispatcherTimer timer;


//this is my stand-in for your constructor. you didn't put it in the example, so i'm using "myClass"
public myClass()
{
    // you will still need InitializeComponent();
    InitializeComponent();

    LoadImages();
    setupTimer(10);
    displayCurrentDate();
}

private void setupTimer(int timeoutInSeconds)
{
    if(timer != null)
    {
        timer.Stop();
    }
    timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(timeoutInSeconds) };

    timer.Tick += (sender, args) =>
    {
         showNextPicture();
    };

    timer.Start();
}

private void LoadImages()
{
    string path = @"C:\Teszt\";            
    string[] Files = Directory.GetFiles(path, "*.jpg");

    foreach (var item in Files)
    {
        kepek.Add(item);
    }
}

private void displayCurrentDate()
{
    string év = "";
    év += DateTime.Now.Year;
    string hónap = "";
    hónap += DateTime.Now.Month;
    string nap = "";
    nap += DateTime.Now.Day;              
    Date.Content = év + "." + hónap + "." + nap + ".";
}

private void showNextPicture()
{
    string képnév = kepek[képnévIndex];
    image.Source = new BitmapImage(new Uri(képnév));

    képnévIndex++;

    if (képnévIndex == kepek.Count)
        képnévIndex = 0;

}

private void Szövegdoboz_TextChanged(object sender, TextChangedEventArgs e)
{
    int parsedNumberOfSeconds;
    //if we entered something that can not be parsed to a number, exit.
    if(!int.tryParse(Szövegdoboz.Text, out parsedNumberOfSeconds))
        return;

    setupTimer(parsedNumberOfSeconds);
}