【问题标题】:UWP fluent design in XamarinFormsXamarin Forms 中的 UWP 流畅设计
【发布时间】:2019-01-14 04:30:53
【问题描述】:

我正在创建一个 XamarinFroms 解决方案,并且我想在我的 UWP 应用程序中实现 Fluent Design 或部分它。如您所知,大多数 Fluent Design 构建块都是 ThemeResources。所以我尝试这样做:

if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.UI.Xaml.Media.AcrylicBrush"))
            {
                var brush = Windows.UI.Xaml.Application.Current.Resources["SystemControlAltHighAcrylicWindowBrush"] as AcrylicBrush;
                var tint = brush.TintColor;
                var opacity = brush.TintOpacity;
                var fallbackColor = brush.FallbackColor;
                var source = brush.BackgroundSource;
            }

但是很意外我得到了一个

System.Exception: 'Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))' on the brush creating line of code. 

如果我无法在我的 UWP 项目的代码中检索这些主题资源,我将无法访问任何预定义的画笔,或者我永远无法实现显示样式

【问题讨论】:

    标签: xaml xamarin.forms uwp fluent-design acrylic-material


    【解决方案1】:

    对于 UWP fluent 设计,它仅在 UWP 中可用,您无法直接在 xamarin Forms 中实现它。目前 Xamarin Forms 还没有提供这样的接口。 =因为它是针对 UWP 的特定设计。并且很难抽象出适用于每个平台的统一接口。所以更好的方法是单独使用自定义渲染器来实现。例如,在您的情况下,要使 SystemControlAltHighAcrylicWindowBrush 在 Xamarin.UWP 中工作,您可以自定义 LayoutRenderer。而下面的段代码实现AcrylicWindowBrushStackLayout

    [assembly: ExportRenderer(typeof(StackLayout), typeof(ICustomStackLayoutRenderer))]
    
    namespace CustomStackLayoutRenderer.UWP
    {
        public class ICustomStackLayoutRenderer : LayoutRenderer
        {
    
            protected override void OnElementChanged(ElementChangedEventArgs<Layout> e)
            {
                base.OnElementChanged(e);
    
            }
            protected override void UpdateBackgroundColor()
            {
                base.UpdateBackgroundColor();
                if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.UI.Xaml.Media.AcrylicBrush"))
                {
                    var brush = Windows.UI.Xaml.Application.Current.Resources["SystemControlAltHighAcrylicWindowBrush"] as AcrylicBrush;
                    var tint = brush.TintColor;
                    var opacity = brush.TintOpacity;
                    var fallbackColor = brush.FallbackColor;
                    var source = brush.BackgroundSource;
                    this.Background = brush;
                }
    
            }
        }
    }
    

    【讨论】:

    • 实际上我从自定义渲染器中提取了提供的代码。当然,为了通过 XF 在 UWP 中实现 Fluent Design,需要使用自定义渲染器或效果。问题是关于我尝试访问 ThemeResource 时遇到的错误
    • 同样的代码在我身边工作。在其他电脑上测试过吗?
    • 现在我没有得到那个例外。如果你能解释我为什么得到它,那将非常有帮助
    • emm,我猜可能是编译器没有加载资源导致这个问题。
    • 这对我很有帮助。非常感谢
    猜你喜欢
    • 2018-02-01
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多