Equaliy operator == WPF和UWP的行为不同 [英] Equaliy operator == behave differ for WPF and UWP

查看:113
本文介绍了Equaliy operator == WPF和UWP的行为不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Equaliy Operator ==不在Universal Windows中工作



我尝试过:



我试图使用相等运算符将两个有价值对象作为keyvaluepair进行比较。条件在WPF中设置为true,在Universal Windows中为false。但在WPF和UWP中使用equal()获取true。对于WPF和UWP平台,相等运算符的行为是否不同?任何人都可以建议你的想法?



Equaliy Operator == is not working in Universal Windows

What I have tried:

Am trying to compare two objects of value as keyvaluepair using equality operator . The condition was set true in WPF and get false in Universal Windows.But using equal() get true in both WPF and UWP. Is the equality operator behave differ for WPF and UWP platform? can anyone please suggest you ideas?

public partial class MainWindow : Window
    {
        public List<KeyValuePair<int, double>> Collection { get; set; }
        public MainWindow()
        {           
            InitializeComponent();                      
            Collection = new List<KeyValuePair<int, double>>();
            Collection.Add(new KeyValuePair<int, double>(1, 18));
            Collection.Add(new KeyValuePair<int, double>(2, 36));
            Collection.Add(new KeyValuePair<int, double>(3, 24));
            Collection.Add(new KeyValuePair<int, double>(4, 42));
            Collection.Add(new KeyValuePair<int, double>(5, 30));
            List<object> actualdata = new List<object>();
            for (int c = 0; c < Collection.Count; c++)
            {
                actualdata.Add(Collection[c]);
            }           
            bool flag = false;
            Model modal = new Model();
            modal.Item = actualdata[1];
            object ob = actualdata[1];
            if (modal.Item == ob)
                flag = true;
            else if (ob.Equals(ob))
                flag = true;
            else
                flag = false;

        }
    }
    public class Model : DependencyObject, INotifyPropertyChanged
    {
        public int xval { get; set; }
        public object obj { get; set; }

        public object Item
        {
            get { return (object) GetValue(ItemProperty); }
            set { SetValue(ItemProperty, value); }
        }
        public static readonly DependencyProperty ItemProperty =
            DependencyProperty.Register("Item", typeof(object), typeof(Model), new PropertyMetadata(null, OnPropertyChanged));
        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as Model).onPropertyChanged("Item");
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void onPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs("Item"));
            }
        }
    }



这是我的要求。尽管值相同,但equlity运算符在WPF中返回true,在UWP中返回false。在这里,我将类型对象的变量与object类型的依赖属性进行了比较。我感到困惑的是依赖属性引用UWP中的任何引用?


This is my requirement. eventhough the values are same the equlity Operator return true in WPF , false in UWP. Here, i have compared variable of type object with dependency property of type object. i get confused is the dependency property refer any reference in UWP?

推荐答案

大家好,



这个下面的代码是我的实际要求。

Hi All,

This below code is my actual requirement.
public partial class MainWindow : Window
   {
       public List<KeyValuePair<int, double>> Collection { get; set; }
       public MainWindow()
       {
           InitializeComponent();
           Collection = new List<KeyValuePair<int, double>>();
           Collection.Add(new KeyValuePair<int, double>(1, 18));
           Collection.Add(new KeyValuePair<int, double>(2, 36));
           Collection.Add(new KeyValuePair<int, double>(3, 24));
           Collection.Add(new KeyValuePair<int, double>(4, 42));
           Collection.Add(new KeyValuePair<int, double>(5, 30));
           List<object> actualdata = new List<object>();
           for (int c = 0; c < Collection.Count; c++)
           {
               actualdata.Add(Collection[c]);
           }
           bool flag = false;
           Model modal = new Model();
           modal.Item = actualdata[1];
           object ob = actualdata[1];
           if (modal.Item == ob)
               flag = true;
           else if (ob.Equals(ob))
               flag = true;
           else
               flag = false;

       }
   }
   public class Model : DependencyObject, INotifyPropertyChanged
   {
       public int xval { get; set; }
       public object obj { get; set; }

       public object Item
       {
           get { return (object) GetValue(ItemProperty); }
           set { SetValue(ItemProperty, value); }
       }
       public static readonly DependencyProperty ItemProperty =
           DependencyProperty.Register("Item", typeof(object), typeof(Model), new PropertyMetadata(null, OnPropertyChanged));
       private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       {
           (d as Model).onPropertyChanged("Item");
       }

       public event PropertyChangedEventHandler PropertyChanged;
       protected virtual void onPropertyChanged(string name)
       {
           PropertyChangedEventHandler handler = PropertyChanged;
           if (handler != null)
           {
               handler(this, new PropertyChangedEventArgs("Item"));
           }
       }
   }









当我尝试运行示例时,等于运算符在WPF中返回true并在UWP中返回false。任何人都可以分享你的想法吗?





When am trying to run the sample , the equal operator return true in WPF and return false in UWP. Can anyone please share you ideas?


这篇关于Equaliy operator == WPF和UWP的行为不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆