public IBindingContext CreateContext()
        {
            var context = BindingContext.Create();

            foreach (var binding in m_bindings)
            {
                List <IBindingRequirement> requirements = new List <IBindingRequirement>();

                foreach (var req in binding.Dependencies)
                {
                    var realReq = BindingRequirements.Instance.With(req.name, req.BindingType);
                    requirements.Add(realReq);
                }


                var args = new List <Type>(binding.Factory.GetParameters().Select(p => p.ParameterType));
                args.Add(binding.Factory.ReturnType);

                var delegateType = Expression.GetFuncType(args.ToArray());

                var factory = System.Delegate.CreateDelegate(delegateType, binding.Factory);

                IBinding realBinding = new Binding(factory, requirements.ToArray());
                if (binding.Singleton)
                {
                    realBinding = new SingletonBinding(realBinding);
                }

                AddToContext(context, binding.Root, realBinding, binding.Subcontexts);
            }

            return(context);
        }
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            View.Frame            = UIScreen.MainScreen.Bounds;
            View.BackgroundColor  = UIColor.White;
            View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;

            UILabel label = new UILabel(new RectangleF(0, 30, View.Frame.Width, 50));

            View.AddSubview(label);

            button = UIButton.FromType(UIButtonType.RoundedRect);

            button.Frame = new RectangleF(
                View.Frame.Width / 2 - buttonWidth / 2,
                View.Frame.Height / 2 - buttonHeight / 2,
                buttonWidth,
                buttonHeight);

            button.SetTitle("Click me", UIControlState.Normal);
            View.AddSubview(button);

            // Do the bindings.
            TestViewModel vm = new TestViewModel();

            BindingContext.Create(vm)
            .Add(s => s.TotalClicks, label, l => l.Text)
            .Add(s => s.TotalClicks, s => button.SetTitle("Total Clicks is now " + s.TotalClicks, UIControlState.Normal));

            CommandBinder.Create()
            .Add(button, "TouchUpInside", vm.IncrementCounter,
                 stateChanged: tf => button.Enabled = tf);
        }
        internal static TemplateDelegate CompileView(ViewReaderFactory readerFactoryFactory, string templatePath, CompilationContext compilationContext)
        {
            var configuration = compilationContext.Configuration;
            IEnumerable <object> tokens;

            using (var sr = readerFactoryFactory(configuration, templatePath))
            {
                using (var reader = new ExtendedStringReader(sr))
                {
                    tokens = Tokenizer.Tokenize(reader).ToArray();
                }
            }

            var layoutToken = tokens.OfType <LayoutToken>().SingleOrDefault();

            var expressions  = ExpressionBuilder.ConvertTokensToExpressions(tokens, configuration);
            var compiledView = FunctionBuilder.Compile(expressions, compilationContext);

            if (layoutToken == null)
            {
                return(compiledView);
            }

            var fs         = configuration.FileSystem;
            var layoutPath = fs.Closest(templatePath, layoutToken.Value + ".hbs");

            if (layoutPath == null)
            {
                throw new InvalidOperationException($"Cannot find layout '{layoutToken.Value}' for template '{templatePath}'");
            }

            var compiledLayout = CompileView(readerFactoryFactory, layoutPath, new CompilationContext(compilationContext));

            return((in EncodedTextWriter writer, BindingContext context) =>
            {
                var config = context.Configuration;
                using var bindingContext = BindingContext.Create(config, null);
                foreach (var pair in context.ContextDataObject)
                {
                    switch (pair.Key.WellKnownVariable)
                    {
                    case WellKnownVariable.Parent:
                    case WellKnownVariable.Root:
                        continue;
                    }

                    bindingContext.ContextDataObject[pair.Key] = pair.Value;
                }

                using var innerWriter = ReusableStringWriter.Get(config.FormatProvider);
                using var textWriter = new EncodedTextWriter(innerWriter, config.TextEncoder, FormatterProvider.Current, true);
                compiledView(textWriter, context);
                var inner = innerWriter.ToString();

                var viewModel = new LayoutViewModel(inner, context.Value);
                bindingContext.Value = viewModel;

                compiledLayout(writer, bindingContext);
            });
Esempio n. 4
0
        internal static TemplateDelegate CompileView(ViewReaderFactory readerFactoryFactory, string templatePath, CompilationContext compilationContext)
        {
            var configuration = compilationContext.Configuration;
            IEnumerable <object> tokens;

            using (var sr = readerFactoryFactory(configuration, templatePath))
            {
                using (var reader = new ExtendedStringReader(sr))
                {
                    tokens = Tokenizer.Tokenize(reader).ToArray();
                }
            }

            var layoutToken = tokens.OfType <LayoutToken>().SingleOrDefault();

            var expressions  = ExpressionBuilder.ConvertTokensToExpressions(tokens, configuration);
            var compiledView = FunctionBuilder.Compile(expressions, compilationContext);

            if (layoutToken == null)
            {
                return(compiledView);
            }

            var fs         = configuration.FileSystem;
            var layoutPath = fs.Closest(templatePath, layoutToken.Value + ".hbs");

            if (layoutPath == null)
            {
                throw new InvalidOperationException($"Cannot find layout '{layoutToken.Value}' for template '{templatePath}'");
            }

            var compiledLayout = CompileView(readerFactoryFactory, layoutPath, new CompilationContext(compilationContext));

            return((in EncodedTextWriter writer, BindingContext context) =>
            {
                var config = context.Configuration;
                using var innerWriter = ReusableStringWriter.Get(config.FormatProvider);
                using var textWriter = new EncodedTextWriter(innerWriter, config.TextEncoder, config.UnresolvedBindingFormatter, true);
                compiledView(textWriter, context);
                var inner = innerWriter.ToString();

                var vmContext = new [] { new { body = inner }, context.Value };
                var viewModel = new DynamicViewModel(vmContext);
                using var bindingContext = BindingContext.Create(config, viewModel);

                compiledLayout(writer, bindingContext);
            });
Esempio n. 5
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button   button = FindViewById <Button>(Resource.Id.MyButton);
            TextView label  = FindViewById <TextView>(Resource.Id.TheLabel);

            // Do the bindings.
            TestViewModel vm = new TestViewModel();

            BindingContext.Create(vm)
            .Add(s => s.TotalClicks, label, l => l.Text)
            .Add(s => s.TotalClicks, s => button.Text = "Total Clicks is now " + s.TotalClicks);

            CommandBinder.Create().Add(button, "Click", vm.IncrementCounter);
        }
Esempio n. 6
0
        public void ExpandTokenGroupsViaCrackNames()
        {
            //point this to a user in the directory
            DirectoryEntry user = TestUtils.CreateDirectoryEntry(
                "CN=User1,OU=Users," + TestUtils.Settings.DefaultPartition);

            //you can use another overload to pass credentials
            using (BindingContext ctx = BindingContext.Create())
                using (user)
                {
                    //convert to array of string SIDs
                    int size = user.Properties["tokenGroups"].Count;
                    PropertyValueCollection pvc = user.Properties["tokenGroups"];

                    string[] sids = new string[size];

                    for (int i = 0; i < size; i++)
                    {
                        sids[i] = AdUtils.ConvertSidToString((byte[])pvc[i]);
                    }

                    //we want to pass in the SID format and retrieve
                    //the NT Format names.  This utility class is
                    //included in our web site library samples
                    //groupNames contains all the converted groups now
                    string[] groupNames = AdUtils.DsCrackNamesWrapper(
                        sids,
                        ctx.Handle,
                        DS_NAME_FORMAT.DS_SID_OR_SID_HISTORY_NAME,
                        DS_NAME_FORMAT.DS_NT4_ACCOUNT_NAME
                        );

                    foreach (string group in groupNames)
                    {
                        Console.WriteLine(group);
                    }
                }
        }
Esempio n. 7
0
        public static IBindingContext GetSubcontext(this IBindingContext me, params object[] names)
        {
            IBindingContext context = me;
            var             length  = names.Length;

            for (int i = 0; i < length; ++i)
            {
                var             name = names[i];
                IBindingContext currentContext;

                if (!context.TryGet <IBindingContext>(name, out currentContext))
                {
                    currentContext = BindingContext.Create();
                    currentContext.FallBack(context);
                    context.Bind <IBindingContext>(name).To(currentContext);
                }

                context = currentContext;
            }



            return(context);
        }