Exemplo n.º 1
0
        private object GetStaticResourceKeyValue(StaticResource staticResource, IServiceProvider serviceProvider)
        { 
            System.Xaml.XamlReader reader = staticResource.ResourceNodeList.GetReader();
            XamlType xamlTypeStaticResourceExtension = reader.SchemaContext.GetXamlType(typeof(StaticResourceExtension));
            XamlMember xamlMemberResourceKey = xamlTypeStaticResourceExtension.GetMember("ResourceKey");
            reader.Read(); 
            if (reader.NodeType == Xaml.XamlNodeType.StartObject && reader.Type == xamlTypeStaticResourceExtension)
            { 
                reader.Read(); 
                // Skip Members that aren't _PositionalParameters or ResourceKey
                while (reader.NodeType == Xaml.XamlNodeType.StartMember && 
                    (reader.Member != XamlLanguage.PositionalParameters && reader.Member != xamlMemberResourceKey))
                {
                    reader.Skip();
                } 

                // Process the Member Value of _PositionParameters or ResourceKey 
                if (reader.NodeType == Xaml.XamlNodeType.StartMember) 
                {
                    object value = null; 
                    reader.Read();
                    if (reader.NodeType == Xaml.XamlNodeType.StartObject)
                    {
                        System.Xaml.XamlReader subReader = reader.ReadSubtree(); 

                        value = EvaluateMarkupExtensionNodeList(subReader, serviceProvider); 
                    } 
                    else if (reader.NodeType == Xaml.XamlNodeType.Value)
                    { 
                        value = reader.Value;
                    }
                    return value;
                } 
            }
            return null; 
        } 
        /// <inheritdoc /> 
        protected virtual async void ShowIcon(Microsoft.Media.Advertising.Icon icon, StaticResource staticResource, CancellationTokenSource cts)
        {
            if (icon.Offset.HasValue)
            {
                try
                {
#if SILVERLIGHT && !WINDOWS_PHONE || WINDOWS_PHONE7
                    await TaskEx.Delay((int)icon.Offset.Value.TotalMilliseconds, cts.Token);
#else
                    await Task.Delay((int)icon.Offset.Value.TotalMilliseconds, cts.Token);
#endif
                }
                catch (OperationCanceledException) { /* swallow */ }
            }

            if (!cts.IsCancellationRequested)
            {
                var iconHost = new HyperlinkButton();
                iconHost.NavigateUri = icon.ClickThrough;

                double topMargin = 0;
                double leftMargin = 0;
                if (icon.Width.HasValue)
                {
                    iconHost.Width = icon.Width.Value;
                }
                if (icon.Height.HasValue)
                {
                    iconHost.Height = icon.Height.Value;
                }
                switch (icon.XPosition)
                {
                    case "left":
                        iconHost.HorizontalAlignment = HorizontalAlignment.Left;
                        break;
                    case "right":
                        iconHost.HorizontalAlignment = HorizontalAlignment.Right;
                        break;
                    default:
                        iconHost.HorizontalAlignment = HorizontalAlignment.Left;
                        int xPositionValue;
                        if (int.TryParse(icon.XPosition, out xPositionValue))
                        {
                            leftMargin = xPositionValue;
                        }
                        break;
                }
                switch (icon.YPosition)
                {
                    case "top":
                        iconHost.VerticalAlignment = VerticalAlignment.Top;
                        break;
                    case "bottom":
                        iconHost.VerticalAlignment = VerticalAlignment.Bottom;
                        break;
                    default:
                        iconHost.VerticalAlignment = VerticalAlignment.Top;
                        int yPositionValue;
                        if (int.TryParse(icon.YPosition, out yPositionValue))
                        {
                            topMargin = yPositionValue;
                        }
                        break;
                }
                iconHost.Margin = new Thickness(leftMargin, topMargin, 0, 0);

                var iconElement = new Image();
                iconElement.Stretch = Stretch.Fill;
                iconElement.Source = new BitmapImage(staticResource.Value);
                iconHost.Content = iconElement;

                //TODO: avoid visually overlaping icons

                iconHost.Tag = icon;
                iconElement.Tag = icon;

                iconHost.Click += iconHost_Click;
                iconElement.ImageOpened += iconElement_ImageOpened;
                AdContainer.Children.Add(iconHost);

                try
                {
                    if (icon.Duration.HasValue)
                    {
#if SILVERLIGHT && !WINDOWS_PHONE || WINDOWS_PHONE7
                        await TaskEx.Delay((int)icon.Duration.Value.TotalMilliseconds, cts.Token);
#else
                        await Task.Delay((int)icon.Duration.Value.TotalMilliseconds, cts.Token);
#endif
                    }
                    else
                    {
                        await cts.Token.AsTask();
                    }
                }
                catch (OperationCanceledException) { /* swallow */ }
                finally
                {
                    AdContainer.Children.Remove(iconHost);
                    iconHost.Click -= iconHost_Click;
                    iconElement.ImageOpened -= iconElement_ImageOpened;
                }
            }
        }