首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何连接ImageSource的绑定?

如何连接ImageSource的绑定?
EN

Stack Overflow用户
提问于 2019-01-07 08:01:39
回答 1查看 85关注 0票数 0

我正在尝试在我的WPF Window上包含一个县海豹的图像,并将源绑定到一个BitmapImage属性,但是图像根本不显示。我还想单击印章并使用LeftMouseButtonDown事件将第一个图像更改为其他七个图像,每次更改之间有两秒的延迟。

我在this StackOverflow post中尝试了这些技术。

MainWindow.xaml中的图像定义:

代码语言:javascript
复制
<Image x:Name="imag_CountySeal" Margin="0,60,0,80" Grid.Row="0" 
    Grid.Column="0"
    Source="{Binding ImageSource, UpdateSourceTrigger=PropertyChanged,Mode=OneWay}"
    MouseLeftButtonDown="Mouse_Down_Seal"
    Width="165" Height="165" Visibility="Visible" />

具有ImageSource属性和PropertyChanged事件的类定义:

代码语言:javascript
复制
public class CountySeals : INotifyPropertyChanged
{
    private BitmapImage _ImageSource;
    public BitmapImage ImageSource
    {
        get { return _ImageSource; }
        set
        {
            _ImageSource = value;
            OnPropertyChanged(new PropertyChangedEventArgs("ImageSource"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }
}

MainWindow.xaml.cs开头的逻辑,用于为第一个镜像设置ImageSource

代码语言:javascript
复制
public partial class MainWindow : Window
{
    CountySeals seals;

    public MainWindow()
    {
        InitializeComponent();
        seals = new CountySeals();
        seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_l transparent.png", UriKind.Absolute));
        SizeChanged += new SizeChangedEventHandler(Window_SizeChanged);
        StateChanged += new EventHandler(Window_StateChanged);
        LocationChanged += new EventHandler(Window_LocationChanged);

MainWindow.xaml.cs中的Mouse_Down_Seal代码:

代码语言:javascript
复制
private void Mouse_Down_Seal(object sender, MouseButtonEventArgs e)
{
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_2 transparent.png", UriKind.Absolute));
    Thread.Sleep(2000);
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_3 transparent.png", UriKind.Absolute));
    Thread.Sleep(2000);
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_4 transparent.png", UriKind.Absolute));
    Thread.Sleep(2000);
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_5 transparent.png", UriKind.Absolute));
    Thread.Sleep(2000);
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_6 transparent.png", UriKind.Absolute));
    Thread.Sleep(2000);
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_7 transparent.png", UriKind.Absolute));
    Thread.Sleep(2000);
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_8 transparent.png", UriKind.Absolute));
}

如何连接ImageSourceBinding

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-07 08:22:08

您需要将seals设置为您的MainWindow DataContext:

代码语言:javascript
复制
public MainWindow()
{
    InitializeComponent();
    seals = new CountySeals();
    this.DataContext = seals; // <---------
    ... etc ...

更新:位图不显示的问题是完全不同的问题。您的鼠标处理程序是在GUI线程上调用的,但是您随后使用Task.Sleep命令将该线程捆绑在一起,因此它永远没有机会更新图像。事实上,你会发现当这一切发生的时候,你的整个应用程序都会冻结。您需要在单独的thread...and中分配ImageSource并执行休眠等操作。在C#中,正确的方法是使用Tasks (除了非常非常少的例外,您永远不应该在C#中调用Thread.Sleep() )。您还需要添加代码来检查任务是否已经在运行,如果已经运行,则首先取消它。下面这样的代码应该能起到作用:

代码语言:javascript
复制
private Task DisplayTask;
private CancellationTokenSource CancelSource;

private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
    // cancel any existing task and wait for it to finish
    if (this.CancelSource != null)
    {
        this.CancelSource.Cancel();
        try
        {
            this.DisplayTask.Wait(this.CancelSource.Token);
        }
        catch (OperationCanceledException)
        {
            // catches the expected exception here
        }

    }

    // start a new task
    this.CancelSource = new CancellationTokenSource();
    this.DisplayTask = Task.Run(DisplayImages);
}

private async Task DisplayImages()
{
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_2 transparent.png", UriKind.Absolute));
    await Task.Delay(TimeSpan.FromSeconds(2), this.CancelSource.Token);
    seals.ImageSource = new BitmapImage(new Uri(@"C:\Users\billw\Documents\Visual Studio 2015\Projects\Images\seal_3 transparent.png", UriKind.Absolute));
    await Task.Delay(TimeSpan.FromSeconds(2), this.CancelSource.Token);
    ... etc ...
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54067072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档