7

[Enhancement] Automatically importing specific resources · Issue #11798 · xamari...

 3 years ago
source link: https://github.com/xamarin/Xamarin.Forms/issues/11798
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Summary

When creating a library, there does not appear to be a way to automagically import the required styles and templates.

API Changes

Not sure what the best way to do this as I can't think of all possible issues this may cause, however if we follow the UWP and WPF pattern, they have a special path in the library: Themes\Generic.xaml which is automatically imported. There are also a few other theme names, but they seem to be for specific styles of Windows.

See more: https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/control-authoring-overview

Intended Use Case

My particular use case is that I want to create a templated control and include a default set of styles for this new control in the library.

Current Implementations

There are currently 2 ways to get the same sort of operation now.

The first is on the user to import the dictionary in App.xaml:

<Application .. xmlns:themes="clr-namespace:SkiaSharp.Extended.Controls.Themes;assembly=SkiaSharp.Extended.Controls" x:Class="SkiaSharpDemo.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <themes:Generic />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

This is "nice" but everyone will forget to do it.

Library

The other way which is "automatic" is done via the control constructor:

public class NewControl : TemplatedView
{
	public NewControl()
	{
		var merged = Application.Current?.Resources?.MergedDictionaries;
		if (merged != null)
		{
			var found = false;
			foreach (var dic in merged)
			{
				if (dic.GetType() == typeof(Themes.Generic))
				{
					found = true;
					break;
				}
			}
			if (!found)
				merged.Add(new Themes.Generic());
		}
	}

This is good in most cases, but obviously is not the greatest thing. The framework should do this all for me.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK