【问题标题】:Applying a background for the control in an Effect in XamarinForms在 Xamarin Forms 的效果中为控件应用背景
【发布时间】:2018-08-10 13:30:03
【问题描述】:

我正在尝试为附加效果的控件应用背景。

首先,我为 UWP 中具有填充或背景属性的控件应用背景效果。 但问题是很多控件在 UWP 中呈现给 FrameworkElement。并且 FrameworkElement 没有 Background 属性。 在 VisualElementRenderer Xamarin 表单中,解决了在内容后面应用背景层的问题,但这是一种效果。 有什么方法可以为附加的效果应用背景吗? 我正在应用 AcrylicBrush 所以它必须直接分配给 Control。

我之前写的代码:

try
{
   Control.GetType().GetProperty("Background").SetValue(Control, brush);
}
catch
{
   Control.GetType().GetProperty("Fill").SetValue(Control, brush);
}

【问题讨论】:

    标签: xamarin.forms uwp background effects acrylic-material


    【解决方案1】:

    我正在应用 AcrylicBrush 所以它必须直接分配给控件。

    您可以使用compositor 将Acrylic 应用于SpriteVisual。在我们有Acrylic API 之前,我们使用Win2DUIElement 生成GaussianBlurEffect。获取 UIElement 的Compositor,并使用这个CompositorCreateSpriteVisual。然后将GaussianBlurEffect 设置为hostSprite.Brush,如下所示。

    Compositor _compositor;
    SpriteVisual _hostSprite;
    
    private void applyAcrylicAccent(Panel panel)
    {
        _compositor = ElementCompositionPreview.GetElementVisual(panel).Compositor;
        _hostSprite = _compositor.CreateSpriteVisual();
        _hostSprite.Size = new Vector2((float)panel.ActualWidth, (float)panel.ActualHeight);
    
        var backdrop = _compositor.CreateHostBackdropBrush();
    
        // Use a Win2D blur affect applied to a CompositionBackdropBrush.
        var graphicsEffect = new GaussianBlurEffect
        {
            Name = "Blur",
            BlurAmount = 100f,
            Source = new CompositionEffectSourceParameter("backdrop")
        };
    
        var effectFactory = Window.Current.Compositor.CreateEffectFactory(graphicsEffect, new[] { "Blur.BlurAmount" });
        var effectBrush = effectFactory.CreateBrush();
    
        effectBrush.SetSourceParameter("backdrop", backdrop);
        _hostSprite.Brush = effectBrush;
    
        ElementCompositionPreview.SetElementChildVisual(panel, _hostSprite);
    }
    

    并使用 applyAcrylicAccent(RootLayout) 调用它。您还需要处理SizeChanged 事件:

    private void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        if (_hostSprite != null)
            _hostSprite.Size = e.NewSize.ToVector2();
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 2020-03-16
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    相关资源
    最近更新 更多