TypeConverter doesn't work in unity
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
it worked in vs blend,but didn't work in unity
Code: Select all
[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;
}
}
Code: Select all
<ex:SharableResdic>/Home/Views/LoginMain/LoginRes.xaml</ex:SharableResdic>
-
-
sfernandez
Site Admin
- Posts: 3250
- Joined:
Re: TypeConverter doesn't work in unity
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:
This dictionary can be used many times and it will be loaded only once:
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):
Hope this helps.
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:
Code: Select all
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;
}
}
Code: Select all
<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>
Code: Select all
public System.Uri SharedSource { ... }
Code: Select all
<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>
-
-
sfernandez
Site Admin
- Posts: 3250
- Joined:
Re: TypeConverter doesn't work in unity
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.
In Unity you can set that dictionary in the Settings inspector.
Re: TypeConverter doesn't work in unity
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.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.
Re: TypeConverter doesn't work in unity
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
-
-
sfernandez
Site Admin
- Posts: 3250
- Joined:
Re: TypeConverter doesn't work in unity
Thanks for the report.
Who is online
Users browsing this forum: Ahrefs [Bot] and 0 guests