示例#1
0
 private void CreateRegistrationForPropertyGrid()
 {
     try
     {
         if (cboRegistrationType.SelectedIndex == 0)
         {
             return;
         }
         RegistrationType = Type.GetType(string.Format(RegistrationTypeFormat,
                                                       cboRegistrationType.Text,
                                                       typeof(NotificationHubDescription).Assembly.FullName));
         if (RegistrationType == null)
         {
             return;
         }
         var propertyInfos = RegistrationType.
                             GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty).
                             Where(p => p.GetSetMethod(false) != null && string.Compare(p.Name, "ExtensionData", StringComparison.InvariantCultureIgnoreCase) != 0);
         IEnumerable <PropertyInfo> enumerable = propertyInfos as IList <PropertyInfo> ?? propertyInfos.ToList();
         RegistrationObject = new CustomObject
         {
             Properties = enumerable.Select(p => new CustomProperty
             {
                 Name = p.Name,
                 Type = p.Name == ChannelUri ?
                        typeof(string) :
                        p.Name == BodyTemplate ?
                        typeof(string) :
                        p.Name == Tags ?
                        typeof(List <string>) :
                        p.Name == MpnsHeaders ||
                        p.Name == WnsHeaders ?
                        typeof(List <NotificationInfo>) :
                        p.Name == Expressions ?
                        typeof(ReadOnlyList <string>) :
                        p.Name == ExpressionLengths ||
                        p.Name == ExpressionStartIndices ?
                        typeof(ReadOnlyList <int>) :
                        p.PropertyType,
                 Editor = p.Name == BodyTemplate ||
                          p.Name == ChannelUri ||
                          p.Name == DeviceToken ||
                          p.Name == GcmRegistrationId ?
                          new CustomTextEditor() as UITypeEditor :
                          p.Name == Expressions ||
                          p.Name == ExpressionLengths ||
                          p.Name == ExpressionStartIndices ?
                          new ReadOnlyEditor() as UITypeEditor :
                          p.Name == Tags ||
                          p.Name == MpnsHeaders ||
                          p.Name == WnsHeaders ?
                          new CustomCollectionEditor() as UITypeEditor :
                          null,
                 IsReadOnly = !p.CanWrite ||
                              p.SetMethod == null ||
                              (p.SetMethod != null && !p.SetMethod.IsPublic)
             }).ToList()
         };
         foreach (var propertyInfo in enumerable)
         {
             if (propertyInfo.PropertyType == typeof(Uri))
             {
                 RegistrationObject[propertyInfo.Name] = null;
             }
             else if (propertyInfo.PropertyType == typeof(CDataMember))
             {
                 RegistrationObject[propertyInfo.Name] = null;
             }
             else if (propertyInfo.PropertyType == typeof(ISet <string>))
             {
                 RegistrationObject[propertyInfo.Name] = new List <TagInfo>();
             }
             else if (propertyInfo.PropertyType == typeof(MpnsHeaderCollection))
             {
                 RegistrationObject[propertyInfo.Name] = new List <NotificationInfo>();
             }
             else if (propertyInfo.PropertyType == typeof(WnsHeaderCollection))
             {
                 RegistrationObject[propertyInfo.Name] = new List <NotificationInfo>();
             }
             else if ((propertyInfo.GetMethod == null ||
                       (propertyInfo.GetMethod != null && !propertyInfo.GetMethod.IsPublic)) &&
                      (propertyInfo.SetMethod == null ||
                       (propertyInfo.SetMethod != null && !propertyInfo.SetMethod.IsPublic)) &&
                      propertyInfo.PropertyType == typeof(List <int>))
             {
                 RegistrationObject[propertyInfo.Name] = new ReadOnlyList <int>();
             }
         }
         registrationPropertyGrid.SelectedObject = RegistrationObject;
     }
     catch (Exception ex)
     {
         MainForm.SingletonMainForm.HandleException(ex);
     }
 }
 private void CreateRegistrationForPropertyGrid()
 {
     try
     {
         if (cboRegistrationType.SelectedIndex == 0)
         {
             return;
         }
         RegistrationType = Type.GetType(string.Format(RegistrationTypeFormat, 
                                         cboRegistrationType.Text, 
                                         typeof(NotificationHubDescription).Assembly.FullName));
         if (RegistrationType == null)
         {
             return;
         }
         var propertyInfos = RegistrationType.
                             GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty).
                             Where(p=>p.GetSetMethod(false) != null && string.Compare(p.Name, "ExtensionData", StringComparison.InvariantCultureIgnoreCase) != 0);
         IEnumerable<PropertyInfo> enumerable = propertyInfos as IList<PropertyInfo> ?? propertyInfos.ToList();
         RegistrationObject = new CustomObject
         {
             Properties = enumerable.Select(p => new CustomProperty
             {
                 Name = p.Name,
                 Type = p.Name == ChannelUri ?
                        typeof(string) :
                        p.Name == BodyTemplate ?
                        typeof(string) :
                        p.Name == Tags ?
                        typeof(List<string>) :
                        p.Name == MpnsHeaders ||
                        p.Name == WnsHeaders ?
                        typeof(List<NotificationInfo>) :
                        p.Name == Expressions ?
                        typeof(ReadOnlyList<string>) :
                        p.Name == ExpressionLengths ||
                        p.Name == ExpressionStartIndices ?
                        typeof(ReadOnlyList<int>) :
                        p.PropertyType,
                 Editor = p.Name == BodyTemplate ||
                         p.Name == ChannelUri ||
                         p.Name == DeviceToken ||
                         p.Name == GcmRegistrationId ?
                         new CustomTextEditor() as UITypeEditor :
                         p.Name == Expressions ||
                         p.Name == ExpressionLengths ||
                         p.Name == ExpressionStartIndices ?
                         new ReadOnlyEditor() as UITypeEditor :
                         p.Name == Tags ||
                         p.Name == MpnsHeaders ||
                         p.Name == WnsHeaders ?
                         new CustomCollectionEditor() as UITypeEditor :
                         null,
                 IsReadOnly = !p.CanWrite ||
                              p.SetMethod == null ||
                             (p.SetMethod != null && !p.SetMethod.IsPublic)
             }).ToList()
         };
         foreach (var propertyInfo in enumerable)
         {
             if (propertyInfo.PropertyType == typeof(Uri))
             {
                 RegistrationObject[propertyInfo.Name] = null;
             }
             else if (propertyInfo.PropertyType == typeof(CDataMember))
             {
                 RegistrationObject[propertyInfo.Name] = null;
             }
             else if (propertyInfo.PropertyType == typeof(ISet<string>))
             {
                 RegistrationObject[propertyInfo.Name] = new List<TagInfo>();
             }
             else if (propertyInfo.PropertyType == typeof(MpnsHeaderCollection))
             {
                 RegistrationObject[propertyInfo.Name] = new List<NotificationInfo>();
             }
             else if (propertyInfo.PropertyType == typeof(WnsHeaderCollection))
             {
                 RegistrationObject[propertyInfo.Name] = new List<NotificationInfo>();
             }
             else if ((propertyInfo.GetMethod == null ||
                      (propertyInfo.GetMethod != null && !propertyInfo.GetMethod.IsPublic)) &&
                      (propertyInfo.SetMethod == null ||
                      (propertyInfo.SetMethod != null && !propertyInfo.SetMethod.IsPublic)) &&
                       propertyInfo.PropertyType == typeof(List<int>))
             {
                 RegistrationObject[propertyInfo.Name] = new ReadOnlyList<int>();
             }
         }
         registrationPropertyGrid.SelectedObject = RegistrationObject;
     }
     catch (Exception ex)
     {
         MainForm.SingletonMainForm.HandleException(ex);
     }
 }
        private void registrationsDataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            var bindingList = registrationsBindingSource.DataSource as SortableBindingList<RegistrationInfo>;
            if (bindingList == null)
            {
                return;
            }
            currentRegistrationRowIndex = e.RowIndex;
            var type = bindingList[e.RowIndex].Registration.GetType();
            var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            var registration = new CustomObject
            {
                Properties = propertyInfos.Select(p => new CustomProperty { Name = p.Name,
                                                                            Type = p.Name == ChannelUri ?
                                                                                   typeof(string) :
                                                                                   p.Name == BodyTemplate ?
                                                                                   typeof(string) :
                                                                                   p.Name == Tags ? 
                                                                                   typeof(List<string>) :
                                                                                   p.Name == MpnsHeaders || 
                                                                                   p.Name == WnsHeaders ?
                                                                                   typeof(List<NotificationInfo>) :
                                                                                   p.Name == Expressions ? 
                                                                                   typeof(ReadOnlyList<string>) :
                                                                                   p.Name == ExpressionLengths ||
                                                                                   p.Name == ExpressionStartIndices ?
                                                                                   typeof(ReadOnlyList<int>) :
                                                                                   p.PropertyType,
                                                                            Editor = p.Name == BodyTemplate ||
                                                                                    p.Name == ChannelUri ||
                                                                                    p.Name == DeviceToken ||
                                                                                    p.Name == GcmRegistrationId ?
                                                                                    new CustomTextEditor() as UITypeEditor : 
                                                                                    p.Name == Expressions || 
                                                                                    p.Name == ExpressionLengths ||
                                                                                    p.Name == ExpressionStartIndices ?
                                                                                    new ReadOnlyEditor() as UITypeEditor:
                                                                                    p.Name == Tags ||
                                                                                    p.Name == MpnsHeaders ||
                                                                                    p.Name == WnsHeaders ?
                                                                                    new CustomCollectionEditor() as UITypeEditor :
                                                                                    null,
                                                                            IsReadOnly = !p.CanWrite || 
                                                                                         p.SetMethod == null ||
                                                                                        (p.SetMethod != null && !p.SetMethod.IsPublic)
                }).ToList()
            };
            foreach (var propertyInfo in propertyInfos)
            {
                object value;
                if (propertyInfo.PropertyType == typeof(Uri))
                {
                    var member = propertyInfo.GetValue(bindingList[e.RowIndex].Registration) as Uri;
                    value = member != null ? member.AbsoluteUri : null;
                }
                else if (propertyInfo.PropertyType == typeof(CDataMember))
                {
                    var member = propertyInfo.GetValue(bindingList[e.RowIndex].Registration) as CDataMember;
                    value = member != null ? member.Value : null;
                }
                else if (propertyInfo.PropertyType == typeof(ISet<string>))
                {
                    var collection = propertyInfo.GetValue(bindingList[e.RowIndex].Registration) as ISet<string>;
                    value = collection == null ?
                            new List<TagInfo>() :
                            collection.Select(i => new TagInfo(i)).ToList();
                }
                else if (propertyInfo.PropertyType == typeof(MpnsHeaderCollection))
                {
                    var collection = propertyInfo.GetValue(bindingList[e.RowIndex].Registration) as MpnsHeaderCollection;
                    value = collection == null ? 
                            new List<NotificationInfo>() : 
                            collection.Select(i => new NotificationInfo(i.Key, i.Value)).ToList();
                }
                else if (propertyInfo.PropertyType == typeof(WnsHeaderCollection))
                {
                    var collection = propertyInfo.GetValue(bindingList[e.RowIndex].Registration) as WnsHeaderCollection;
                    value = collection == null ?
                            new List<NotificationInfo>() :
                            collection.Select(i => new NotificationInfo(i.Key, i.Value)).ToList();
                }
                else if ((propertyInfo.GetMethod == null ||
                         (propertyInfo.GetMethod != null && !propertyInfo.GetMethod.IsPublic)) &&
                         (propertyInfo.SetMethod == null ||
                         (propertyInfo.SetMethod != null && !propertyInfo.SetMethod.IsPublic)) &&
                          propertyInfo.PropertyType == typeof(List<int>))
                {
                    var collection = propertyInfo.GetValue(bindingList[e.RowIndex].Registration) as List<int>;
                    value = collection == null ? new ReadOnlyList<int>() : new ReadOnlyList<int>(collection);
                }
                else
                {
                    value = propertyInfo.GetValue(bindingList[e.RowIndex].Registration);
                }

                registration[propertyInfo.Name] = value;
            }
            registrationPropertyGrid.SelectedObject = registration;  
        }
        private void cboMpnsNotificationTemplate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboMpnsNotificationTemplate.SelectedIndex == 0)
            {
                mpnsTemplatePropertyGrid.SelectedObject = null;
                SetGotFocusHandler(txtMpnsPayload, textBox_GotFocus, true);
                return;
            }
            if (cboMpnsNotificationTemplate.SelectedIndex == 1)
            {
                mpnsTemplatePropertyGrid.SelectedObject = null;
                SetGotFocusHandler(txtMpnsPayload, textBox_GotFocus, false);
            }
            else
            {
                var methodInfo = mpnsMethodInfoList[cboMpnsNotificationTemplate.SelectedIndex - 2];
                var parameterInfos = methodInfo.GetParameters();

                var notification = new CustomObject
                {
                    Properties = parameterInfos.Select(p => new CustomProperty { Name = p.Name, Type = p.ParameterType}).ToList()
                };

                foreach (var property in notification.Properties.Where(property => property.Type == typeof (bool)))
                {
                    property.DefaultValue = property.Name.Contains("IsRelative") || property.Name.Contains("IsResource");
                    notification[property.Name] = property.DefaultValue;
                }

                mpnsTemplatePropertyGrid.SelectedObject = notification;
                var values = notification.Properties.Select(p => notification[p.Name]).ToArray();
                // ReSharper disable CoVariantArrayConversion
                mpnsPayload = methodInfo.Invoke(null, values) as string;
                // ReSharper restore CoVariantArrayConversion
                txtMpnsPayload.Text = XmlHelper.Indent(mpnsPayload);

                // Make txtMpnsTemplate editable if method == CreateRaw
                var isNotCreateRaw = string.Compare(methodInfo.Name, "CreateRaw", StringComparison.CurrentCultureIgnoreCase) != 0;
                SetGotFocusHandler(txtMpnsPayload, textBox_GotFocus, isNotCreateRaw);
            }
        }
        private void cboWnsNotificationTemplate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboWnsNotificationTemplate.SelectedIndex == 0)
            {
                wnsTemplatePropertyGrid.SelectedObject = null;
                SetGotFocusHandler(txtWnsPayload, textBox_GotFocus, true);
                return;
            }
            if (cboWnsNotificationTemplate.SelectedIndex == 1)
            {
                wnsTemplatePropertyGrid.SelectedObject = null;
                SetGotFocusHandler(txtWnsPayload, textBox_GotFocus, false);
            }
            else
            {
                var methodInfo = wnsMethodInfoList[cboWnsNotificationTemplate.SelectedIndex - 2];
                var parameterInfos = methodInfo.GetParameters();
                
                var notification = new CustomObject
                    {
                        Properties = parameterInfos.Select(p => new CustomProperty {Name = p.Name, Type = p.ParameterType}).ToList()
                    };

                wnsTemplatePropertyGrid.SelectedObject = notification;
                var values = notification.Properties.Select(p => notification[p.Name]).ToArray();
                // ReSharper disable CoVariantArrayConversion
                wnsPayload = methodInfo.Invoke(null, values) as string;
                // ReSharper restore CoVariantArrayConversion
                if (!string.IsNullOrWhiteSpace(wnsPayload))
                {
                    txtWnsPayload.Text = XmlHelper.Indent(wnsPayload);
                }
                SetGotFocusHandler(txtWnsPayload, textBox_GotFocus, true);
            }
        }