Reading OperatorValueFilterDescriptorBase.UnsetValue

4 posts, 0 answers
  1. Pawel
    Pawel avatar
    39 posts
    Member since:
    Apr 2014

    Posted 02 Dec 2014 Link to this post

    I came across a strange case when I was writing code for saving FilterDescriptors settings for RadDataFilter. The default value for string filter editor is OperatorValueFilterDescriptorBase.UnsetValue, the FilterDescriptor.Value is equal to OperatorValueFilterDescriptorBase.UnsetValue (FilterDescriptor .Value == OperatorValueFilterDescriptorBase.UnsetValue). When I save and read this value from isolated storage is not equal to OperatorValueFilterDescriptorBase.UnsetValue. See example bellow.

    The following example is completely useless but it shows error.

    MainWindow.xaml:
    <Window x:Class="TestTelerikWpfApp.MainWindow"
            xmlns:local="clr-namespace:TestTelerikWpfApp"
            Title="MainWindow" Height="300">
        <Window.Resources>
        </Window.Resources>
        <StackPanel>
            <TextBox telerik:PersistenceManager.StorageId="textBox" />
            <StackPanel Orientation="Vertical">
                <telerik:RadButton Content="Save" Click="SaveButton_Click" />
                <telerik:RadButton Content="Read" Click="ReadButton_Click" />
            </StackPanel>
        </StackPanel>
    </Window>

    MainWindow.xaml.cs
    namespace TestTelerikWpfApp
    {
        using System.Windows;
        using System.Windows.Controls;
        using Telerik.Windows.Persistence.Services;
        using Telerik.Windows.Persistence.Storage;
     
        public partial class MainWindow : Window
        {
            private IsolatedStorageProvider isoProvider;
     
            public MainWindow()
            {
                InitializeComponent();
     
                ServiceProvider.RegisterPersistenceProvider<ICustomPropertyProvider>(typeof(TextBox), new UnsetValuePropertyProvider());
     
                isoProvider = new IsolatedStorageProvider();
            }
     
            private void SaveButton_Click(object sender, RoutedEventArgs e)
            {
                isoProvider.SaveToStorage();
            }
     
            private void ReadButton_Click(object sender, RoutedEventArgs e)
            {
                isoProvider.LoadFromStorage();
            }
        }
    }

    For one TextBox I save or read data to isolated storage.

    UnsetValuePropertyProvider.cs:
    namespace TestTelerikWpfApp
    {
        using Telerik.Windows.Data;
        using Telerik.Windows.Persistence.Services;
     
        class UnsetValuePropertyProvider : ICustomPropertyProvider
        {
            public CustomPropertyInfo[] GetCustomProperties()
            {
                return new CustomPropertyInfo[]
                {
                    new CustomPropertyInfo("UnsetValue", typeof(UnsetValueProxy))
                };
            }
     
            public void InitializeObject(object context)
            {
            }
     
            public object InitializeValue(CustomPropertyInfo customPropertyInfo, object context)
            {
                return null;
            }
     
            public object ProvideValue(CustomPropertyInfo customPropertyInfo, object context)
            {
                return new UnsetValueProxy() { Value = OperatorValueFilterDescriptorBase.UnsetValue };
            }
     
            public void RestoreValue(CustomPropertyInfo customPropertyInfo, object context, object value)
            {
                var unsetValueProxy = value as UnsetValueProxy;
                var isUnsetValueSame = unsetValueProxy.Value == OperatorValueFilterDescriptorBase.UnsetValue; // isUnsetValueSame = false !!!
            }
        }
    }

    UnsetValueProxy.cs:
    namespace TestTelerikWpfApp
    {
        class UnsetValueProxy
        {
            public object Value { get; set; }
        }
    }

    In ProvideValue method I pass to proxy object OperatorValueFilterDescriptorBase.UnsetValue value and in RestoreValue unsetValueProxy.Value is not equal to OperatorValueFilterDescriptorBase.UnsetValue and it should be. See also locals tab in debug mode (example.png).
  2. Pawel
    Pawel avatar
    39 posts
    Member since:
    Apr 2014

    Posted 02 Dec 2014 Link to this post

    I add that the problem can be easily solved by replace OperatorValueFilterDescriptorBase.UnsetValue value with null value in saving to isolated storage and in reading from isolated storage replace null value with OperatorValueFilterDescriptorBase.UnsetValue value.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1183 posts

    Posted 04 Dec 2014 Link to this post

    Hi Pawel,

    Thank you for contacting us on that matter. I created a sample project with the snippets you provide and I confirm that the issue exists as you describe it. However, when working with the RadPersistenceFramework you should keep in mind that it persists primitive types, UIElements and collections of UIElements. What this means for your application is that you should return primitive types in the ProvideValue() method. Furthermore, based on those types you should create the object that you need to restore in the RestoreValue() method.

    In your particular implementation, in the ProvideValue() method you can return the string "UnsetValue" instead of OperatorValueFilterDescriptorBase.UnsetValue. 

    public object ProvideValue(CustomPropertyInfo customPropertyInfo, object context)
    {
        return new UnsetValueProxy() { Value = "UnsetValue" };
        //return new UnsetValueProxy() { Value = OperatorValueFilterDescriptorBase.UnsetValue };
    }
    Please give this approach a try and let me know if it works in your scenario.

    Regards,
    Pavel R. Pavlov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  5. Pawel
    Pawel avatar
    39 posts
    Member since:
    Apr 2014

    Posted 04 Dec 2014 in reply to Pavel R. Pavlov Link to this post

    Of course it will work. Aproach with string is similar to aproach with null value. For me, may be one or the other. Given what you said problem I described is not an error.
Back to Top