如何在 WP 7 中使用 gif 动画图像 [英] How to use gif animated image in WP 7

查看:15
本文介绍了如何在 WP 7 中使用 gif 动画图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这篇文章:在 WP7 应用程序中显示 GIF银光

但就我而言?动画我使用弹出窗口.因此,当应用程序启动时,它会显示一个 5 秒的弹出窗口.在这个弹出窗口中,我想显示一些 .gif 图像,但它不起作用.

But in my case? for animating I am using a popup. So when application starts it shows a popup for 5 seconds. In this popup I want to show some .gif image, but it doesn't work.

这是我实现的代码:

    public partial class AnimatedSplashScreen : UserControl
    {
        protected Uri ImageSource
        {
            get;
            set;
        }
        public AnimatedSplashScreen()
        {
            InitializeComponent();
           ImageSource =
                new Uri(
                    "http://upload.wikimedia.org/wikipedia/commons/thumb/3/36/Sunflower_as_GIF.gif/200px-Sunflower_as_GIF.gif",
                    UriKind.Absolute);
            ImageTools.IO.Decoders.AddDecoder<GifDecoder>();

        }

xaml 代码是:

<UserControl.Resources>

        <imagetools:ImageConverter x:Key="ImageConverter" />
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot"
          Width="480"
          Height="800"
          Background="White">
        <imagetools:AnimatedImage Source="{Binding ImageSource, Converter={StaticResource ImageConverter}}"  />

但结果它不起作用,它显示一个空的背景.

But in result it does't work, it shows an empty background.

更新:ImageTools.IO.Decoders.AddDecoder();ImageSource = new Uri("http://a3.twimg.com/profile_images/1136683647/hisoka_normal.gif", UriKind.Absolute);还是不行

Updated: ImageTools.IO.Decoders.AddDecoder(); ImageSource = new Uri("http://a3.twimg.com/profile_images/1136683647/hisoka_normal.gif", UriKind.Absolute); It still doesn't work

推荐答案

终于开始工作了... 谈论密谋反对您的事件... 您需要先解决所有这些问题!

Finally working... Talk about events conspiring against you... You need to fix all these things first!

(请注意,只有前 2 帧动画存在以下问题,但这是另一个问题):

最后,ImageTools.Controls.ImageConverter 不支持以/"开头的相对图像 URL,因此您需要使用不带前导/"的相对 URL.我发现一旦解决了所有其他问题,我就会收到不受支持的路径异常.

Lastly relative image URLs starting with "/" are not supported by the ImageTools.Controls.ImageConverter, so you need to use a relative URL without the leading "/". I found that once every other problem was fixed I was getting an unsupported exception with the path.

        ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
        InitializeComponent();
        this.ImageSource = new Uri("layer1.gif", UriKind.Relative);
        this.DataContext = this;

第 5 部分

您需要在某处设置绑定 DataContext.

Part 5

You need to set the binding DataContext somewhere.

您没有将 XAML 页面 DataContext 连接到代码隐藏对象.我看不出你是在哪里做的.一个非常简单/快速的方法是在页面的构造函数中设置 this.DataContext = this;.

You do not connect the XAML page DataContext to the code behind object. I could not see where you had done this. A very simple/quick way is to set this.DataContext = this; in the page's constructor.

您只能绑定到公共属性!

You can only bind to public properties!

您的 ImageSource 属性当前受到保护.将其更改为 Public

Your ImageSource property is currently protected. Change it to Public

    public Uri ImageSource
    {
        get;
        set;
    }

第 3 部分

我还注意到您的 ImageSource 属性不是 INotifyPropertyChange 类型的属性.所以在 InitializeComponent 之后设置是不行的.

Part 3

I also note your ImageSource property is not an INotifyPropertyChange type property. So setting it after InitializeComponent will not work.

以这种方式尝试(或将其更改为使用通知属性):

Try it this way round (or change it to use a notify property):

public AnimatedSplashScreen()
{
   ImageSource =
        new Uri(
            "/200px-Sunflower_as_GIF.gif",
            UriKind.Relative);
    ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
    InitializeComponent();
}

第 2 部分(实际上 ImageTools.Controls.ImageConverter 不支持)

跨域文件显然只是一个问题.根据评论,您还需要将图像存储在自己的网站上,并使用适当的 URI 格式引用它们.

Part 2 (actually not support by ImageTools.Controls.ImageConverter)

The cross domain file was apparently only one problem. Based on the comments you also need to store your images on your own website and reference them with an appropriate URI format.

如果您将文件放在 ClientBin 下名为 images 的文件夹中,则使用以下格式:

If you put your files in a folder called images under ClientBin you use this format:

"/images/imagename.jpg"

这是最好的选择,因为图像也使用浏览器缓存!

对于您的示例,它会是这样的:

For your example it would be like this:

    ImageSource =
                new Uri(
                    "/images/200px-Sunflower_as_GIF.gif",
                    UriKind.Relative);
            ImageTools.IO.Decoders.AddDecoder<GifDecoder>();

并将示例文件放在图像下的客户端 bin 文件夹中.

and put the example file in your client bin folder under images.

如果您不使用前导/",Silverlight 假定文件是当前模块中的资源,例如

If you do not use the leading "/" Silverlight assumes the files are resources in the current module instead e.g.

"images/imagename.jpg"

第 1 部分

这实际上是一个版权问题,以阻止人们在未经许可的情况下从其他人的网站深度链接文件.

Wikimedia.org 站点没有任何跨域访问文件,例如:

The Wikimedia.org site does not have any cross domain access files e.g.:

...大概是因为他们不希望其他人在他们自己的网站之外使用他们在那里托管的文件.

... presumably as they do not want others to use the files they host there outside of their own website.

这意味着 Silverlight 将不允许访问这些站点上的文件,因为它是一个优秀的互联网公民.尝试将文件托管在您自己的站点(您的 Silverlight 应用所在的站点)上,然后它就根本不需要任何跨域访问文件.

That means Silverlight will not allow access to files on those sites as it is a good Internet citizen. Try hosting the files on your own site (where your Silverlight app resides), then it will not need any cross domain access file at all.

旁注:如果您确实需要网站上的跨域文件供 Silverlight 使用,请使用 crossdomainpolicy.xml,因为另一个没有那么有用(专为较旧的 Flash 使用而设计)

这篇关于如何在 WP 7 中使用 gif 动画图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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