我们如何匹配WPF中图像控件的内容 [英] how we will match the content of image control in wpf

查看:57
本文介绍了我们如何匹配WPF中图像控件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望该图像控件包含gol1s2.gif图像,它将转移到window2.xaml形式,否则将转移到window3.xaml形式.

i want if that image control will contain gol1s2.gif image it will transfer to window2.xaml form otherwise it will transfer to window3.xaml form.

<window x:class="WpfApplication1.Window1" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="509">
    <grid height="275" width="271">
        <Image Margin="10,10,61,115" Grid.Row="0" Grid.Column="0"  Source="C:\Users\New1\Desktop\ba\WpfApplication1\WpfApplication1\img\gol1s2.gif" Name="image1" Stretch="Fill" />
        <Button Height="23" Margin="100,0,96,48" Name="btnSelect" VerticalAlignment="Bottom" Click="btnSelect_Click">Select</Button>
    </grid>
</window>


代码后面的页面是


page behind code is

string a=@"C:\Users\New1\Desktop\ba\WpfApplication1\WpfApplication1\img\gol1s2.gif";
            if(image1=a)
            {
                Window2 r=new Window2();
                r.Show();
            }
            else
            {
                 Window3 rr=new Window3();
                rr.Show();
            }



在此编码中,我遇到错误了




In this coding i am getting error is


Cannot implicitly convert type ''string'' to ''System.Windows.Controls.Image''



先生,我只收到该错误



Sir i am getting that error only

推荐答案

有一个名为BaseUriImage受保护属性,因此您可以做一点修改,并创建一个新类,继承Image类并具有返回base.BaseUri.AbsolutePath的函数.然后调用此函数以检查图像是否指向该路径.

但是,我认为可能有一个我不知道的更好的解决方案.



现在我有了一个:)

我正在使用引用URI的Image.Source属性,因此我必须声明字符串以检查是否相等,如下所示:
There is a protected property of Image called BaseUri so you could do a little hack and create a new class that inherits the Image class and has got a function returning base.BaseUri.AbsolutePath. Then call this function to check wether the image refers to the path or not.

However, I think there could be a better solution I don''t know yet.



Now I''ve got one:)

I''m using the Image.Source property that refers to a URI so I have to declare the string to check equality as follows:
string uri = "C:/Users/New1/Desktop/ba/WpfApplication1/WpfApplication1/img/gol1s2.gif";


这是必需的,因为在实例化Image对象(您仍可以使用硬盘驱动器上的绝对路径或相对路径进行实例化)之后,对图像文件的引用将另存为URI.

最后检查是否已加载正确的图像:


This is necessary because after instantiating the Image object (you can still use an absolute or relative path on your hard-disk-drive for instantiating) the reference to the Image-File is saved as URI.

Finally check if the correct image has been loaded:

if (image1.Source.ToString().Contains(uri))
{
    // Do something
}


Apfelmuus已经关闭,但是问题出在URI中.

这是解决问题的一种方法,但可能不是一种优雅的方法.
Apfelmuus was close, but the problem is in the URI.

Here is one way of solving it but it may not be an elegant one.
String orgFile = @"C:\Documents and Settings\aUser\My Documents\My Pictures\Winter.jpg";
String orf = orgFile.Replace("\\", "/");
ImageSource imgSource = image1.Source;
String str = imgSource.ToString();
String fileName = str.Remove(0, 8);
if (fileName.Equals(orf))
{
    MessageBox.Show("Equal");
}



我遇到的几个问题是:

1)String str返回源代码:"file:///C:\Documents and Settings\aUser\My Documents\My Pictures\Winter.jpg".它的Pack URI形式.
解决方案:所以我删除了前8个字母.

2)接下来,它将无法与作为资源存储在应用程序中的图像一起使用,因为源将返回如下所示的压缩包uri:"pack://application:,,,/WpfApplication1;component/Winter.jpg".
如果此解决方案不适合您,则可以考虑Apfelmuus提供的第一个解决方案.



A few problems that I encountered was that :

1) the String str returns the source like this : "file:///C:\Documents and Settings\aUser\My Documents\My Pictures\Winter.jpg". Its of Pack URI form.
Solution : so I removed the first 8 letters.

2) next was that it won''t work with Images stored as resources in your application as the source will return a pack uri like this : "pack://application:,,,/WpfApplication1;component/Winter.jpg".

If this solution doesn''t suit you, then the first solution provided by Apfelmuus can be considered.


这篇关于我们如何匹配WPF中图像控件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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