shawn
Topic Author
Posts: 13
Joined: 14 Mar 2019, 10:05

TypeConverter doesn't work in unity

19 Apr 2019, 09:24

For every time I refer a ResourceDictionary in multiple places. Noesis will create multiple instance,although these ResourceDictionary are same. so I write a class inherit from ResourceDictionary named "SharableResdic",so that i can specify a TypeConverter to it
    [TypeConverter(typeof(SharableResdicConverter))]
    public class SharableResdic : ResourceDictionary
    {
        public int RefNum { get; private set; }
        internal SharableResdic Retain(){
            RefNum++;
            return this;
        }

        internal int Release(){
            return --RefNum;
        }
    }
    public class SharableResdicConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType){return sourceType == typeof(string);}
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
            if (value is string){
                return SharableResdicManager.Instance.Get(value as string);
            } return null;
        }
    }
<ex:SharableResdic>/Home/Views/LoginMain/LoginRes.xaml</ex:SharableResdic>
it worked in vs blend,but didn't work in unity

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: TypeConverter doesn't work in unity

22 Apr 2019, 13:12

Hi,

Unfortunately custom TypeConverters are not supported in NoesisGUI C# API yet.
Could you please report it in our bugtracker to include them in future releases?

In the meantime I came up with another way to accomplish what you want:
namespace Testing
{
    public class SharedDictionary : ResourceDictionary
    {
        private string _sharedSource;
        public string SharedSource
        {
            get { return _sharedSource; }
            set
            {
                _sharedSource = value;

                // remove old shared dictionary
                if (_sharedDict != null)
                {
                    MergedDictionaries.Remove(_sharedDict);
                    _sharedDict = null;
                }

                // add new shared dictionary
                if (!string.IsNullOrEmpty(_sharedSource))
                {
                    _sharedDict = SharedDictionaryManager.Instance.Get(_sharedSource);
                    MergedDictionaries.Add(_sharedDict);
                }
            }
        }

        private ResourceDictionary _sharedDict;
    }
}
This dictionary can be used many times and it will be loaded only once:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:Testing"
  xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions">
    <noesis:Xaml.Dependencies>
        <noesis:Dependency Source="XAML/MySharedResources.xaml"/>
    </noesis:Xaml.Dependencies>
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <local:SharedDictionary SharedSource="Assets/Test/XAML/MySharedResources.xaml"/>
                <local:SharedDictionary SharedSource="Assets/Test/XAML/MySharedResources.xaml"/>
                <local:SharedDictionary SharedSource="Assets/Test/XAML/MySharedResources.xaml"/>
                <local:SharedDictionary SharedSource="Assets/Test/XAML/MySharedResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>
    <Rectangle Width="100" Height="50" Fill="{StaticResource redBrush}"/>
</Grid>
For next 2.2.2 release we fixed a bug related to Uri properties that will allow you to simplify that xaml by exposing the SharedSource property as an Uri (so dependencies are automatically detected):
public System.Uri SharedSource { ... }
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:Testing">
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <local:SharedDictionary SharedSource="XAML/MySharedResources.xaml"/>
                <local:SharedDictionary SharedSource="XAML/MySharedResources.xaml"/>
                <local:SharedDictionary SharedSource="XAML/MySharedResources.xaml"/>
                <local:SharedDictionary SharedSource="XAML/MySharedResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>
    <Rectangle Width="100" Height="50" Fill="{StaticResource redBrush}"/>
</Grid>
Hope this helps.
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: TypeConverter doesn't work in unity

23 Apr 2019, 16:31

Also, you should consider using the global application dictionary to share resources through several xamls.
In Unity you can set that dictionary in the Settings inspector.
 
shawn
Topic Author
Posts: 13
Joined: 14 Mar 2019, 10:05

Re: TypeConverter doesn't work in unity

24 Apr 2019, 05:52

Also, you should consider using the global application dictionary to share resources through several xamls.
In Unity you can set that dictionary in the Settings inspector.
yes,I know. I don't want to load all the ResourceDictionary Xaml files. I want to load it when it is needed and unload it when it is not needed.
 
shawn
Topic Author
Posts: 13
Joined: 14 Mar 2019, 10:05

Re: TypeConverter doesn't work in unity

24 Apr 2019, 05:57

The way that you proposed accomplished my requirment. Thank you. I aso submited a bug here https://www.noesisengine.com/bugs/view.php?id=1454
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: TypeConverter doesn't work in unity

24 Apr 2019, 15:13

Thanks for the report.

Who is online

Users browsing this forum: No registered users and 1 guest