您好
我想创建一个WPF中的图像控制,但在运行时,无法在页面上显示任何图片,任何人都可以帮助在运行时改变图像的循环。下面是我的代码: -
string[] imgs = new string[]{ "print1.png", "print2.png", "print3.png", "print4activation-click.png"};
string[] caption = new string[] { "Picture1", "Picture2", "Picture3", "Picture4"};
private int selected = 0;
private string _threadOutput = "";
public readonly SynchronizationContext mySynchronizationContext;
bool _stopThreads = false;
public MainWindow()
{
InitializeComponent();
mySynchronizationContext = SynchronizationContext.Current;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Thread thread1 = new Thread(new ThreadStart(DisplayThread1));
thread1.SetApartmentState(ApartmentState.STA);
thread1.IsBackground = true;
thread1.Start();
}
void DisplayThread1()
{
while (_stopThreads == false)
{
if (selected == 0)
{
selected = imgs.Length - 1;
showImage(imgs[selected], selected);
}
else if (selected == imgs.Length - 1)
{
selected = 0;
showImage(imgs[selected], selected);
}
else
{
selected = selected + 1;
}
}
}
private void showImage(string img, int i)
{
BitmapImage b1 = new BitmapImage();
b1.BeginInit();
b1.UriSource = new Uri(img,UriKind.RelativeOrAbsolute);
b1.EndInit();
b1.Freeze();
SendOrPostCallback callback = _ =>
{
Action action = delegate {image1.Source = b1;};
image1.Dispatcher.Invoke(action);
};
mySynchronizationContext.Send(callback, null);
}