简体   繁体   English

如何在WP 7中使用gif动画图像

[英]How to use gif animated image in WP 7

I have seen this post: Display GIF in a WP7 application with Silverlight 我看过这篇文章: 使用Silverlight在WP7应用程序中显示GIF

But in my case? 但就我而言? for animating I am using a popup. 动画我正在使用一个弹出窗口。 So when application starts it shows a popup for 5 seconds. 因此,当应用程序启动时,它会弹出5秒钟。 In this popup I want to show some .gif image, but it doesn't work. 在此弹出窗口中,我想显示一些.gif图像,但是它不起作用。

Here is the code which I implement: 这是我实现的代码:

    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>();

        }

And xaml code is: 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. 但是结果是它不起作用,它显示了一个空白的背景。

Updated: ImageTools.IO.Decoders.AddDecoder(); 更新:ImageTools.IO.Decoders.AddDecoder(); ImageSource = new Uri(" http://a3.twimg.com/profile_images/1136683647/hisoka_normal.gif ", UriKind.Absolute); ImageSource =新的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! 终于工作了...谈论串谋不利于您的事情...您需要首先解决所有这些问题!

(note there is a following problem with only the first 2 frames being animated but that is for another question) : (请注意,只有前两个帧具有动画效果,这是一个以下问题,但这是另一个问题)

Part 6 (getting sleepy now) 第6部分(现在要昏昏欲睡)

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 "/". 最后, ImageTools.Controls.ImageConverter不支持以“ /”开头的相对图像URL,因此您需要使用不带前导“ /”的相对URL。 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;

Part 5 第5部分

You need to set the binding DataContext somewhere. 您需要在某个地方设置绑定DataContext。

You do not connect the XAML page DataContext to the code behind object. 您没有将XAML页面DataContext连接到对象后面的代码。 I could not see where you had done this. 我看不到你在哪里做的。 A very simple/quick way is to set this.DataContext = this; 一种非常简单/快速的方法是设置this.DataContext = this; in the page's constructor. 在页面的构造函数中。

Part 4 第4部分

You can only bind to public properties! 您只能绑定到公共财产!

Your ImageSource property is currently protected. 您的ImageSource属性当前受保护。 Change it to Public 更改为Public

    public Uri ImageSource
    {
        get;
        set;
    }

Part 3 第三部分

I also note your ImageSource property is not an INotifyPropertyChange type property. 我还要注意,您的ImageSource属性不是INotifyPropertyChange类型的属性。 So setting it after InitializeComponent will not work. 因此,在InitializeComponent之后进行设置将不起作用。

Try it this way round (or change it to use a notify property): 尝试这种方式(或更改为使用notify属性):

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

Part 2 (actually not support by ImageTools.Controls.ImageConverter) 第2部分(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. 根据评论,您还需要将图像存储在自己的网站上,并以适当的URI格式引用它们。

If you put your files in a folder called images under ClientBin you use this format: 如果将文件放在ClientBin下的图像文件夹中,则使用以下格式:

"/images/imagename.jpg"

This is the best option as the images also use Browser caching! 这是最佳选择,因为图像也使用浏览器缓存!

For your example it would be like this: 以您的示例为例:

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

and put the example file in your client bin folder under images. 并将示例文件放在图像下的客户bin文件夹中。

If you do not use the leading "/" Silverlight assumes the files are resources in the current module instead eg 如果不使用前导“ /”,Silverlight会假定文件是当前模块中的资源,例如

"images/imagename.jpg"

Part 1 第1部分

This is actually a copyright issue, to stop people deep-linking files from other people's sites without permission. 这实际上是一个版权问题,目的是阻止人们未经许可而深深链接他人站点中的文件。

The Wikimedia.org site does not have any cross domain access files eg: Wikimedia.org网站没有任何跨域访问文件,例如:

... presumably as they do not want others to use the files they host there outside of their own website. ...大概是因为他们不希望其他人使用他们在自己的网站之外托管的文件。

That means Silverlight will not allow access to files on those sites as it is a good Internet citizen . 这意味着Silverlight是良好的Internet公民 ,因此将不允许访问这些站点上的文件。 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应用程序所在的站点)上,那么它根本不需要任何跨域访问文件。

Side note: If you do ever need a cross domain file on a website, for use by Silverlight, use a crossdomainpolicy.xml as the other one is not as useful (designed for older flash use) 旁注:如果您确实需要网站上的跨域文件以供Silverlight使用,请使用crossdomainpolicy.xml,因为另一个则不那么有用(设计用于较早的Flash使用)

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

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