private void AddPropertyLabel(SampleProperty property)
        {
            var propertyLabel = new TextBlock {
                Text = property.Name, Foreground = new SolidColorBrush(Colors.Black)
            };

            this.RootPanel.Children.Add(propertyLabel);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes the sample.
        /// </summary>
        /// <returns>
        /// Returns a task.
        /// </returns>
        public async Task InitializeAsync()
        {
            if (string.IsNullOrWhiteSpace(this.BindingFileSource))
            {
                return;
            }

            if (this.BindingSource == null)
            {
                using (var stream = await StorageStreamHelper.GetPackagedFileStreamAsync($"Samples/{this.GroupName}/{this.Name}/{this.BindingFileSource}"))
                {
                    var bindingSource = await stream.ReadAsStringAsync();

                    var regex = new Regex(@"@\[(?<name>.+?):(?<type>.+?):(?<value>.+?)(:(?<parameters>.*))*\]");

                    this.BindingSource = new SampleProperties();
                    var bindings = (IDictionary <string, object>) this.BindingSource.Bindings;

                    foreach (Match match in regex.Matches(bindingSource))
                    {
                        var propertyName       = match.Groups["name"].Value;
                        var propertyTypeString = match.Groups["type"].Value;
                        var defaultValueString = match.Groups["value"].Value;

                        var propertyType = ParseHelper.SafeParseEnum <SamplePropertyType>(propertyTypeString);

                        SampleProperty property;

                        switch (propertyType)
                        {
                        case SamplePropertyType.Enum:
                            try
                            {
                                property = new SampleProperty();
                                var split      = defaultValueString.Split('.');
                                var typeString = string.Join(".", split.Take(split.Length - 1));
                                var enumType   = typeString.GetTypeByName(true, true);
                                property.DefaultValue = Enum.Parse(enumType, split.Last());
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                            break;

                        case SamplePropertyType.Boolean:
                            property = new SampleProperty
                            {
                                DefaultValue = ParseHelper.SafeParseBool(defaultValueString)
                            };
                            break;

                        case SamplePropertyType.Integer:
                            try
                            {
                                var integerProperty = new IntegerSampleProperty
                                {
                                    DefaultValue =
                                        ParseHelper.SafeParseDouble(defaultValueString)
                                };

                                var parameters = match.Groups["parameters"].Value;
                                var split      = parameters.Split('-');

                                integerProperty.MinValue = ParseHelper.SafeParseDouble(split[0]);
                                integerProperty.MaxValue = ParseHelper.SafeParseDouble(split[1]);

                                property = integerProperty;
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                            break;

                        default:
                            property = new SampleProperty()
                            {
                                DefaultValue = defaultValueString
                            };
                            break;
                        }

                        property.Name          = propertyName;
                        property.Code          = match.Value;
                        property.Type          = propertyType;
                        bindings[propertyName] = new SamplePropertyValue(property.DefaultValue);

                        this.BindingSource.Properties.Add(property);
                    }
                }
            }
        }