1

I'm creating a XamarinFroms solution and i want to implement Fluent Design or pats of it in my UWP app. As you may know, Most of Fluent Design building blocks are ThemeResources. so i tried to do:

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;
            }

but unfortionalty i get a

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

I won't be able to access any predefined Brushes or i can never implement reveal styles if i'm not able to retrieve those theme resources in code in my UWP project

Adham Ali
  • 81
  • 8

1 Answers1

0

For UWP fluent design, it is only available in UWP, you could not implement it within xamarin Forms directly. Currently Xamarin Forms has not provided such interface. =Because it is specific design for UWP. And it is hardly to abstract a unified interface that available for each platform. So the better way is implement this with Custom Renderer separately. For example, as in your case, to make SystemControlAltHighAcrylicWindowBrush work in Xamarin.UWP, you could custom LayoutRenderer. And the following segment code realize AcrylicWindowBrush for StackLayout.

[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;
            }

        }
    }
}

enter image description here

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • Actually i extracted the provided code from a custom renderer. Of course in order to implement Fluent Design in UWP through XF is using a custom renderer or an effect. The question was about the error i get when i attempt to access the ThemeResource – Adham Ali Aug 08 '18 at 05:47
  • The same code woks in my side. Have tested in other computer? – Nico Zhu Aug 08 '18 at 05:51
  • Now i don't get that exception. It would be very helpful if you explained why i was getting it – Adham Ali Aug 08 '18 at 05:57
  • emm, I guess it may be that the compiler is not loading resources causing this issue. – Nico Zhu Aug 08 '18 at 06:01
  • That really helped me. Thanks a lot – Adham Ali Aug 08 '18 at 06:19
  • @NicoZhu-MSFT I'm developing a UWP app and having the same issue in the UWP app at runtime, whenever I try to access `App.Resources`. It says: ": 'Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))'". P.S. there are no resources defined at all, but I get this error anyway. – Shimmy Weitzhandler Jun 22 '19 at 21:14