C#WPF图像未更改? .............. [英] C# WPF image not changing? ..............

查看:62
本文介绍了C#WPF图像未更改? ..............的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

this is my code

int n=1;
    private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (n == 1)
        {
            myStoryboard.Begin(this);
            myStoryboarda.Begin(this);
            BitmapImage img = new BitmapImage();
            img.BeginInit();
            img.UriSource = new Uri("pack://application:,,/Resources/MB2.png");
            img.EndInit();
            image1.Source = img;
            n = 2;
        }
        else
        {
            myStoryboard2.Begin(this);
            myStoryboardb.Begin(this);
            BitmapImage img = new BitmapImage();
            img.BeginInit();
            img.UriSource = new Uri("pack://application:,,/Resources/MB2.png");
            img.EndInit();
            n = 1;
        }
    }



我在这条线上出现错误



and i get an error on this line

img.EndInit();



错误显示

Cannot locate resource ''resources/mb2.png''.

我的资源称为"MB2",它是我资源中的png.出于某种原因,它不起作用,我也不知道为什么,有人可以帮帮我吗?



and the error says

Cannot locate resource ''resources/mb2.png''.

and my resource is called " MB2 " and it is a png in my resources. and for some reason it just doesn''t work and i don''t know why, can some one please help me?

推荐答案

在App中创建资源. xaml

create resource in the App.xaml

<ImageSource x:Key="test1">banner_img_1.jpg</ImageSource>
<ImageSource x:Key="test2">banner_img_2.jpg</ImageSource>



现在像这样更改源代码



Now change the source like that

private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
       {
           image1.Source = this.FindResource("test2") as ImageSource;
       }


这可能是网址路径出现的问题.
只需尝试new URI(/Resources/MB2.png).
This could be an issue with the url path.
Just try new URI(/Resources/MB2.png).


这篇关于C#WPF图像未更改? ..............的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆