简体   繁体   English

动态更改位图图像

[英]Changing bitmap image dynamically

I'm having issues of having an bitmap image change dynamically between UserControl and Page load. 我遇到了在UserControl和Page加载之间动态更改位图图像的问题。

I'm using a dispatcher timer to keep the image changed. 我正在使用调度程序计时器来更改图像。 However it does not work. 但是,它不起作用。 The error stated that my uriSource is null. 该错误表明我的uriSource为空。

I'm using ms visual studio 2012, metro application c# 我正在使用MS Visual Studio 2012,地铁应用程序C#

Code in UserControl xaml: UserControl xaml中的代码:

    <Image x:Name="img">
        <Image.Source>
            <BitmapImage UriSource="{Binding Path=BitmapImage}" />
        </Image.Source>
    </Image>

Code in UserControl: UserControl中的代码:

        public BitmapImage BitmapImage
        { 
            get { return _bitmapImage; }
            set
            {
                if (_bitmapImage == value) return;
                _bitmapImage = value;
                 RaisePropertyChanged("BitmapImage");
            }
         }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Codes in Page load: 页面加载中的代码:

    int eyesState = 1;

    private void minionAnimation_Tick(object sender, object e)
    {
      if (eyesState == -1)
        {

            ghosts[0]._bitmapImage.UriSource = new Uri(this.BaseUri, @"Assets/test.png");
        }
        else
        {
            ghosts[0]._bitmapImage.UriSource = new Uri(this.BaseUri, @"Assets/test2.png");
        }

        eyesState *= -1;
    }

Try: 尝试:

XAML: XAML:

<BitmapImage UriSource="{Binding Path=BitmapImageUri}" />

Code: 码:

private Uri _bitmapImageUri;
public Uri BitmapImageUri
{
    get { return _bitmapImageUri; }
    set
    {
        if (_bitmapImageUri == value) return;
        _bitmapImageUri= value;
        RaisePropertyChanged("BitmapImageUri");
    }
}

In timer: 在计时器中:

ghosts[0].BitmapImageUri = new Uri(this.BaseUri, @"Assets/test.png");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM